20
CSE-105 – Structured Programming CSE, BUET Programming Style CSE 105 Structured Programming Language Presentation - 2 Thanks to Arie Rapoport, cs.huji.ac.il

CSE-105 – Structured Programming CSE, BUET Programming Style CSE 105 Structured Programming Language Presentation - 2 Thanks to Arie Rapoport, cs.huji.ac.il

Embed Size (px)

Citation preview

Page 1: CSE-105 – Structured Programming CSE, BUET Programming Style CSE 105 Structured Programming Language Presentation - 2 Thanks to Arie Rapoport, cs.huji.ac.il

CSE-105 – Structured ProgrammingCSE, BUET

Programming Style

CSE 105Structured Programming Language

Presentation - 2

Thanks to Arie Rapoport, cs.huji.ac.il

Page 2: CSE-105 – Structured Programming CSE, BUET Programming Style CSE 105 Structured Programming Language Presentation - 2 Thanks to Arie Rapoport, cs.huji.ac.il

2

CSE-105 – Structured ProgrammingCSE, BUET

Word Counting (KnR – 1.5.4)

#include <stdio.h>

#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;else if (state == OUT) {

state = IN;++nw;

}}printf(“%d %d %d\n”, nl, nw, nc);

}

c != EOF

Start

++nc;

state=OUTnl=nw=nc=0

c = getchar()

++nl

c==‘\n‘

state = OUT

c is white- char

state==OUT

state=IN++nw

print nl,nw,nc

End

Y

N

Y

N

Y

N

Y

Page 3: CSE-105 – Structured Programming CSE, BUET Programming Style CSE 105 Structured Programming Language Presentation - 2 Thanks to Arie Rapoport, cs.huji.ac.il

3

CSE-105 – Structured ProgrammingCSE, BUET

Program StyleReadabilityClarityCommon senseRight focus

Page 4: CSE-105 – Structured Programming CSE, BUET Programming Style CSE 105 Structured Programming Language Presentation - 2 Thanks to Arie Rapoport, cs.huji.ac.il

4

CSE-105 – Structured ProgrammingCSE, BUET

What’s in a nameExample

#define ONE 1

#define TEN 10

#define TWENTY 20

More reasonable#define INPUT_MODE 1

#define INPUT_BUFSIZE 10

#define OUTPUT_BUFSIZE 20

Page 5: CSE-105 – Structured Programming CSE, BUET Programming Style CSE 105 Structured Programming Language Presentation - 2 Thanks to Arie Rapoport, cs.huji.ac.il

5

CSE-105 – Structured ProgrammingCSE, BUET

What’s in a nameUse descriptive names for global

variables

int npending = 0; // current length of input queue

Naming conventions varynumPendingnum_pendingNumberOfPendingEvents…

Page 6: CSE-105 – Structured Programming CSE, BUET Programming Style CSE 105 Structured Programming Language Presentation - 2 Thanks to Arie Rapoport, cs.huji.ac.il

6

CSE-105 – Structured ProgrammingCSE, BUET

What’s in a nameComparefor( theElementIndex = 0;

theElementIndex < numberOfElements;

theElementIndex++ )

elementArray[theElementIndex] = theElementIndex;

andfor( i = 0; i < nelems; i++ )

elem[i] = i;

Use short names for locals

Page 7: CSE-105 – Structured Programming CSE, BUET Programming Style CSE 105 Structured Programming Language Presentation - 2 Thanks to Arie Rapoport, cs.huji.ac.il

7

CSE-105 – Structured ProgrammingCSE, BUET

What’s in a nameConsiderint noOfItemsInQ;

int frontOfTheQueue;

int queueCapacity;

The word “queue” appears in 3 different ways

Be ConsistentFollow naming guidelines used by your peers

Page 8: CSE-105 – Structured Programming CSE, BUET Programming Style CSE 105 Structured Programming Language Presentation - 2 Thanks to Arie Rapoport, cs.huji.ac.il

8

CSE-105 – Structured ProgrammingCSE, BUET

What’s in a nameUse active name for functions

now = getDate()

Compare if( checkdigit(c) ) …to if( isdigit(c) ) …

Accurate active names makes bugs apparent

Page 9: CSE-105 – Structured Programming CSE, BUET Programming Style CSE 105 Structured Programming Language Presentation - 2 Thanks to Arie Rapoport, cs.huji.ac.il

9

CSE-105 – Structured ProgrammingCSE, BUET

IndentationUse indentation to show structure

Compare for(n++; n <100; field[n++] = 0);

c = 0; return ‘\n’;

to for( n++; n <100; n++)

field[n] = 0;

c = 0;

return ‘\n’;

Page 10: CSE-105 – Structured Programming CSE, BUET Programming Style CSE 105 Structured Programming Language Presentation - 2 Thanks to Arie Rapoport, cs.huji.ac.il

10

CSE-105 – Structured ProgrammingCSE, BUET

