<p>What advantage, if any, is provided by formatting C code as follows:</p>

<pre><code>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);
</code></pre>

<p>versus something more typical such as</p>

<pre><code>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);
</code></pre>

<p>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.</p>

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

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

[{'answer_id': 70862, 'author': 'cruizer', 'author_id': 6441, 'author_profile': 'https://Stackoverflow.com/users/6441', 'pm_score': 3, 'selected': False, 'text': '<p>Nothing. Indentation and other coding standards are a matter of preference.</p>\n'}, {'answer_id': 70863, 'author': 'JamesSugrue', 'author_id': 1075, 'author_profile': 'https://Stackoverflow.com/users/1075', 'pm_score': 2, 'selected': False, 'text': '<p>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</p>\n'}, {'answer_id': 70865, 'author': 'Dark Shikari', 'author_id': 11206, 'author_profile': 'https://Stackoverflow.com/users/11206', 'pm_score': 1, 'selected': False, 'text': "<p>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.</p>\n"}, {'answer_id': 70867, 'author': 'prakash', 'author_id': 123, 'author_profile': 'https://Stackoverflow.com/users/123', 'pm_score': 2, 'selected': False, 'text': '<p>Code formatting is personal taste. As long as it is easy to read, it would pay for maintenance!</p>\n'}, {'answer_id': 70873, 'author': 'benefactual', 'author_id': 6445, 'author_profile': 'https://Stackoverflow.com/users/6445', 'pm_score': 1, 'selected': False, 'text': '<p>In order for this format to have "advantage", we really need some equivalent C code in another format to compare to!</p>\n\n<p>Where I work, this indentation scheme is used in order to facilitate a home-grown folding editor mechanism.</p>\n\n<p>Thus, I see nothing fundamentally wrong with this format - within certain rational limits, formatting is a matter of personal preference. </p>\n'}, {'answer_id': 70893, 'author': 'Andreas Bakurov', 'author_id': 7400, 'author_profile': 'https://Stackoverflow.com/users/7400', 'pm_score': 2, 'selected': False, 'text': "<p>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 ?</p>\n"}, {'answer_id': 70923, 'author': 'Ben', 'author_id': 11522, 'author_profile': 'https://Stackoverflow.com/users/11522', 'pm_score': 2, 'selected': False, 'text': "<p>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.</p>\n\n<p>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.:</p>\n\n<pre><code>if (x == 0) \n{\n  if (y == 2)\n  {\n    if (z == 3)\n    {\n       do_something (x);\n    }\n  }\n}\n</code></pre>\n\n<p>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.</p>\n\n<p>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.</p>\n"}, {'answer_id': 271627, 'author': 'brian d foy', 'author_id': 2766176, 'author_profile': 'https://Stackoverflow.com/users/2766176', 'pm_score': 6, 'selected': True, 'text': '<p>The top example is know as "Whitesmiths style". <a href="http://en.wikipedia.org/wiki/Indent_style" rel="noreferrer">Wikipedia\'s entry on Indent Styles</a> explains several styles along with their advantages and disadvantages.</p>\n'}, {'answer_id': 272759, 'author': 'bendin', 'author_id': 33412, 'author_profile': 'https://Stackoverflow.com/users/33412', 'pm_score': 3, 'selected': False, 'text': '<p>The indentation you\'re seeing is <a href="http://en.wikipedia.org/wiki/Indent_style#Whitesmiths_style" rel="noreferrer">Whitesmiths style</a>. It\'s described in the first edition of <em>Code Complete</em> as "begin-end Block Boundaries". The basic argument for this style is that in languages like C (and Pascal) an <code>if</code> governs either a single statement or a block. Thus the whole block, not just its contents should be shown subordinate to the <code>if</code>-statement by being indented consistently.</p>\n\n<pre><code>XXXXXXXXXXXXXXX       if (test)\n   XXXXXXXXXXXX           one_thing();\n\nXXXXXXXXXXXXXXX       if (test)\n   X                     {\n   XXXXX                 one_thing();\n   XXXXX                 another_thing();\n   X                     }\n</code></pre>\n\n<p>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" (<a href="http://java.sun.com/docs/codeconv/html/CodeConventions.doc6.html#15395" rel="noreferrer">Sun\'s Java Style</a>, which is almost <a href="http://en.wikipedia.org/wiki/Indent_style#K.26R_style" rel="noreferrer">K&amp;R</a>).</p>\n\n<pre><code>XXXXXXXXXXXXXX X      if (test) {\n   XXXXXX                one_thing();\n   XXXXXX                another_thing();\nX                     }\n</code></pre>\n\n<p>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 <a href="http://www-old.oberon.ethz.ch/oreport.html" rel="noreferrer">previous language</a> which was a "pure block" language, requiring no "emulation". There are no single-statement bodies, blocks are inherent in the control structure syntax.</p>\n\n<pre><code>IF test THEN\n   oneThing;\n   anotherThing\nEND\n</code></pre>\n'}, {'answer_id': 58456821, 'author': 'foo', 'author_id': 448779, 'author_profile': 'https://Stackoverflow.com/users/448779', 'pm_score': 2, 'selected': False, 'text': '<p>The "advantage" of Whitesmiths style (as the top one in your example is called) is that it mirrors the actual logical structure of the code:</p>\n\n<ul>\n<li>indent if there is a logical dependency</li>\n<li>place corresponding brackets on the same column so they are easy to find</li>\n<li>opening and closing of a context (which may open/close a stack frame etc) are visible, not hidden</li>\n</ul>\n\n<p>So, less if/else errors, loops gone wrong, and catches at the wrong level, and overall  logical consistency.</p>\n\n<p>But as benefactual wrote: within certain rational limits, formatting is a matter of personal preference. </p>\n'}]
['https://Stackoverflow.com/questions/70850', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2974/']