Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have read the Wikipedia article on Indent Styles, but I still don't understand. What is the difference between K&R and 1TBS?

share|improve this question
    
I read somewhere that the style in K&R was governed by space considerations - i.e. to reduce the vertical space the code took up in the book. –  ChrisF Aug 9 '11 at 14:14
    
@ChrisF it also reduces vertical space on the screen. When we had 80 col x 25 line terminals it was worth it! –  Martin Beckett Aug 9 '11 at 15:37
    
Apple's "goto fail" is a great example of a serious bug that would surely have been prevented by using 1TBS: imperialviolet.org/2014/02/22/applebug.html –  Jim Hankers Apr 9 at 5:31
add comment

2 Answers

up vote 27 down vote accepted

The biggest difference between K&R and the One True Brace Style (1TBS) is that in the 1TBS, all if, else, while, and for statements have opening and closing braces, even if they aren't necessary. The purpose is to make it easy to insert new statements and know exactly how they will be grouped.

As an example:

K&R:

int i;
for (i = 0; i < 10; i++)
  printf("Hi.");

1TBS:

int i;
for (i = 0; i < 10; i++) {
  printf("Hi");
}
share|improve this answer
    
Between which styles? –  Billy ONeal Aug 8 '11 at 22:25
1  
Sorry. I missed that the title and question are between two different styles. Let me clarify my post. –  Thomas Owens Aug 8 '11 at 22:57
    
Thanks, and sorry on my part :( –  GavinR Aug 9 '11 at 0:03
add comment

K&R is like this:

if (x) 
    a();
else {
    b();
    c();
}

That is: braces used only where needed, opening brace on same line as controlling statement, closing brace on its own line.

The "1TBS" turns a single controlled statement into a compound statement by enclosing it in braces:

if (x) {
    a();
}
else {
    b();
    c();
}

Allman style goes a bit further than 1TBS, and forces vertical spacing by placing the opening brace on a line by itself as well:

if (x) 
{
    a();
}
else 
{
    b();
    c();
}

Edit:

I'm still trying to figure out exactly how it qualifies as "arrogant" to say "Dennis Ritchie was an extremely smart guy who not only invented a good language, but also came up with a really good brace style for it."

For those who insist that it's being arrogant anyway, here's a little challenge: go to Sourceforge, Github (etc.) and pick out projects using the K&R brace style. Go through their records of bugs and commits, and try to find a single bug that was caused by the brace style they used.

If you don't want to do that much work, try doing a simple statistical analysis. Compare projects using different brace styles, and see if you can show "bimodality" -- a statistically significant difference in bug counts (severity, etc.) that correlates with bracing style.

I did both of these a few years ago, and couldn't find a single bug that I could attribute to bracing styles, nor could I find anything approaching a statistically significant correlation between the two. On average, those using K&R bracing had slightly fewer bugs, but the difference was much too small to qualify as statistically significant.

Since it was brought up, I'll comment on the situation with multi-statement macros. A macro that includes multiple statements but doesn't surround them with braces itself, has a bug. My job is not to write code that covers up that bug. Quite the contrary, my job is to find and eradicate that bug as quickly as possible.

Writing code in the hope of its covering up bugs so they remain undiagnosed and un-fixed is downright evil. Call that arrogant if you like, but I do not see this as even close to negotiable. Bugs should be found and fixed, not covered up. The longer the exist, the more likely it is that they will become much more difficult and expensive to fix.

share|improve this answer
1  
Deleted all the comments as they had descended in to bickering and noise. If you have a valid point then post it as an answer. If you want a discussion take it to chat –  ChrisF Aug 9 '11 at 15:17
5  
doesn't the 1tbs put the } and else on one line ? Saving vertical space while preserving the wondrous beautiful symmetry is the point ! –  Martin Beckett Aug 9 '11 at 16:00
    
@Martin: Different people's ideas of 1TBS vary on that point (which is to say that yes, the '1' is something of a misnomer, because when you get down to it, it's a family of minor variations). –  Jerry Coffin Aug 9 '11 at 17:07
2  
@Jerry - well any good holy war needs a few schisms ;-) –  Martin Beckett Aug 9 '11 at 17:09
    
goto fail; goto fail; –  Jamie Pate Apr 30 at 17:58
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.