ExpressionsUse parenthesis to resolve ambiguity

Compareleap_year = y % 4 == 0 && y %100 != 0

|| y % 400 == 0;

toleap_year = ((y % 4 == 0) && (y %100 != 0))

|| (y % 400 == 0);

Page 11: CSE-105 – Structured Programming CSE, BUET Programming Style CSE 105 Structured Programming Language Presentation - 2 Thanks to Arie Rapoport, cs.huji.ac.il

11

CSE-105 – Structured ProgrammingCSE, BUET

StatementsUse braces to resolve ambiguityCompareif( i < 100 )

x = i;

i++;

toif( i < 100 )

{

x = i;

}

i++;

Page 12: CSE-105 – Structured Programming CSE, BUET Programming Style CSE 105 Structured Programming Language Presentation - 2 Thanks to Arie Rapoport, cs.huji.ac.il

12

CSE-105 – Structured ProgrammingCSE, BUET

IdiomsDo not try to make code “interesting”

i = 0;

while( i <= n – 1 )

array[i++] = 1;

for( i = 0; i < n; )

array[i++] = 1;

for( i = n; --i >= 0; )

array[i] = 1;

for( i = 0; i < n; i++ )

array[i] = 1; This is the common “idiom” that any programmer will recognize

Page 13: CSE-105 – Structured Programming CSE, BUET Programming Style CSE 105 Structured Programming Language Presentation - 2 Thanks to Arie Rapoport, cs.huji.ac.il

13

CSE-105 – Structured ProgrammingCSE, BUET

IdiomsUse “else if” for multi-way decisions

if ( cond1 )

statement1

else if ( cond2 )

statement2

else if ( condn )

statementn

else

default-statement

Page 14: CSE-105 – Structured Programming CSE, BUET Programming Style CSE 105 Structured Programming Language Presentation - 2 Thanks to Arie Rapoport, cs.huji.ac.il

14

CSE-105 – Structured ProgrammingCSE, BUET

Idiomsif( x > 0 )

if( y > 0 )

if( x+y < 100 )

{

...

} else

printf(“Too large!\n" );

else

printf("y too small!\n");

else

printf("x too small!\n");

if( x <= 0 )

printf("x too small!\n");

else if( y <= 0 )

printf("y too small!\n");

else if( x+y >= 100 )

printf("Sum too large!\n" );

else

{

...

}

Page 15: CSE-105 – Structured Programming CSE, BUET Programming Style CSE 105 Structured Programming Language Presentation - 2 Thanks to Arie Rapoport, cs.huji.ac.il

15

CSE-105 – Structured ProgrammingCSE, BUET

CommentsDon’t 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

Page 16: CSE-105 – Structured Programming CSE, BUET Programming Style CSE 105 Structured Programming Language Presentation - 2 Thanks to Arie Rapoport, cs.huji.ac.il

16

CSE-105 – Structured ProgrammingCSE, BUET

CommentsDocument each function

// random: return a random integer in [0..r]

int random( int r )

{

return (int)floor(rand()*r);

}

Page 17: CSE-105 – Structured Programming CSE, BUET Programming Style CSE 105 Structured Programming Language Presentation - 2 Thanks to Arie Rapoport, cs.huji.ac.il

17

CSE-105 – Structured ProgrammingCSE, BUET

CommentsA more elaborate function comment

//

// GammaGreaterThanOne( Alpha )

//

// Generate a gamma random variable when alpha > 1.

//

// Assumption: Alpha > 1

//

// Reference: Ripley, Stochastic Simulation, p.90

// Chang and Feast, Appl.Stat. (28) p.290

//

double GammaGreaterThanOne( double Alpha )

{ … }

Page 18: CSE-105 – Structured Programming CSE, BUET Programming Style CSE 105 Structured Programming Language Presentation - 2 Thanks to Arie Rapoport, cs.huji.ac.il

18

CSE-105 – Structured ProgrammingCSE, BUET

CommentsDon’t comment bad code – rewrite it!

// If result = 0 a match was found so return

// true; otherwise return false;

return !result;

Instead…

return matchfound;

Page 19: CSE-105 – Structured Programming CSE, BUET Programming Style CSE 105 Structured Programming Language Presentation - 2 Thanks to Arie Rapoport, cs.huji.ac.il

19

CSE-105 – Structured ProgrammingCSE, BUET

Style recapDescriptive namesClarity in expressionsStraightforward flowReadability of code & commentsConsistent conventions & idioms

Page 20: CSE-105 – Structured Programming CSE, BUET Programming Style CSE 105 Structured Programming Language Presentation - 2 Thanks to Arie Rapoport, cs.huji.ac.il

20

CSE-105 – Structured ProgrammingCSE, BUET

Why Bother?

Good style:Easy to understand codeSmaller & polishedMakes errors apparentYields a good grade

Sloppy code bad codeHard to readBroken flow Harder to find errors & correct them, maintenance.