Logo HeaderGraphic
"... A Yankee in the Land of The Long White Cloud, Aotearoa ..."

Curly Braces and Indentations 

Today’s entry is on some of the most inconsequential things… and yet, they are so important to so many of us, the Curly Brace and Indentations.

More specifically, the placement of curly braces in C#, C++, Java, and other C type languages in relation to the structure they define.  Which do you prefer

for ( i = 0 ; i < 10 ; i++){

	if (i < 5 ){
		//... do something
	}

}

or

for ( i = 0 ; i < 10 ; i++)
{

	if (i < 5 )
	{
		//... do something
	}

}

For myself, I choose the first option, but so many programmers I know prefer the second one.  I find it easier to associate the closing } with the matching structure, a quick scan up, and the first thing I encounter on the way up, is the parent structure.  I’m not so sure WHY it is so important to so many of us that it be done the “right” way (meaning, of course our way), but there is little doubt that it is.

Indentations are another subject over which holy war’s have been fought.  How many spaces should each indentation be?  2, 3, 4?

 

for ( i = 0 ; i < 10 ; i++){
  if (i < 5 ){
    //... do something
  }
}

for ( i = 0 ; i < 10 ; i++){
   if (i < 5 ){
      //... do something
   }
}
for ( i = 0 ; i < 10 ; i++){
    if (i < 5 ){
        //... do something
    }
}

I used to be a big believer in 4 spaces, then I found that 3 seemed better to me, and 2 was not enough.  And then one day I discovered that there was an easy way to get rid of this argument.  Have everyone in the project modify their settings to keep tabs as tabs, and each person could define them the way they liked.  If we all used Tabs instead of spaces, then each of us could see the code in the way we preferred just by specifying how big the tab space is in Visual Studio.

image

I had a client once say he didn’t believe it was acceptable to tell everyone to use tabs (and not use spaces) and while I understood his position (sometimes getting programmers to change their habits is painful), There were other things that we always insist our programmers do, and this one in the long run is pretty minor, allows each programmer to see the code in the way they like, regardless of what other people have as their settings, and once you get them to try it, I have never had a one who in the end found that she / he preferred it that way. 

I’ve had a couple of people fight me for a day or two, but in preparation for this blog, I contacted them and asked them which way they did it now 1 year and 5 years (respectively) later they both are with tabs and had to laugh with me, about “I’m not sure why I thought it was a big deal”.

Next time I’m looking for a light blog entry I’ll give you my thoughts on a line termination character:

Hint:  Programming languages shouldn’t “code for norm” and generally I dislike extra work, no matter how trivial, since it adds up.

Digg This
 
Posted on 31-Mar-09 by Matthew C. Hintzen
Bookmark this post with:        
Tags: