09 coding standards_n_guidelines


Published on

Principles of Software Engineering by Utpal Roy, Jadavpur University, Kolkata, India

Published in: Education, Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

09 coding standards_n_guidelines

  1. 1. CODING STANDARDS AND GUIDELINES CODING STANDARDS AND GUIDELINES Let’s Discuss The IDEAL Coding AttitudeJanuary 13, 2012 Made By Utpal Ray 1
  2. 2. CODING STANDARDS AND GUIDELINES CODING STANDARDS Coding is a step to implement the design of the software system. A program converts design intentions into an executable set of instruction, and achieves the specified goal of the software. The process is to write a program using suitable language. Our goal is to write a program that is easy to understand, test, modify and maintain.January 13, 2012 Made By Utpal Ray 2
  3. 3. CODING STANDARDS AND GUIDELINES PROGRAMMING STANDARDS AND PROCEDURES If a program is given to three coders (programmers) to write, you willget three different programs, achieving successfully the desired result.So each must careful in their coding. It is like traffic rules: they are to be followed for your own and otherssafety, and also for the convenience of all who are on the road. Coding standards are three types, namely Universal, which is common irrespective of system. Domain specific, which depends on particular platform like Windows, Linix, Solaris, AIX, Embedded System etc. Organization Specific. which depends upon where you are developing code like, Oracle, SUN, HP, IBM, CISCO etc.January 13, 2012 Made By Utpal Ray 3
  4. 4. CODING STANDARDS AND GUIDELINES Programming style standards Variable names should be mnemonic, clear and simple e.g. “largest” rather than “x”. Expressions should be clear and simple e.g.: abs (x) <=0.01” rather than “(x<0.01) and (x> -0.01)” Indentation and format Indent bodies of structured statements at least 3 spaces Leave blank lines between logically related groups of statements, after the declarations, and between functions. Wrap long lines at no more than 80 chars and indent the continued statement further than the beginning of that statement. Clearly delineate subprograms (procedures, functions, modules)January 13, 2012 Made By Utpal Ray 4
  5. 5. CODING STANDARDS AND GUIDELINES COMMENTING Program should begin with a preface stating: 1. Programmers name 2. Date written 3. Course and assignment number 4. A summary or description of functionality i.e. what the program does and a brief description of how to run it. 6. Any special instructions on how to run it. 5. Any known assumptions built into the programJanuary 13, 2012 Made By Utpal Ray 5
  6. 6. CODING STANDARDS AND GUIDELINES Special Source Code Structure static char rcsid[] = "$Id$"; /* $Log$ */January 13, 2012 Made By Utpal Ray 6
  7. 7. CODING STANDARDS AND GUIDELINES Program Robustness:1. Do not check for equality among real numbers2. Prevent the abnormal termination of a program due to input error, check for the validity of the data when appropriate.3. Programs should not be built around a specific data set, programs, unless otherwise stated, should work correctly for any reasonable test data set.4. Do not use GOTO statements. January 13, 2012 Made By Utpal Ray 7
  8. 8. CODING STANDARDS AND GUIDELINES I/O Behavior: All I/O should use a proper format 1. All real number output should be formatted in decimal form unless scientific notation is appropriate. 2. All input from the keyboard should be preceded by a prompt telling the user what format is expected. For example “, Please input the date in mm/dd/yy format”. 3. Label all output valuesJanuary 13, 2012 Made By Utpal Ray 8
  9. 9. CODING STANDARDS AND GUIDELINES Modules and modular program structure Module coherence: each module should correspond to one subtask in the overall algorithm to solve the problem. Module independence: each module should be independent .i.e it should be self-contained and it should perform its task successfully without needing to know the inner working of the calling module. Procedures or functions should , in general occupy at most 50 lines. Use appropriate parameters rather than global variables. Avoid side effects. Hide any information from the caller that it does not need. The default should be to hide information unless the caller actually needs it.January 13, 2012 Made By Utpal Ray 9
  10. 10. CODING STANDARDS AND GUIDELINES Programming GuidelinesAll programs have three main factors that need to be handled properly tomake a program efficient and effective for which it is written.These three factors are: 1. Algorithms How the problem gets solved in the program? 2. Control Structure How the branching (if-then-else) and looping (while-true) should be organized? 2. Data structures How the data should be organized? (local vs. global, individual variable vs. group of variables – data structure)January 13, 2012 Made By Utpal Ray 10
  11. 11. CODING STANDARDS AND GUIDELINES Indentation Four spaces should be used as the unit of indentation. The exact construction of the indentation (spaces vs. tabs) is unspecified. Tabs must be set exactly every 8 spaces (not 4). Line Length Avoid lines longer than 80 characters, since theyre not handled well by many terminals and tools. Wrapping Lines When an expression will not fit on a single line, break it according to these general principles: Break after a comma. Break before an operator. Prefer higher-level breaks to lower-level breaks. Align the new line with the beginning of the expression at the same level on the previous line. If the above rules lead to confusing code or to code thats squished up against the right margin, just indent 8 spaces instead.January 13, 2012 Made By Utpal Ray 11
  12. 12. CODING STANDARDS AND GUIDELINES EXAMPLE of Bad INDENTATION //DONT USE THIS INDENTATION if ((condition1 && condition2) || (condition3 && condition4) ||!(condition5 && condition6)) { doSomethingAboutIt(); }January 13, 2012 Made By Utpal Ray 12
  13. 13. CODING STANDARDS AND GUIDELINES EXAMPLE of Good INDENTATION if ((condition1 && condition2) || (condition3 && condition4) ||!(condition5 && condition6)) { doSomethingAboutIt(); } ORif ((condition1 && condition2) || (condition3 && condition4) ||!(condition5 && condition6)) { doSomethingAboutIt();}January 13, 2012 Made By Utpal Ray 13
  14. 14. CODING STANDARDS AND GUIDELINES Three acceptable ways to format ternary expressions: alpha = (aLongBooleanExpression) ? beta : gamma; alpha = (aLongBooleanExpression) ? Beta : gamma; alpha = (aLongBooleanExpression) ? Beta : gamma;January 13, 2012 Made By Utpal Ray 14
  15. 15. CODING STANDARDS AND GUIDELINES BLOCK COMMENT EXAMPLE/** Here is a block comment.*/Block comments can start with /*-, which is recognized by indent(1) asthe beginning of a block comment that should not be reformatted.Example:* Here is a block comment with some very special/*-** formatting that I want indent(1) to ignore.* one* two* three*/A block comment should be preceded by a blank line to set itapart from the rest of the code.January 13, 2012 Made By Utpal Ray 15
  16. 16. CODING STANDARDS AND GUIDELINES Perfect Declarations FormattingHow many Declaration Per Line?One declaration per line is recommended since it encouragescommenting.In other words,int level; // indentation levelint size; // size of tableis preferred overint level, size;Do not put different types on the same line. Example: int foo, fooarray[]; //WRONG!January 13, 2012 Made By Utpal Ray 16
  17. 17. CODING STANDARDS AND GUIDELINES Single Line Statements Each line should contain at most one statement. Example:argv++; // Correctargc--; // Correctargv++; argc--; // AVOID!January 13, 2012 Made By Utpal Ray 17
  18. 18. CODING STANDARDS AND GUIDELINES The correct form of if, if-else, if else-if else Statements if (condition) { if (condition) { statements; statements; } else { } statements; } if (condition) { statements; } else if (condition) { } else { statements; }January 13, 2012 Made By Utpal Ray 18
  19. 19. CODING STANDARDS AND GUIDELINES The correct form of for Statements for (initialization; condition; update) {When using the comma statements;operator in the }initialization or updateclause of a forstatement, avoid thecomplexity of usingmore than threevariables. for (initialization; condition; update);January 13, 2012 Made By Utpal Ray 19
  20. 20. CODING STANDARDS AND GUIDELINES The correct form of do-while Statements do { statements; } while (condition);January 13, 2012 Made By Utpal Ray 20
  21. 21. CODING STANDARDS AND GUIDELINES switch (condition) {The correct form ofswitch Statements case ABC: statements; /* falls through */ case DEF: statements; break; case XYZ: statements; break; default: statements; break; }January 13, 2012 Made By Utpal Ray 21