Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Doing C with Style.

Similar presentations


Presentation on theme: "Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Doing C with Style."— Presentation transcript:

1 Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Doing C with Style

2 Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Style vs. Standards “Standards are important! Everyone should have one!” ”A foolish consistency is the hobgoblin of little minds, adored by little statesmen and philosophers and divines.” Ralph Waldo Emerson “Consistency is the last refuge of the unimaginative.” Oscar Wilde

3 Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Disclaimer The rest of this lecture describes the style I prefer. You may prefer other style conventions. There is no “right” or “wrong” style! Your style should reflect your view of the world… not mine! No points will ever be deducted for using a different style Points may be deducted for obfuscation e.g. don’t choose a style that uses variable names like: AaTtrrXxFfVvvG

4 Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Capatilization #defines are all caps: #define ARRAY_SIZE 256 Variable/function names are lower case: int area; Unless two word names Either separate words with underscores: int box_width=6; Or capitalize first letter of second word: int boxWidth=6; Constants - First letter capitalized: const double Avogadro=6.23e26;

5 Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Indentation Use small tab settings – 2 or 3 spaces or 0.3” Indent lines between “{“ and “}” Indent continued lines int main(int argc, char **argv) { if (argc==0) printf(“No arguments\n); else { printf(“Arguments…\n”); for(i=0; i<argc; i++) { printf(“ %d: %s\n”, i,argv[i]); }

6 Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Alternate Indentation Put “{“ and “}” on their own lines Allows “{“ to line up vertically with “}” Takes more vertical space int main(int argc, char **argv) { if (argc==0) printf(“No arguments\n); else { printf(“Arguments…\n”); for(i=0; i<argc; i++) { printf(“ %d: %s\n”, i,argv[i]); }

7 Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Comments Only when code is not obvious Line comments using // Block comments using /*------------------ --------------------*/ Normal comments use indentation Debug comments out-dented … x = i>>3; // i/8 round <- … while(x>0) { /*------------ Process this stuff as … … -------------*/ x=myfunc(z) … /*DBG*/ printf(“x is now %d\n”,x); }

8 Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Commenting Out Code Best: #if 0 / #endif Single Lines… // Debug /* DBG */ …. or /* DBG … */

9 Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 C File Order #defines #includes (order?.. I put mine first) Global Variable Definitions Function Declaraions (except main, in call order?) Function Definitions (in call order?)

10 Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Multi-File Organization Limit number of functions in one file Use files to group similar functions One header file for each auxiliary C file Header file : function declarations (defined in C file) Header file contains #includes required for function declarations Use #ifdef XYZ_H #define XYZ_H … #endif around xyz.h contents


Download ppt "Binghamton University CS-220 Spring 2015 Binghamton University CS-220 Spring 2015 Doing C with Style."

Similar presentations


Ads by Google