09 coding standards_n_guidelines

21
CODING STANDARDS AND GUIDELINES 6/6/22 Made By Utpal Ray 1 CODING STANDARDS AND GUIDELINES Let’s Discuss The IDEAL Coding Attitude

Upload: university-of-computer-science-and-technology

Post on 03-Sep-2014

788 views

Category:

Education


2 download

DESCRIPTION

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

TRANSCRIPT

Page 1: 09 coding standards_n_guidelines

CODING STANDARDS AND GUIDELINES

April 7, 2023 Made By Utpal Ray 1

CODING STANDARDS AND

GUIDELINES

Let’s Discuss The

IDEAL Coding Attitude

Page 2: 09 coding standards_n_guidelines

CODING STANDARDS AND GUIDELINES

April 7, 2023 Made By Utpal Ray 2

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.

Page 3: 09 coding standards_n_guidelines

CODING STANDARDS AND GUIDELINES

April 7, 2023 Made By Utpal Ray 3

PROGRAMMING STANDARDS AND PROCEDURES

If a program is given to three coders (programmers) to write, you will get 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 others safety, 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.

Page 4: 09 coding standards_n_guidelines

CODING STANDARDS AND GUIDELINES

April 7, 2023 Made By Utpal Ray 4

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 spacesLeave 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)

Page 5: 09 coding standards_n_guidelines

CODING STANDARDS AND GUIDELINES

April 7, 2023 Made By Utpal Ray 5

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.

5.      Any known assumptions built into the program

6.      Any special instructions on how to run it.

COMMENTING

Page 6: 09 coding standards_n_guidelines

CODING STANDARDS AND GUIDELINES

April 7, 2023 Made By Utpal Ray 6

Special Source Code Structure

static char rcsid[] = "$Id$";

/*$Log$*/

Page 7: 09 coding standards_n_guidelines

CODING STANDARDS AND GUIDELINES

April 7, 2023 Made By Utpal Ray 7

Program Robustness:

1.      Do not check for equality among real numbers

2.      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.

Page 8: 09 coding standards_n_guidelines

CODING STANDARDS AND GUIDELINES

April 7, 2023 Made By Utpal Ray 8

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 values

 

Page 9: 09 coding standards_n_guidelines

CODING STANDARDS AND GUIDELINES

April 7, 2023 Made By Utpal Ray 9

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.

Page 10: 09 coding standards_n_guidelines

CODING STANDARDS AND GUIDELINES

April 7, 2023 Made By Utpal Ray 10

Programming Guidelines

All programs have three main factors that need to be handled properly to make a program efficient and effective for which it is written.These three factors are:

1. AlgorithmsHow the problem gets solved in the program?

2. Control StructureHow the branching (if-then-else) and looping (while-true) should be organized?

3. Data structuresHow the data should be organized? (local vs. global, individual variable vs. group of variables – data structure)

Page 11: 09 coding standards_n_guidelines

CODING STANDARDS AND GUIDELINES

April 7, 2023 Made By Utpal Ray 11

IndentationFour 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 LengthAvoid lines longer than 80 characters, since they're not handled well by many terminals and tools.

Wrapping LinesWhen 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 that's squished up against the right margin, just indent 8 spaces instead.

Page 12: 09 coding standards_n_guidelines

CODING STANDARDS AND GUIDELINES

April 7, 2023 Made By Utpal Ray 12

//DON'T USE THIS INDENTATION

if ((condition1 && condition2) || (condition3 && condition4) ||!(condition5 && condition6)) { doSomethingAboutIt(); }

EXAMPLE of Bad INDENTATION

Page 13: 09 coding standards_n_guidelines

CODING STANDARDS AND GUIDELINES

April 7, 2023 Made By Utpal Ray 13

EXAMPLE of Good INDENTATION

if ((condition1 && condition2) || (condition3 && condition4) ||!(condition5 && condition6)) { doSomethingAboutIt();}

OR

if ((condition1 && condition2) || (condition3 && condition4) ||!(condition5 && condition6)) { doSomethingAboutIt();}

Page 14: 09 coding standards_n_guidelines

CODING STANDARDS AND GUIDELINES

April 7, 2023 Made By Utpal Ray 14

Three acceptable ways to format ternary expressions:

alpha = (aLongBooleanExpression) ? beta : gamma;

alpha = (aLongBooleanExpression) ? Beta : gamma;

alpha = (aLongBooleanExpression) ? Beta : gamma;

Page 15: 09 coding standards_n_guidelines

CODING STANDARDS AND GUIDELINES

April 7, 2023 Made By Utpal Ray 15

A block comment should be preceded by a blank line to set it apart from the rest of the code.

/*

* Here is a block comment. */ Block comments can start with /*-, which is recognized by indent(1) as the 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

*/

BLOCK COMMENT EXAMPLE

Page 16: 09 coding standards_n_guidelines

CODING STANDARDS AND GUIDELINES

April 7, 2023 Made By Utpal Ray 16

Perfect Declarations Formatting

How many Declaration Per Line?

One declaration per line is recommended since it encourages commenting.

In other words,

int level; // indentation level

int size; // size of table is preferred over

int level, size;

Do not put different types on the same line. Example:

int foo, fooarray[]; //WRONG!

Page 17: 09 coding standards_n_guidelines

CODING STANDARDS AND GUIDELINES

April 7, 2023 Made By Utpal Ray 17

Single Line Statements

Each line should contain at most one statement. Example:

argv++; // Correct

argc--; // Correct

argv++; argc--; // AVOID!

Page 18: 09 coding standards_n_guidelines

CODING STANDARDS AND GUIDELINES

April 7, 2023 Made By Utpal Ray 18

The correct form of if, if-else, if else-if else Statements

if (condition) { statements;}

if (condition) { statements;} else { statements;}

 

if (condition) { statements;} else if (condition) {} else { statements;}

Page 19: 09 coding standards_n_guidelines

CODING STANDARDS AND GUIDELINES

April 7, 2023 Made By Utpal Ray 19

The correct form of for Statements

for (initialization; condition; update) { statements;}

When using the comma operator in the initialization or update clause of a for statement, avoid the complexity of using more than three variables.

for (initialization; condition; update);

Page 20: 09 coding standards_n_guidelines

CODING STANDARDS AND GUIDELINES

April 7, 2023 Made By Utpal Ray 20

The correct form of do-while Statements

do { statements;} while (condition);

Page 21: 09 coding standards_n_guidelines

CODING STANDARDS AND GUIDELINES

April 7, 2023 Made By Utpal Ray 21

The correct form of switch Statements

switch (condition) {

case ABC: statements; /* falls through */

case DEF: statements; break;

case XYZ: statements; break;

default: statements; break;}