Programming Style - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Programming Style

Description:

This is the common 'idiom' that any programmer will recognize. 13 ... Consistent conventions & idioms. 20. CSE-105 Structured Programming. CSE, BUET. Why Bother? ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 21
Provided by: bue9
Category:

less

Transcript and Presenter's Notes

Title: Programming Style


1
Programming Style
  • CSE 105
  • Structured Programming Language
  • Presentation - 2

Thanks to Arie Rapoport, cs.huji.ac.il
2
Word Counting (KnR 1.5.4)
Start
stateOUT nlnwnc0
  • include ltstdio.hgt
  • define IN 1 / inside a word /
  • define OUT 0 / outside a word /
  • / count lines, words, and characters in input /
  • main( )
  • int c, nl, nw, nc, state
  • state OUT
  • nl nw nc 0
  • while ((c getchar()) ! EOF)
  • nc
  • if (c \n)
  • nl
  • if (c c \n c \t)
  • sate OUT

c getchar()
c ! EOF
N
Y
nc
c\n
N
Y
nl
c is white- char
state OUT
N
Y
Y
state OUT
stateIN nw
print nl,nw,nc
End
3
Program Style
  • Readability
  • Clarity
  • Common sense
  • Right focus

4
Whats in a name
  • Example
  • define ONE 1
  • define TEN 10
  • define TWENTY 20
  • More reasonable
  • define INPUT_MODE 1
  • define INPUT_BUFSIZE 10
  • define OUTPUT_BUFSIZE 20

5
Whats in a name
  • Use descriptive names for global variables
  • int npending 0 // current length of input
    queue
  • Naming conventions vary
  • numPending
  • num_pending
  • NumberOfPendingEvents

6
Whats in a name
  • Compare
  • for( theElementIndex 0
  • theElementIndex lt numberOfElements
  • theElementIndex )
  • elementArraytheElementIndex
    theElementIndex
  • and
  • for( i 0 i lt nelems i )
  • elemi i
  • Use short names for locals

7
Whats in a name
  • Consider
  • int noOfItemsInQ
  • int frontOfTheQueue
  • int queueCapacity
  • The word queue appears in 3 different ways
  • Be Consistent
  • Follow naming guidelines used by your peers

8
Whats in a name
  • Use active name for functions
  • now getDate()
  • Compare
  • if( checkdigit(c) )
  • to
  • if( isdigit(c) )
  • Accurate active names makes bugs apparent

9
Indentation
  • Use indentation to show structure
  • Compare
  • for(n n lt100 fieldn 0)
  • c 0 return \n
  • to
  • for( n n lt100 n)
  • fieldn 0
  • c 0
  • return \n

10
Expressions
  • Use parenthesis to resolve ambiguity
  • Compare
  • leap_year y 4 0 y 100 ! 0
  • y 400 0
  • to
  • leap_year ((y 4 0) (y 100 ! 0))
  • (y 400 0)

11
Statements
  • Use braces to resolve ambiguity
  • Compare
  • if( i lt 100 )
  • x i
  • i
  • to
  • if( i lt 100 )
  • x i
  • i

12
Idioms
  • Do not try to make code interesting
  • i 0
  • while( i lt n 1 )
  • arrayi 1
  • for( i 0 i lt n )
  • arrayi 1
  • for( i n --i gt 0 )
  • arrayi 1
  • for( i 0 i lt n i )
  • arrayi 1

This is the common idiom that any programmer
will recognize
13
Idioms
  • Use else if for multi-way decisions
  • if ( cond1 )
  • statement1
  • else if ( cond2 )
  • statement2
  • else if ( condn )
  • statementn
  • else
  • default-statement

14
Idioms
  • if( x gt 0 )
  • if( y gt 0 )
  • if( xy lt 100 )
  • ...
  • else
  • printf(Too large!\n" )
  • else
  • printf("y too small!\n")
  • else
  • printf("x too small!\n")
  • if( x lt 0 )
  • printf("x too small!\n")
  • else if( y lt 0 )
  • printf("y too small!\n")
  • else if( xy gt 100 )
  • printf("Sum too large!\n" )
  • else
  • ...

15
Comments
  • Dont belabor the obvious
  • // return SUCCESS
  • return SUCCESS
  • // Initialize total to number_received
  • total number_received
  • Test does comment add something that is not
    evident from the code

16
Comments
  • Document each function
  • // random return a random integer in 0..r
  • int random( int r )
  • return (int)floor(rand()r)

17
Comments
  • A more elaborate function comment
  • //
  • // GammaGreaterThanOne( Alpha )
  • //
  • // Generate a gamma random variable when alpha gt
    1.
  • //
  • // Assumption Alpha gt 1
  • //
  • // Reference Ripley, Stochastic Simulation, p.90
  • // Chang and Feast, Appl.Stat. (28) p.290
  • //
  • double GammaGreaterThanOne( double Alpha )

18
Comments
  • Dont comment bad code rewrite it!
  • // If result 0 a match was found so return
  • // true otherwise return false
  • return !result
  • Instead
  • return matchfound

19
Style recap
  • Descriptive names
  • Clarity in expressions
  • Straightforward flow
  • Readability of code comments
  • Consistent conventions idioms

20
Why Bother?
  • Good style
  • Easy to understand code
  • Smaller polished
  • Makes errors apparent
  • Yields a good grade
  • Sloppy code ?bad code
  • Hard to read
  • Broken flow
  • Harder to find errors correct them, maintenance.
Write a Comment
User Comments (0)
About PowerShow.com