I suppose one of the strangest thing about standards is there are so many of them. For most programmers this is probably a familiar issue. Style and standards are pretty much dependent on the programmer or the company rather than the language, which defeats the purpose a wee bit. The only solid fundamental is that style and standards are used to make code more legible. How easy is your code to read?

Most of the debate revolves around indent style but other arguments arise about commenting, identifier naming, spacing etc. So lets have a look and see what we can find.

Indenting

Indenting really is at the heart of the debates about programming styles. Lets see what all the fuss is about.

K&R Style

int main(int argc, char *argv[]){
    while (x == y) {
        something();
    }
}

The K&R style, so-called because it was used in Kernighan and Ritchie’s book The C Programming Language, is commonly used in C. Thier examples used this style of code with eight spaces or a tab.

Allman Style

int main(int argc, char *argv[])
{
    while (x == y)
    {
        something();
    }
}

The Allman style is named after Eric Allman. The style puts the brace associated with a control statement on the next line, indented to the same level as the control statement. Statements within the braces are indented to the next level.

Whitesmiths Style

int main(int argc, char *argv[])
    {
    while (x == y)
        {
        something();
        }
    }

The Whitesmiths style is less common today compared to the prior two. This style puts the brace associated with a control statement on the next line, indented. Statements within the braces are indented to the same level as the braces.

GNU Style

int main(int argc, char *argv[])
  {
    while (x == y)
      {
        something ();
      }
  }

Like the Allman and Whitesmiths styles, GNU style puts braces on a line by themselves. The braces are indented by 2 spaces, and the contained code is indented by a further 2 spaces.

Commenting

Regardless of what language you are programming in, the majority of your code should include comments. Most languages support several different kinds of comments.

/* This is a C style comment */
 
// This is a C++ style comment
 
# this is a unix shell style comment

Remember that the whole point in comments is to help other people (or you) to understand what the code is meant to be doing. So consistency is they key. Keep your comments in the same format throughout your code. Sometimes more than one line of comments are needed. In this case we use block comments.

/*
 * Here is a Java style block comment.
 */
 
// C# and C++ style block
// comments tend to
// be across multiple lines
 
##################################################
###                                            ###
###    this comment is in a box                ###
###                                            ###
##################################################

Block comments are used to provide descriptions of files, methods, data structures and algorithms. They tend to involve a description of what the following bit of code does and other details (like parameters and return values). Each Class or function should be preceded by a comment block in most languages.

Naming Conventions

Another hot debate when it comes to coding style is naming conventions. Basically how are variables and functions named? There are several styles in use.

Underscore

int my_integer = 0;
function function_name();

It was only in the late 1960s that the widespread adoption of the ASCII character set made both lower case and the underscore character “_” universally available. Some languages, notably C, promptly adopted underscores as word separators.

Camel Case

int myInteger = 0;
function FunctionName();

Camel Case name are joined without spaces and are capitalized. One widely used Java coding style dictates that UpperCamelCase be used for classes, and lowerCamelCase be used for instances and methods. Some people and organizations use the term camel case only for lower camel case, and refer to upper camel case as Pascal Case.

Hungarian Notation

int nMyInteger = 0; //n indicates integer
function fnFunctionName(); //fn indicates function

Hungarian notation is a naming convention in which the name of a variable indicates its type or intended use.

Conclusion

So we have seen a range of different types of coding styles, but which one should you use? Well it really comes down to which language your are using and who you are working for. Some things to remember are

  • Always be consistent. Once you have chosen a coding style stick with it!
  • Is your code easy to read? Remember other people might need to understand what you have done aswell as yourself.
  • Is you code well documented? Remeber to always comment what you have done, especially if you think it is a complex or hard to understand piece of code.
  • Is it easy to debug? This ties in with being easy to read but remember if your code is all squashed together and your variable names are unclear you are going to spend a lot more time debugging when you encounter a bug.

Further Reading

If you want to read more into this subject then here are some recommended articles.

http://en.wikipedia.org/wiki/Coding_style
http://en.wikipedia.org/wiki/Commenting
http://en.wikipedia.org/wiki/Indent_style
http://en.wikipedia.org/wiki/Identifier_naming_convention

Related Posts

  • No Related Posts

More?

If you enjoyed this post, please consider promoting it.
  • Digg
  • del.icio.us
  • StumbleUpon
  • Design Float
  • Reddit
  • Mixx
  • Technorati

Remeber to subscribe to the feed and get future articles delivered to your feed reader.

If you want to discuss this tutorial or any other thoughts you have then you can do so over on our fourm.

Comments


No comments yet...

Leave a Comment

(required)

(required)