Coding Standard - ShajadulKarim/LevelUp GitHub Wiki
Purpose
Our goal is to create uniform coding guidelines among programmers so that programs can be easier to read, check and maintain. It helps different persons to easily understand the code scenario. When a project adheres to common standards many good things happen: *New people can easily understand the code standard and speed up their code quickly. *New programmers make fewer mistakes inconsistent environments. *Maintain is very easy for every new programmer. There are many coding standards formats that exist but now we are discussing some of them here.
1. Naming Convention:
The most important consideration in naming a variable, constant is that totally explains the exact meaning that the structure tells. Clean and meaningful names make the code more readable and minimize the complexity and less amount of comments needed. Naming Convention is any type. We discuss some of them which are easy to follow and every programmer is familiar with it.
*Camel Casing:
In this case, the programmer can declare their variable names with this camel style.
Example: Normal style is (int usernumber)
,
Camel Casing is (int UserNumber)
It is helpful because of its upper case word. Each word must have an upper case letter at the beginning. It also looks nice to see.
*Naming Constants:
In this convention constants and functions shall be nouns, with or without modifiers.
Example: Line, InputLine, NumInputLines
Constants variable name should be Capitalized: MAX_LINES(its easy to understand that this is not changed)
* Use of prefix of a data variable
This naming convention is widely used in C and Visual Basic language. If used, prefixes can vary from language to language and across applications. A list of the prefixes should be defined as part of the project-specific standard.
Example: char *apchFileSpec[10]
. And that tells us a lot more about the variable than a simple name FileSpec.
2. Indentation:
Proper and consistent indentation is important in producing easy-to-read and maintainable programs.
*Emphasize the body of a control statement such as a loop or select statement
*The body of a conditional statement
*new scope block
A minimum 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 indentation amount be consistently applied throughout the program. Tabs shall not be used for indentation purposes.
Example:
for ( int i = 0 ; i < number_of_employees ; ++i )
{
total_wages += employee [ i ] . wages ;
}
{
System.out.println ( “VIN: “ + vin ) ;
System.out.println ( “Make: “ + make ) ;
System.out.println ( “Model: “ + model ) ;
System.out.println ( “Year: “ + year ) ;
}
Inline Comments:
Inline comments explaining the functioning of the subroutine or key aspects of the algorithm shall be frequently used. It helps us to understand the line or understand the specific function works.
Example:
while (!$done)
{
// We don't want an infinite loop here.
$done = true;
}
Use of Braces:
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
for (int j = 0 ; j < max_iterations ; ++j) { /* Some work is done here. */ }
the Kernighan and Ritchie style:
for ( int j = 0 ; j < max_iterations ; ++j ) { /* Some work is done here. */ }
Spacing:
The proper use of spaces within a line of code can enhance readability. Good rules of thumb are as follows: • A keyword followed by a parenthesis should be separated by a space. • A blank space should appear after each comma in an argument list. • All binary operators except “.” should be separated from their operands by spaces. Blank spaces should never separate unary operators such as unary minus, increment (“++”), and decrement (“—“) from their operands. • Casts should be made followed by a blank space.
Example:
Bad:
cost=price+(price*sales_tax);
fprintf(stdout ,“The total cost is %5.2f\n”,cost);
Better:
cost = price + ( price * sales_tax );
fprintf (stdout, “The total cost is %5.2f\n”, cost) ;