tags:

views:

207

answers:

7

I'm interested in where this variation of the pure block style stems from. When I say putting the else directly after the end of the if block I mean...

if ( foo ) {
  DoThis();
  DoThat();
} else {
  DoThing();
}

I noticed that the Google C++ Style Guidelines use this, but how often do you see other developers using it? Also, where does it stem from? Obviously someone decided to take code like this...

if ( foo ) {
  DoThis();
  DoThat();
}
else {
  DoThing();
}

and cut down the space, thinking it was more readable I suppose (I'm offering no opinion. I don't want a religious war. I just want to know where the style stems from.)

+1  A: 

Your question doesn't go far enough; why not:

if ( foo ) 
{
  DoThis();
  DoThat();
}
else 
{
  DoThing();
}

My answer is: vertical lines are valuable; often I have related functions next to each other in the source file I'm browsing, and any time I can see more without scrolling, I win.

wrang-wrang
+5  A: 

Personally, I hate the Google style of putting the opening brace on the same line but that's just me - it seems to go against ( no pun intended ) the idea of a block being a block.
I personally ( and it is a pure personal thing ) prefer

if ( foo )
{
   DoThis();
   DoThat();
}
else
{
   DoThing();
}
zebrabox
It's a personal opinion not dogma! If you prefer it then knock yourself out ;)
zebrabox
I don't think that's an answer to the question at all... Or are you saying Google does this **because** you don't like it? What have you done to them that you have to assume they just do it to annoy you?
sth
@sth I'm sure Google have better things to do with their time then design coding conventions specifically to annoy me. I wouldn't be so egotistical as to assume the world revolves around me. All power to them - it's just not my bag
zebrabox
@zebrabox: My point was just that "I like it differently" doesn't answer the question "Why does Google use this convention?".
sth
+3  A: 

We use your first style (sometimes called "line-saver mode") exclusively... It provides an obvious paring to the preceding if and helps reduce the vertical space used (seeing lots of code at once is valuable).

It stems from the K&R style, specifically the 1TBS variant.

jheddings
Seeing lots of code was a good argument 30 years ago when you could only get 15 lines on a moniter. Nowadays with 50+ lines visable at good font or 70+ with a small font this is not so valid a consideration (And a lot more if I turn my moniter sideways into portrait mode)).
Martin York
@Martin: it's all preference... I turn a 23" widescreen on its side at 1200x1900 and still want more ;) Using this style means I don't have to burn vertical real estate to see more information.
jheddings
+1  A: 

I find the first example easier to read. I use that form myself, unless the if block needs to be commented, in which case I do:

//Zombie killed, resurrect
if (zombie_killed()) {
    resurrect_zombie();
}

//Human killed, mourn
else {
    mourn_human();
}

I'm also a big advocate of saving vertical space. Not many people seem to understand that it is the most important kind of space there is. Unless you're working on a terminal 80 characters wide, you've got all the horizontal space you need. No amount of vertical space is ever enough*. Think 'scroll wheel'; almost all of them are vertically mounted. That is for a good reason.

*: Maybe more than you can see, or more than you can look at without straining your neck.

aib
"No amount of vertical space is ever enough*" - I dunno. I could rotate my monitor into portrait orientation if I wanted, and I don't want (24", 1920x1200). So I guess 1200px is enough for me. I've seen people programming with 2 or 3 monitors that size or bigger, though, all vertical.
Steve Jessop
I think portrait orientation would work better, yes, but I guess there's a problem with aesthetics. BTW that was a bit tongue-in-cheek: Of course 1200px could be enough for you. 1024 (home) and 1050 (work) are enough for me. But I'm guessing your IDE is set up so most of the toolbars are on the left and right sides? Your code area is roughly square?
aib
Yes, and task bar on the left too. Code window varies a bit, but if anything it's taller than it is wide even with the widescreen monitor. I like having files side by side too.
Steve Jessop
+1  A: 

I would tend to believe the google style leans towards what would be closer to a Java style coding preference as seen here

Steve Obbayi
A: 

Google style sacrifices readability for the sake of saving vertical space. Maybe someone thought that saving vertical space is more important. Maybe someone even had a good reason for such an extreme saving of vertical space in some specific environment. Who knows?

From the readability point of view, in my opinion, it is much more important to have vertical separation between the if condition and the branch code, as well as around else. The most readable version for this reason is

if (foo) 
{ /* Space can be used for comment */
  DoThis();  
  DoThat();
}
else 
{ /* Space can be used for comment */
  DoThing();
  DoAnotherThing();
}

The so-called K&R style is an intermediate variant between the two. I'd say that if you value vertical space so much, you better use K&R. It is less readable than the above, but the Google variant is simply ridiculous.

AndreyT
Edan Maor
A: 

It is called the K&R style. I would say that the advantage apart from the space savings is to disallow insertion of new code in "unsafe" places. Consider this example.

if ( foo ) {
  DoThis();
  DoThat();
}
// A statement here would be between the if and the else block. Error!
else {
  DoThing();
}

compared to the "safe" K&R-ish style

if ( foo ) {
  DoThis();
  DoThat();
  // A statement here would be in the if block
} else {
  // A statement here would be in the else block
  DoThing();
}

It is used at least in K&R, maybe even earlier.

sluukkonen