views:

1196

answers:

9

What advantage, if any, is provided by formatting C code as follows:

while(lock_file(lockdir)==0)
    {
    count++;
    if(count==20)
        {
        fprintf(stderr,"Can't lock dir %s\n",lockdir);
        exit(1);
        }
    sleep(3);
    }
if(rmdir(serverdir)!=0)
    {
    switch(errno)
        {
        case EEXIST:
            fprintf(stderr,"Server dir %s not empty\n",serverdir);
            break;
        default:
            fprintf(stderr,"Can't delete dir %s\n",serverdir);
        }
    exit(1);
    }
unlock_file(lockdir);

versus something more typical such as

while(lock_file(lockdir)==0) {
    count++;
    if(count==20) {
        fprintf(stderr,"Can't lock dir %s\n",lockdir);
        exit(1);
    }
    sleep(3);
}
if(rmdir(serverdir)!=0) {
    switch(errno) {
        case EEXIST:
            fprintf(stderr,"Server dir %s not empty\n",serverdir);
            break;
        default:
            fprintf(stderr,"Can't delete dir %s\n",serverdir);
    }
    exit(1);
}
unlock_file(lockdir);

I just find the top version difficult to read and to get the indenting level correct for statements outside of a long block, especially for longs blocks containing several nested blocks.

Only advantage I can see is just to be different and leave your fingerprints on code that you've written.

I notice vim formatting would have to be hand-rolled to handle the top case.

+9  A: 

Nothing. Indentation and other coding standards are a matter of preference.

cruizer
+5  A: 

Personal Preference I would have thought? I guess it has the code block in one vertical line so possibly easier to work out at a glance? Personally I prefer the brace to start directly under the previous line

KiwiBastard
+2  A: 

Its just another style--people code how they like to code, and that is one accepted style (though not my preferred). I don't think it has much of a disadvantage or advantage over the more common style in which brackets are not indented but the code within them is. Perhaps one could justify it by saying that it more clearly delimits code blocks.

Dark Shikari
+3  A: 

Code formatting is personal taste. As long as it is easy to read, it would pay for maintenance!

Prakash
+1  A: 

In order for this format to have "advantage", we really need some equivalent C code in another format to compare to!

Where I work, this indentation scheme is used in order to facilitate a home-grown folding editor mechanism.

Thus, I see nothing fundamentally wrong with this format - within certain rational limits, formatting is a matter of personal preference.

benefactual
+2  A: 

By following some formatting and commenting standards, first of all you show your respect to other people that will read and edit code written by you. If you don't accept rules and write somehow esoteric code the most probable result is that you will not be able communicate with other people (programmers) effectively. Code format is personal choice if software is written only by you and for you and nobody is expected to read it, but how many modern software is written only by one person ?

andreasmk2
I wish it were that simple. The problem is competing standards. Some, but not all, prefer the former (or rather, Ben's description of the former). Others prefer the latter. And both sides get religious about it.
Randolpho
+5  A: 

It looks pretty standard to me. The only personal change I'd make is aligning the curly-braces with the start of the previous line, rather than the start of the next line, but that's just a personal choice.

Anyway, the style of formatting you're looking at there is a standard one for C and C++, and is used because it makes the code easier to read, and in particular by looking at the level of indentation you can tell where you are with nested loops, conditionals, etc. E.g.:

if (x == 0) 
{
  if (y == 2)
  {
    if (z == 3)
    {
       do_something (x);
    }
  }
}

OK in that example it's pretty easy to see what's happening, but if you put a lot of code inside those if statements, it can sometimes be hard to tell where you are without consistent indentation.

In your example, have a look at the position of the exit(1) statement -- if it weren't indented like that, it would be hard to tell where this was. As it is, you can tell it's at the end of that big if statement.

Ben
+19  A: 

The top example is know as "Whitesmiths style". Wikipedia's entry on Indent Styles explains several styles along with their advantages and disadvantages.

brian d foy
I prefer Allman or GNU myself, as I always have a hard time reading the other types, with the function() { layout.
Peter C.
Tim Post
+7  A: 

The indentation you're seeing is Whitesmiths style. It's described in the first edition of Code Complete as "begin-end Block Boundaries". The basic argument for this style is that in languages like C (and Pascal) an if governs either a single statement or a block. Thus the whole block, not just its contents should be shown subordinate to the if-statement by being indented consistently.

XXXXXXXXXXXXXXX       if (test)
   XXXXXXXXXXXX           one_thing();

XXXXXXXXXXXXXXX       if (test)
   X                     {
   XXXXX                 one_thing();
   XXXXX                 another_thing();
   X                     }

Back when I first read this book (in the 90s) I found the argument for "begin-end Block Boundaries" to be convincing, though I didn't like it much when I put it into practice (in Pascal). I like it even less in C and find it confusing to read. I end up using what Steve McConnel calls "Emulating Pure Blocks" (Sun's Java Style, which is almost K&R).

XXXXXXXXXXXXXX X      if (test) {
   XXXXXX                one_thing();
   XXXXXX                another_thing();
X                     }

This is the most common style used to program in Java (which is what I do all day). It's also most similar to my previous language which was a "pure block" language, requiring no "emulation". There are no single-statement bodies, blocks are inherent in the control structure syntax.

IF test THEN
   oneThing;
   anotherThing
END
bendin