Coding Standards



Coding Standards
A comprehensive coding standard encompases all aspects of code construction. While developer should prudently implement a standard, it should be adhered to whenever practical. Completed source code should reflect a harmonized style, as  if a single developer wrote the code in one session. At the inception of a software project, establishing a coding standard to ensure that all developers on the project are working in concert. When the software project incoporates existing source code, or when performing maintenance on an existing software system, the coding standard should state how to deal with the existing code base.

Identation

proper and consistent identation is important in producing easy to read and maintainable programs. Identation should be used to:

-->Emphasize the body of a control statement such as loop or a select-statement
-->Emphasize the body of a conditional statement
-->Emphasize a new scope block

A minimun of 3 spaces shall be used to indent. Generally, indenting by three or four spaces is considered to be adequate. once the programmer chooses the number of spaces to indent by, then it is important that this identation amount be consistently applied throughout the program.

Tabs tabs must not be used for identation purposes
-----------------------------------------------------------------------------------
Inline comments
Inline comments explaining the functioning of the subroutine or key aspects of the algorithm shall be frequently used

Structured Programming
Structured (or modular) programming techniques shall be used. GO TO statements shall not be used as they lead to "spaghetti" code, which is hard to read and maintain. Sometimes it is necessary use GO TO because it can make code more faster and brings down Cyclomatic Compexity Module, but it is necessary to educate Developers on good practices about using it.

Classes, Procedures, functions, and Methods
Keep proceduresm functions, and methods reasonably sized. This depends upon the language being used. For guidance on how large to make the software modules and methods, see Code Guidelines Section.

A good rule of thumb for module length is to constraint each module to ine function or action (i.e. each module should obly do one "thing").

If a module grows too large, it is usually because the programmer ir trying to perform too many actions at one time

The name of the classes, subroutines, functions, and methods shall have verbs in them. That is the names shall specify an action, e.g. "get_name", "calculate_tax"

Source Files
The name of the source file or script must represent its function. All of the procedures in a file should have a common purpose.

Variable Names
Variables should have a mnemonic or meaningful names that helps any programmer to understand the intent of its use.

Variables must be initialized prior of its first use

Varibles should be declared in languages where declaration of variables is optional and they shall use specific data types whenever possible; in other words if a variable is going to be used only to store numbers there is no reason to declare it as a "Variant" or "Object".



Braces in coding standards
  In some languages, braces are used to delimit the bodies of conditional statements, control constructs, and blocks of scope. Programmers shall use either of the following bracing styles.
Example:
for(int i = 0 ; j < 100 ; i++)
{
 /* Add code here. */
}
or the Kernigham and Ritchie style
for ( int i = 0 ; i < 100 ; i++ ) {
 /* Add code here. */
}
Whichever style is used, be sure to be consistent throughout the code. When editing code written by another author, adopt the style of bracing used.

Braces must be used even when there is only one statement in the control block


Compiler Warnings

Compiler warnings normally do not stop the compilation process. However, compiler errors do stop the compilation process, forcing the developer to fix the problem and recompile.

Compiler and linker warnings shall be treated as errors and fixed. Even though the program will continue to compile in the presence of warnings, they often indicate problems which may affect the behavior, reliability and portability of the code.

Some compilers have options to supress or enhance compile-time warning messages. Developers must choolse the options which fully enable the compiler's code-checking features.

Comentarios

Entradas populares