Codewalk.com

Search For The One True Indentation Style

January 20th, 2008
By Sai Matam

In this anecdote, I describe how I got caught in the indentation style wars and how I dealt with the situation. I conclude by describing my take on code indentation and how to deal with different style schools.

Intern Gets A Coding Assignment

Long, long ago, I was working as a junior programmer for a Unix/C software shop. As the newest member of the team everybody was eager to coach me in the various aspects of software design and programming. The most zealous of them all were my team lead and my software development manager!

I was ecstatic that I was getting so much help from the team! Everything was cool and I was assigned my very first coding assignment. I was to develop a load generator test program. I was well versed with 'C' and 'Unix' and was fresh from college with all the enthusiasm. I was eager to apply all I know to code a 'real' program.

Problems With The Brace

The little coding project was going along smoothly when I was faced with a peculiar problem. Algorithm was not it. The issue was - you guessed it - my indentation style!

It was not my indentation style per se that was the problem but the fact that my team lead was from K&R brace school while the software development manager was from Allman brace style. Each of them wanted to review my code - daily - and insisted that I follow their favored indentation style!

 

 

K & R Brace Style

if (some > all) {
    doSomething();
    i++;
    ....
    ....
}

The K and R refer to Kernighan and Ritchie and they introduced the indentation style in their legendary book The C Programming Language. This indentation style keeps the first opening brace on the same line as control statement, indents the statements within the braces, and puts the closing brace on the same indentation level as the control statement (on a line of its own).

 

Allman Brace Style

if (some > all)
{
    doSomething();
    i++;
    ....
    ....
}

The Allman style is named after Eric Allman (of SendMail fame). It 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.

A Stupid Clever Solution

As the lowest leaf node in the org tree, I did not want to displease either my team lead or my manager. Luckily or unluckily for me, my team lead made his rounds in the morning and my manager made his rounds after lunch. So I devised an ingenious solution.

I started maintaining two sources one indented in K&R style for my lead and another set of source code indented in allman style for my manager! The K&R style code made it to the source control repository where as the Allman style code existing on my desktop. I expended considerable energy in indenting these two pieces of sources and even more energy in keeping them in sync.

Tool Support

In retrospect, I should have used a code formatting tool like C Beautifier to format my source code to suit various indentation needs. Modern day IDEs like Eclipse allow you to specify the code indentation style in great detail. These tools allow you to browse the code in your style without changing the whole world.

One True Grand Indentation Style

It is common for programming teams and software companies to have a prescribed coding style. It would be impossible, impractical and very expensive to insist that all code conform to the prescribed coding style.

This is because the code in a project is often from diverse sources - ranging from open source libraries to the code library from a different group within the company. We all realize the futility of reformatting open source library to conform to our coding style.

Instead of battling the diversity of code, why not use tools to provide you with a dynamic browing view conforming to your own indentation styles!. In modern IDEs one can click on a brace and instantly find the matching brace for that block!

Conclusion

Stop waging 'coding style wars' and trying to unify all the code under one true indentation style. Use the tool support in IDEs to browse code in your preferred format - on demand - rather than reformatting the sources. Use IDE support and make the most of syntax coloring, brace matching and other code browsing features to comprehend the code.

Please let me know your comments via e-mail: saimatam at yahoo dot com