Coding Standards

 What are they?

Coding standards are rules that enforce a certain style of programming. Coding standards address how long source code lines can be, where blank lines should be, in general coding standards specify the format and layout of your source code. This choice is completely arbitrary and does not affect how the code executes. However, just because coding standards are arbitrary does not mean they are unnecessary. Without coding standards your project's development time and maintainability will suffer. Your project should choose or define a coding standard that best fits your needs.

Coding standards increase the readability of your source code, help others understand what the code is doing and minimize coding errors. When there are multiple developers on a software project it is important that they all agree on a common programming style since they will be reading and adding to each others source code. It is always difficult to read someone else's code and try to understand what it is doing. It is very frustrating if they have used a style of programming that is unfamiliar. Coding standards increase productivity and efficiency within your group.

If some developers in your team want to use auto format tools to change the format of the checked in code be careful that these tools do not cause the versioning control system to mark every line as changed when the code is checked in. This completely defeats the version control systems "diff" tool and must be avoided. Pick a coding standard and have every developer follow it.

The elements of good style.
Code appearance.

Line length.
Maximum line length should be agreed upon and used by all the programmers in a project. The primary reason for this is that all code will be printed out and distributed for code reviews. It is nearly impossible to hold a code review if excessive line length causes lines to wrap.
Usually the maximum line length is set to 120 characters. This line length printed out in landscape mode with a easily readable font will not cause any lines to wrap.

Indenting styles assist in identifying control flow and in making the code more readable, for example, this style:
if (hours <= = 24) {
    do something;
} else {
    do something;
}// if
is more readable than this:
if (hours <= = 24) 
{do something;} else

Spacing or whitespace is ignored by compilers so it can be used to make your code easier to read. See the coding standards links below for examples.

Variable naming.

Meaningful variable names.
Always use descriptive and meaningful variable names. Don't worry about the length. Meaningful variable names help other people reading your code understand what is going on. Example:
if (a <= = 24) 
What is a? This is confusing.
if (vacationHours <= = 24) 
See, better.
<li>Variable scope indicated by name.
Code is easier to read and understand if each variable indicates it's scope. One way to do this is to add prefixes to variable names, for example:
Scope = Class member or instance variable: m_hours
Scope = Class static variable: s_hours
Scope = Parameter passed in a method call: p_hours
Scope = local variable to a method: l_hours
If you do this then someone reading your code knows instantly the scope of the variable when it appears in the code. If you don't do this the reader has to do a search to see where the variable was defined in order to learn its scope.
Adding more information to your variable names such as data type or size will result in complex and confusing variable names and is not recommended.
For more information about coding standards please refer to the following.