The Facts Behind the Code Indention Style War

About a week ago an anonymous visitor to Experiment Garden made the following comment:

"Please, please, PLEASE, for humanity's sake: use the Kernighan and Ritchie's indenting style!
Don't use braces for single line statements.
Much less, put them in their own lines!

Vertical space is precious. By committing those sins, you're making your code unnecessaringly [sic] long, which
means I must scroll down to read it completely, whereas it could've fitted in very few lines, if you take out all the fluff!!!"

This is just one more example of the great code indention style war that has plagued programmers for decades.

In most languages code indention isn't a requirement. Compilers leave a great deal of room for flexibility in how code is formatted. As long as you use braces and semicolons to separate blocks and statements the compiler will process the code. Code indention is purely for the convenience of programmers and others who need to read and debug code. Indention is designed to help blocks and logic levels to stand out.


The Allman Brace Style

Personally, I favor the Allman Brace style, which is credited to Eric Allman, the programmer who created sendmail. In the Allman style code brace blocks are placed one line below their respective control structures, at the same indentation level. The code inside the braces is indented one level.

if(condition_variable==condition1)
{
//Some code.
condition_variable=condition2;
}
else if(condition_variable==condition2)
{
//Some code.
condition_variable=condition1;
}
else
{
//Some code
condition_variable=false;
}

In my opinion, the Allman brace style is easier to read because it separates the code into solid blocks that can quickly be distinguished from the rest of the code. The conditional control structures associated with each block are one line above.

One major advantage to Allman is that it allows you to comment out entire blocks of code with very little work. In addition you can have multiple control statements for testing purposes and easily comment out one to try another. For example:

if(condition_variable==condition1)
/*if(condition_variable==condition2)*/
{
//Alternate control structures being tried.
condition_variable=condition2;
}


if(condition_variable==condition2)
/*{
//Block temporarily commented out.
condition_variable=condition1;
}*/

According to some sources the Allman brace style is most popular with C++ and C# programmers.

The Kernigan and Richie Brace Style

The Kernigan and Richie style, typically abbreviated to K&R style, is clearly the indentation style of choice for my anonymous commenter. The Kernigan and Richie style was introduced in the popular book "The C Programming Language" by Kernigan and Richie.

The difference between K&R style and Allman style is that K&R style places the opening brace on the same line as the control statement.

if(condition_variable==condition1) {
//Some code.
condition_variable=condition2;
} else if(condition_variable==condition2) {
//Some code.
condition_variable=condition1;
} else {
//Some code
condition_variable=false;
}

In my personal opinion, K&R style is considerably harder to read, because there is no vertical space between the code in the block and the control structures associated with it. The code tends to run together, making it harder to locate the start and end of code blocks.

According to my anonymous commenter, however, "vertical space is precious" and the extra "fluff" forces the person reading the code to scroll down to read it. Interestingly the K&R style was especially popular in the early days of C programming because computer terminals had limited resolution, and vertical space was very precious.

These days high definition monitors are common, with the typical resolution being 1024x768. In most cases vertical space is no longer a problem. However, there are still people who like the K&R style, probably because they began programming back when K&R was the dominant coding style.

Many books that teach beginning programming also make use of K&R style. This is because in a book vertical space is still precious. There is a limited amount of vertical space on each page, and if the Allman style was used extra pages would have to be added to make room for all the extra lines.

In a real world program, though, or a blog or website vertical space is not so precious. You can always scroll down in the browser or IDE to see more code.

The War Behind Indentation Style

As I did research for this post and examined blog posts and forum discussions across the web it became clear to me that there is no solid agreement on any one indentation style in particular. This leads to interesting dilemmas, especially in the work environment.

In his article "Search for the One True Indentation Style" Sai Matam describes what happened when two software project managers both wanted to review his code daily. The only problem was that one expected the code to be in K&R style, while the other demanded that he use Allman style. Sai Matam's solution was to write two copies of the code, one in K&R style, the other in Allman style. The extra time involved in syncing the two copies must have been immense.

