Cuddled Else Blocks

[Moved from UncuddledParagraphs because that page was intended to be about formatting email message text and not source code. Feel free to discuss the source code formatting issues here]

The term "cuddled" comes from GNU and Perl code-formatting terminology. In an "if-then-else" statement, the following are examples of "cuddled" and "uncuddled" elses in C:

   |   /* cuddled "else" */    |   /* uncuddled "else" */
   |   if (x > 0) {            |   if (x > 0) {
   |      x += y;              |      x += y;
   |   } else {                |   }
   |      y += x;              |   else {
   |   }                       |      y +=x;
                               |   }


There are two competing forces that need balancing. The first is the reader's need to see maximal context in limited vertical space. One way of resolving this force leads to cuddling code blocks and eliminating vertical whitespace. The second force is the reader's need to quickly distinguish paragraphs of code. One way of resolving this force is to uncuddle code blocks and add vertical whitespace.

How to best resolve these competing forces seems to me to be highly subjective. I don't find the cuddled fragment above to be significantly more legible than the uncuddled fragment. --DaveSmith


Actually, the GNU styleguide would have you write that code with each brace on a separate line:

   |   /* GNU-style uncuddled "else" */
   |   if (x > 0)
   |     {
   |        x += y;
   |     }
   |   else
   |     {
   |        y += x;
   |     }

(Except for single-line bodies they would recommend that you omit the braces because it looks silly.) Again, I don't find it any more or less clear, it's just different -- I think part of the reason is to prove Gnu's Not Unix. -- MartinPool


See also "BadCodingStandards," for more comments on blocks of code.


EditText of this page (last edited June 29, 1999) or FindPage with title or text search