Presentation is loading. Please wait.

Presentation is loading. Please wait.

The Elements of Programming Style

Similar presentations


Presentation on theme: "The Elements of Programming Style"— Presentation transcript:

1 The Elements of Programming Style

2 References The elements of programming style by Kernighan and Plauger
The practice of programming by Kernighan and Pike The pragmatic programmer by Hunt and Thomas

3 What distinguishes journeyman from master?
Attitude Style Philosophy

4 Documentation The only reliable document is the code.
A comment is of <=0 value if it is wrong. Make sure comments and code agree. Don’t just echo the code with comments – make every comment count. Don’t comment on bad code – rewrite it. Comment functions and global data. Don’t over-comment.

5 Names Use descriptive names for globals, short names for locals.
Be consistent. Use active names for functions. Be accurate.

6 Using descriptive names for globals and short names for locals.
int npending = 0; for (theElementIndex=0; theElementIndex<numberOfElements; theElementIndex++) elementArray[theElementIndex] = theElementIndex; for (i =0; i<nElem; i++) arr[i] = i;

7 Be consistent. class UserQueue { }
int noOfItemsInQ, frontOfTheQueue, queueCapacity; … …. } int nitems, front, capacity;

8 Use active names for functions
int getHour(); bool isEven(int n);

9 Be accurate bool isOdd(int num) { return (num%2 == 0); }

10 Expressions and statements
Say what you mean, simply and directly. Use the natural form for expression. Parenthesize to resolve ambiguity. Break up complex expressions. Replace repetitive expressions by calls to a common function. Be careful with side effects. Use the “telephone test” for readability.

11 Say what you mean, simply and directly.
int v[10][10]; for (int i = 1; i<=10; i++) for (int j = 1; j<=10; j++) v[i-1][j-1] = (i/j)*(j/i);

12 Use natural form for expression
if (!(block_id < actblks) || !(block_id >= unblocks)) if ((block_id >= actblks) || (block_id < unblocks))

13 Parenthesize to resolve ambiguity.
*this.hour (*this).hour or *(this.hour)?

14 Break up complex expression
*x += (*xp = (2*k <(n-m)?c[k+1];d[k--])); if (2*k < (n-m)) *xp = c[k+1]; else *xp = d[k--]; *x += *xp;

15 Be careful with side effects
scanf(“%d %d”, &yr, &profit[yr]); scanf(“%d”, &yr); scanf(“%d”, &profit[yr]);

16 Consistency and idioms
Use a consistent indentation and brace style. Use idioms for consistency. Use else-if for multi-way decision.

17 Use idiom for consistency
while (i <= n-1) array[i++] = 1.0; for (i=0;i<n;) for (i=n;--i>=0;) array[i] = 1.0; for (i=0;i<n;i++)

18 Use idiom for consistency
do { c = getchar(); putchar(c); } while (c!=EOF); while ((c=getchar())!=EOF)

19 Use idiom for consistency
int i, *iArray, nmemb; iArray = malloc(nmemb*sizeof(int)); for (i = 0; i<=nmemb; i++) iArray[i]=i; for (i = 0; i<nmemb; i++)

20 Use else-if for multi-way decisions
if (argc == 3) if ((fin=fopen(argv[1],”r”)) !=NULL) if ((fout = fopen(argv[2],”w”)) != NULL) { while ((c = getc(fin)) !=EOF) putc(c,fout); fclose(fin); fclose(fout); } else printf(“Can’t open output file %s\n”, argv[2]); else printf(“Can’t open input file %s\n”, argv[1]); printf(“Usage: cp input file output file\n”);

21 Use else-if for multi-way decisions
if (argc != 3) printf(“Usage: cp input file output file\n”); else if ((fin=fopen(argv[1],”r”)) ==NULL) printf(“Can’t open input file %s\n”, argv[1]); else if ((fout = fopen(argv[2],”w”)) == NULL) printf(“Can’t open output file %s\n”, argv[2]); else { while ((c = getc(fin)) !=EOF) putc(c,fout); fclose(fin); fclose(fout); }

22 Avoid function macros while ((a=getchar() !=EOF) && isupper(a))
#define isupper(c) ((c)>=‘A’ && (c) <=‘Z’) while (isupper(a = getchar())) while ((a=getchar() !=EOF) && isupper(a))

23 Parenthesize the macro body and arguments
#define square(x) (x)+(x) 1/square(y) will be expanded to 1/(y)+(y) #define square(x) ((x)*(x)) 1/((y)+(y))


Download ppt "The Elements of Programming Style"

Similar presentations


Ads by Google