Further complicating the indentation war is the continued conflict between those who indent their code using spaces and those who indent using tabs. For example, the EmacsWiki goes into great detail to explain why tabs are evil. Meanwhile article writer Xah Lee makes his argument to explain why tabs are proper, and spaces are improper.

Personally I prefer spaces over tabs because tabs can vary from system to system and IDE to IDE. One person may configure tabs to be the equivalent of four spaces, whereas I have tabs set up to be two spaces. This changes code alignment, especially if you use formatting to line up variable assignments. Suddenly what was once a very readable technique for assigning values to a group of variable becomes an unreadable mess.

However, others make the argument that tabs are specifically designed for formatting while spaces are like a work around. According to Xah Lee, in his article mentioned above, tabs are proper because they give formatting options. If someone doesn't like the way the code looks they can just change the tab space settings and all indention over the whole code base will be adjusted. This would be considerably more difficult to do if spaces were used rather than tabs.

Conclusion

There are obviously advantages and disadvantages to each style preference. Personally I like Allman's style with spaces for indentation. These days, though, any decent IDE can reformat all your code for you. If someone viewing it doesn't like Allman, all they have to do is refactor the code in K&R.

If IDE's were really intelligent they could be configured to auto format all code that enters the IDE. Then programmers would only have to set their preferred coding style preference once, and from then on they would see all code in that style. If HTML code tags had a language attribute and more complicated functionality then browsers could also be made to reformat code automatically. In time the war between advocates of Allman's style and K&R could become a thing of the past, with each side viewing code in their preferred style, blissfully unaffected by users of the "wrong" coding style.

Maybe it will happen in the future. In the meantime, I'll just walk the middle path. On this blog I will use Allman's in some articles, and K&R in others. It will all depend on how long the code samples are and how I want them to fit in the vertical space.

What is your favorite code indention style and why do you use it over other styles?

