Effective Programming Style Guidelines

the elements of programming style n.w
1 / 23
Embed
Share

Dive into the essence of programming style with insights on attitude, documentation, naming conventions, consistency, and clarity in expressions and statements.

  • Programming
  • Guidelines
  • Style
  • Documentation
  • Coding

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

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; . } class UserQueue { 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 i = 0; while (i <= n-1) array[i++] = 1.0; for (i=0;i<n;) array[i++] = 1.0; for (i=n;--i>=0;) array[i] = 1.0; for (i=0;i<n;i++) array[i] = 1.0;

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

  19. Use idiom for consistency int i, *iArray, nmemb; iArray = malloc(nmemb*sizeof(int)); for (i = 0; i<=nmemb; i++) iArray[i]=i; int i, *iArray, nmemb; iArray = malloc(nmemb*sizeof(int)); for (i = 0; i<nmemb; i++) iArray[i]=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]); else 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 #define isupper(c) ((c)>= A && (c) <= Z ) while (isupper(a = getchar())) #define isupper(c) ((c)>= A && (c) <= Z ) 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/square(y) will be expanded to 1/((y)+(y))

Related


More Related Content