Programming Style Requirements

MIM 3132, C/UNIX for Information Systems

Summer 2001

Programming Style Requirements: 1. Robustness. Check all input for validity. Invalid input should generate an error condition. 2. Generality Program should be as general as possible. Avoid code that refers to specific input. Use constants where appropriate. 3. Input/Output Behavior With interactive programs, prompt the user for correct input. Output should be clearly laid out and well labeled. Do not hesitate to use blank lines to improve format. 4. Modularity Break down the program logically into modules. No global variables! 5. Global Definitions Structure definitions, typedefs, #define-ed constants, and other user- defined data types are generally global. Function prototypes can also be global. Never, under any circumstances, should variables be declared global. 6. Testing As input supply both valid and invalid data to ensure your program is capable of handing error conditions. Invalid data will be used during the grading process. 7. Simplicity Without loss of generality, keep your program as simple as possible. Concentrate on fulfilling the expectations of the assignment first, and later add any extra features you may choose to include. 8. Mnemonics Select meaningful identifiers for variables, functions, constants, etc. 9. Documentation Comment statements are required. Lack thereof will affect the grade. Good documentation is essential both for the programmer's own reference and for grading purposes. Begin the program with your name, the date, and a general description of the program, including valid input and expected output. Be sure to state any assumptions here. Explain variable definitions, the purpose of functions. Label end brackets. Keep comments over to the right as much as possible away from the code. Avoid comments that are so long they "snake" to the next line. 10. Parameter Passing. Do not use variables declared on higher levels unless they have been passed as parameters. 11. Indentation. Follow ANSI guidelines. All blocks of code should be indented a minimum of 3 spaces. Make sure end-brackets line up with the line on which the begin-brackets appears. End-brackets should not occur on the same line as code. Begin-Brackets should appear on a line of code only if they are at the end of the line. Separate clauses of a compound statement should appear on separate lines.

Example: if(x==3) if(x==3) printf("x equals 3\n"); printf("x equals 3\n"); else printf("x not 3\n"); else printf("x not 3\n"); while(i<10){ while(i<10) line1; { line2; line1; line3; line2; } line3; } Unacceptable Styles: for(i=0;i<10;i++)printf("%d\n",i); if(x<y)printf("...");else x=10;