Coding Standards
Coding Standards
Often, both contain a lot of similar points, and for companies that only program
in one language, both are often almost identical. However for companies which
develop software in a variety of languages, they often formulate their own coding
standard. For example, Mozilla Firefox is written in C++, XUL, XBL, and
JavaScript. Instead of inefficiently using the four separate coding standards of
each language, Mozilla has created its own coding standard.
Examples
In each coding standard, there are many points and rules. For example, the
coding standard for GNUs Not Unix (GNU) (an open source operating system) is
88 pages long. There are many points contained within such coding standards,
some of which I shall discuss here.
E.g. 1
Indentation
Indentation is discussed in all coding standards at some level. In some
languages, indentation is used by the compiler to identify the scope of functions.
In freeform languages, such as Java, it has no actual effect on the compilation of
code itself, and is just used to make the code more human readable, giving an
indication of scope without actually affecting the program directly.
Type 1
if (g < 17 && h < 22 || i < 60) { return true; } else {System.out.println
(incorrect) ; return false; }
Type 2
if (g < 17 && h < 22 || i < 60)
{
return true;
}
else
{
System.out.println(incorrect);
return false;
}
It is important to understand that type one and type two do exactly the same
thing, and since both pieces of code are written in is Java, the indentation is
ignored the compiler. The indentation is meant for the human user. It can be
seen when examining both pieces of code above that the type 2 code is much
more easier to read than the type one code. I have used a simple if-else loop do
demonstrate how even the simplest of code can become confusing when
indentation is not used effectively. The type two code will not only be easier to
read and understand for the person who is developing the code, but will make it
much easier for the developer maintaining the code to do their job correctly and
effectively.
E.g. 2
Commenting code
Commenting code is also an area which is addressed in the majority of coding
standards. Other languages, such a Tex, are self-documenting and are able to
automatically produce documentation describing themselves. In others, such as
COBOL the code itself is self commenting so there is less of an emphasis on such
areas. However, just commenting code is not enough, as some comments can
actually be of little use at all, and can often make the code more confusing.
Comments should clearly demonstrate the function of the code, not only to other
developers but also to you. Often when writing larger programs it can be easy to
lose track of what certain functions do, and it is easier to read a well written
comment that it is to trawl through lines of code trying to remember what the
function of the code you have already written.
Comments should also not be too long. If your code has followed a coding
standard, it should not be too difficult for developers to understand your code.
Long comments are often unnecessary and make your code look messy.
Comments should not be every line. Often lines such as loops and variable
assignments are very simple to understand and do not require comments. Most
coding standards recommend commenting the end of functions and objects,
rather than commenting every line. In functional languages, it is generally
recommended that a comment explaining a function is written before the code
itself.
The comments should also not be too complicated. The point of a comment is
help the reader understand the code. It is important to keep the comments short
and simple. For programs used on a global scale, it is recommended that they
are written in English, as this is the most commonly used language among
developers today.
According to the Ada coding standard in 2005,
Programmers should include comments whenever it is difficult to understand the code
without the comments.
This is very good advice, and the huge majority of other coding standards agree
with this statement.
E.g.3
Variable declarations.
This is another area which is almost always included in coding standards.
Variable declarations are often overlooked when programming but in larger
programs they can be the difference between understanding code and not.
Variable declarations should be long and demonstrate the function of the
variable which they store. In the majority of programming languages, variable
can have any name, excluding a few keywords that are reserved by the language
itself.
e.g.
{
return false;
}
These are only a few of the many details generally included in coding standards.
Others range for the more general avoid big functions to the more specific
Limit the line length to a maximum of 120 characters.
Conclusion
It is vital to remember that coding standards are only as useful as the method
which his used to enforce them. Even if a company has a well thought out,
comprehensive coding standard, if they are not enforced, then they are useless.
Programs such as StyleCop, can be used to check your code against the coding
standard of the Microsoft development centre. Other tools, such as the Enerjy
code analyzer can not only check your code complies with the Java coding
standard but can also detect some bugs in the system. This is however not a
fully comprehensive solution and it is ultimately the responsibility of the
programmer to comply with the standard.
References
GNU Coding standard
http://www.gnu.org/prep/standards/standards.html
Mozilla Firefox Coding Standards
https://developer.mozilla.org/En/Developer_Guide / Coding_Style
Enerjy Code Analyzer
http://www.enerjy.com/index.html
Brian W.Kernighan and Plaugher, The elements of programming style second
edition.
Rob pike and Brian W Kernighan, The Practice of programming