Coding standards 101

Without coding standards, your team's code will slide into chaos. But where should the standards come from? How extensive should they be? And, more importantly, who should enforce them?

Coding standards increase productivity in the development process, make code easier to maintain, and keep code from being tied to one person or team. But where should standards come from? How extensive should they be, and more importantly, who should enforce them? Here are the short answers to these questions.

Who dictates the coding standards?
You can choose from many industry coding standards. Some companies—Microsoft and Sun Microsystems, for example—offer guidelines. Some coding standards, such as Hungarian Notation, are the product of one person’s labor (in this case, Dr. Charles Simonyi, a Hungarian software developer at Microsoft).

Certain coding standards are language specific. Standards for Visual Basic dictate that you should prefix all textboxes with ”txt” and all buttons with ”cmd.” Sun Microsystems offers coding standards for Java.

Once you’ve settled on a coding standard, take the language-specific suggestions and add your own. For example, you might decree that variable and function names should be descriptive, or that the behavior of particular functions must be clearly commented. (My mantra is “Document, document, document: every class, every method, every few lines of code.”)

How extensive should coding standards be?
While it’s important to have coding standards, it’s equally important not to have superfluous coding standards. Imagine, if you will, a standard that dictates that the first character of a variable name must indicate the application in which it resides, the second character must indicate the module, and the third character must indicate the data type. Excessive coding standards such as this can impede the creative process and become a hindrance to development.

You should not have more coding standards than will fit on one or two pages. It makes it very hard to name your variable if you have to consult a chart to do so.

Who enforces coding standards?
I’ve worked in many software shops where coding standards existed, but no one enforced them. The result: The coding standards were about as effective as the “So, come here often?” pickup line.

The first step in enforcing coding standards is to keep those standards easily accessible. Ideally, you should have a laminated cheat sheet pinned up in each developer’s cube.

During peer code review sessions, someone’s job must include being code standards bad cop. However, automated tools offer another option to managers looking to enforce standards.

Some of the best tools on the market are made by Parasoft. Parasoft produces language-specific tools that automatically enforce more than 300 coding standards. The products include a RuleWizard feature, which allows developers to create and implement their own custom rules. I will review Parasoft products in future articles.

One of the standards enforced by Parasoft products requires that one-character variable names be used only for their conventional purpose, such as b for a byte, c for a char, f for a float. If this standard is violated, the output would look like this:
package examples.rules.naming;
 public class CVN {
     void method () {
         int b = 1;  // VIOLATION

Coding standards are a must for any development team. They ensure consistency and simplify the maintenance process with legible code. Your job is to make sure a standard is chosen, strictly followed, and always on the developer’s mind.

Editor's Picks