Programming Style, Whitespace and Formatting

You can see if someone is a good programmer not only by the technical solution he/she chooses, but also by the programming style he/she uses in their programs. One of the hallmarks of having a good programming style is consistency and clarity. If the style is constant it is much easier to read and understand. A good style is about making the program clear, understandable and readable as well as easily modifiable. Always choose the most easily understood technique for a problem, accept if performance is an issue. If you do this then the source code is much easier to read, thus easier to understand.

Formatting and White-space

If you use the right formatting and use white-space at the right places then it is much easier to read. (Too much text is a strain on the reader’s eyes.) You can use white-space everywhere you want in the source code, because it’s ignored by the compiler (but use it in a consistent manor.)

Indentations

The use of indentations in your source is a natural thing to do. If you don’t use it in the beginning you will after a while. It makes reading of the source code so much easier. For example you should use indentations on every code block:

	
        if( something)
	{
		//code block
		a = a + 1;
		if( anything)
		{
			//Another code block
			b = b + 1;
		}
	}

The indentation depth is a matter of personal choice; normally we use one tab for a code block, but it is really up to you. You can use tabs or spaces for your indentations, but you should be warned that the tab width can vary per editor. A well formatted program (with tabs) may look great in one editor, but in another editor it may not look OK (especially long lines of code.) That is why some people only use spaces, but it is up to you.

The use of braces

You can use the indentations where ever you want, but normally they follow after a brace (curly bracket.) For example we use the style as you can see above, but there are many people that use a different style. This is fine as long as everybody in the same project is using the same style of indentations and use of braces (curly brackets.)

Some examples of brace styles:


	if ( something )
	{
		//code
	}


	if ( something ) {
		//code
	}


	if ( something )
		{
			//code
		}

No correct use of whitespace

Maybe the following question popped up in your mind when you read this article: “So what if I don’t use the right indentations?”

Yes, nothing happens, but it is much harder to read or it can even confuse the reader. Take the following example:


	if ( something )
	a = a + b;
	b = b + 10;

If this block of code was in a large programming, then you may not notice that there are no braces (curly brackets) used. Due to misuse of indentations (in this case) you may think that both statements get executed when the “if statement” executes (which is not the case only a = a + b; is executed by the “if statement”.)

That is why we always use braces even if there is only one line after the “if statement” and we try to always use the right indentations. Same example, but now how we do it:


        if ( something )
	{
		a = a + b;
	}
	b = b + 10;

In Conclusion

Try to develop you own formatting style and stick with it (only change it when there are multiple programmers in one project, then you should discuss which formatting style everyone should use.) After a while you don’t even notice that you format your source code, so give it a go!

A last tip: normally after we have programmed a program and everything works as it should be, we go over the source one more time. We correct formatting (such as indentations, etc) and add comment lines where needed.

This entry was posted in General Programming. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed. Tweet This! Tweet This! or use to share this post with others.

There is currently one response to “Programming Style, Whitespace and Formatting”

Why not let us know what you think by adding your own comment!

  1. Darshana on July 9th, 2013:

    good one to understand