20 comments:

  1. The problem with the IDE auto formatting the source is that, if your diff tool is not configured correctly, the continual changes in formatting will overwhelm the diff between two version of the file in your version control system.

    Of course if we all used the same diff tools with the same settings then it would work. But the correct settings for diff tools is just another war waiting to happen :(

    ReplyDelete
  2. Language workbenches would fix this. Store the code as a data structure, and render it in the IDE as the user desires.

    ReplyDelete
  3. That is a very good point. Diff tools would have to be considerably smarter and able to detect differences in code and comments without respect to differences in formatting.

    It would be very difficult to code such a diff tool.

    ReplyDelete
  4. That would be a very good solution, if formatting of code was separate from code content, as in XML, HTML, or other markup languages. The only problem is that I doubt anyone would want to go to all the effort to write the formatting markup. It would inflate code base as well.

    ReplyDelete
  5. Not really. You would configure the IDE, similarly to how you can tune Eclipse to your tastes. You don't have to annotate the source code at all. You just type, and the IDE parses, stores it as data structures, and renders it again when you come back to the code - in your style.

    Smalltalk environments do this to a certain extent, though not really with the multi-user view in mind I think.

    ReplyDelete
  6. Okay, I see what you are saying. All the formatting/markup information would be stored separately, but it would be generated automatically by the IDE. You would never even have to see it.

    That would definitely work. The IDE would obviously have to translate the code to plain text for the compiler, but that wouldn't be difficult at all.

    ReplyDelete
  7. I work with both styles depending on project I work on. After a while it just doesn't seem to matter. However: Vertical space IS a problem. In my IDE I get to see 35 lines at once. If my class is no more than 35 lines long - I see everything at once.

    The main problem with vertical space IMO are not the braces. It's lines that don't do much. For example import statements in Java, pro forma comments and attribute accessors. Some examples are here http://www.jeromemueller.ch/archives/4/thoughts-about-code-readability

    ReplyDelete
  8. Smalltalk environments generally have an auto-format. There was some discussion at a Camp Smalltalk of abandoning TextSource -> Bytecode and just JIT code from the AST. This would allow one to have the source code displayed with individual programmer's preferences. Handling comments would be harder, but I think could be done if comments were formatted according to Token Ordering instead of syntax-tree ordering. (Formatter would have to pay attention to indent depth, but that would be it.) The Smalltalk Refactoring Browser's treatment of comments suffers because it tries to use syntax-tree ordering instead of token ordering.

    ReplyDelete
  9. I love the idea of JIT code. I think it has real potential. However, getting people to adopt a new technique like that would probably just lead to another format war, just with a different focus.

    As long as everyone used a compatible IDE it would work though.

    ReplyDelete
  10. Thanks for sharing your article.

    I do agree that there are cases when boilerplate code, or code that doesn't do much could be compressed. In situations where serious logic and important processing is going on though it helps to have plenty of whitespace and brace structure to make it clearer.

    In a case where something is going to be longer than 35 lines anyway, I don't want to cram it all together and have it still longer than 35 lines. So if a little bit of compression can get it to fit in that 35 lines, fine. But a longer, logic heavy function that is going to be longer than 35 lines even in K&R I'll code in Allman to keep it clean and easy to read later if I need to debug it.

    ReplyDelete
  11. I totally agree. Readability of the code is more important than compressing it to as few lines as possible. Even consistency is more important. So if all the project is in Allman, I wouldn't use K&R.

    I do believe though that after using both styles for years, you can easily get your eyes trained and that there is no difference in how quick you can understand code written in either style.

    The only reason why one style is easier to read, is the amount of practice one has had reading it.

    ReplyDelete
  12. I am a K&R 2 space kinda guy. The more code I can see on one screen without scrolling, the faster I am going to be able to understand what is happening. To me, Allman style just seems wasteful to look at. I could not agree more that IDE's should be able to transpose one format for the other. This debate is worst in teams. I find it difficult to code in alternate styles and I'm sure plenty of other people do as well.

    ReplyDelete
  13. The simple solution to the diff tool is to have the IDE auto format the code to an agreed shared format on save.

    ReplyDelete
  14. That would definitely work. Then when the diff tool displayed the code and its differences it would reformat the two saved copies of the code to whatever preferred format style the user wants to see.

    The user of the diff tool could synch the two files and resave, with the code being saved in the common format again.

    ReplyDelete
  15. Since I develop with code in website design, I have a little experience but not that much. The anonymous poster just sounds a little over-dramatic for how someone else chooses to do their code. I mean, they don't have to come to this blog if they don't want to.

    ReplyDelete
  16. Very true. Unfortunately, a great many people have turned coding style preference into a "holy war" of sorts. Many get extremely vocal about why their preference is better.

    I wrote this post to explain the two major styles, why I prefer Allman's and how I think that the situation could be remedied by the use of intelligent development tools and/or browsers that automatically reformat the code.

    ReplyDelete
  17. People could always get a 30 inch monitor and turn the font size down, if it matters THAT much.

    (I forgot to reply to you, could you please delete the comment below? Thank you.)

    ReplyDelete
  18. if(condition_variable==condition2)
    /*{
    //Block temporarily commented out.
    condition_variable=condition1;
    }*/

    This is probably a bad idea.

    ReplyDelete
  19. I far prefer Allman, when I started coding I used K&R, but now i hate it, and the sight of it makes my eyes hurt. If your fitting more code in a smaller space it means you are pressing your code together, which makes it harder to read. I also use 4 spaces, tabs are really awful, especially when switching IDEs. Also it is far more logical to have the brace on its own line, as it belongs to the closing brace, not the header.

    ReplyDelete
  20. At the risk of sounding like a newbie or an old-school paper guy, I see one advantage in Allman's:

    In a complex case, that style allows to draw a vertical line between the opening and closing braces based on the logic I am implementing. If such as vertical line has to cross another such line for my logic to flow as I intend to, this immediately flags a coding error.

    And yes, I prefer Allman's as it is clearer. Clarity should come first, above other considerations. If clarity is sacrificed over space in a book, it also makes the book example a bit harder to read.

    Nobody would use two left shifts instead of explicitly multiplying by 4. Clarity of programmer intent sacrificed because maybe the (poorly-designed) compiler MIGHT produce more efficient code ? Not for me/

    ReplyDelete