News for hackers. Stuff that matters.
Re:Deconstructing "K&R C" by adavies42 on Tuesday January 10, 2012 @05:12PM (#3449332)
> Braces Are Free, Use Them

I disagree. Braces have a cognitive load, particularly if they're given whole lines to themselves. Which is easier to read,

  while ((len = getline(line, MAXLINE)) > 0)
      if (len > max) {
          max = len;
          copy(longest, line);
      }
  if (max > 0) /* there was a line */
      printf("%s", longest);
or

  while ((len = getline(line, MAXLINE)) > 0)
  {
      if (len > max)
      {
          max = len;
          copy(longest, line);
      }
  }
  if (max > 0) /* there was a line */
  {
      printf("%s", longest);
  }
? Personally, I'd probably write it

  while ((len = getline(line, MAXLINE)) > 0)
      if (len > max) {
          max = len;
          copy(longest, line);
      }
  if (max > 0) printf("%s", longest);
. Consistent structural indenting makes it easy to see the boundaries of the control structures.
Re:Deconstructing "K&R C" by scott_s on Tuesday January 10, 2012 @06:26PM (#3449635)
Because it's the style I use, and because I don't need to bug-check the indented code to make sure it's braced correctly, I find this easiest to read:

  while ((len = getline(line, MAXLINE)) > 0) {
      if (len > max) {
          max = len;
          copy(longest, line);
      }
  }
  if (max > 0) { /* there was a line */
      printf("%s", longest);
  }
Re:Deconstructing "K&R C" by silon3 on Wednesday January 11, 2012 @03:24AM (#3450884)
By far.
Re:Deconstructing "K&R C" by ufo on Tuesday January 10, 2012 @05:29PM (#3449420)
We are talking abou K&R here. Please use the less spacious TrueBraceStyle instead of the heretic Allman style. :)

Joke aside, the problem I see in your argument is that lack of braces is also a cognitive load. If braces are always there I can safely ignore them but if they can be ommited I need to worry if a certain piece if code is braced or not when I read and edit it.

The only exception I currently live with is single line if statements, as long as the statement is on the same line as the `if`

Re:Deconstructing "K&R C" by SeanLuke on Tuesday January 10, 2012 @05:16PM (#3449353)
I'm probably the last person in the world to prefer:

      while ((len = getline(line, MAXLINE)) > 0)
          if (len > max)
              {
              max = len;
              copy(longest, line);
              }
    
      if (max > 0) /* there was a line */
          printf("%s", longest);
          
I fear this may be literally true: this brace style is called Whitesmith's, and I was reporting bugs to the cc-mode indenter for emacs a while ago. BSD/Allman and K&R styles never made sense to me.
Re:Deconstructing "K&R C" by zedshaw on Wednesday January 11, 2012 @12:16AM (#3450555)
Add a new statement to the while-loop. Now you've figured out why that style isn't so hot. You now have to remember to go add the new braces, and typically people are mentally putting them in so they forget. Same with the last if-statement. You'll go, add the new line, indent and go "cool done". Then scratch your head for hours wondering why it's not working.
Re:Deconstructing "K&R C" by SeanLuke on Wednesday January 11, 2012 @04:37PM (#3453822)
Oh, well there are two issues here: whether to always include braces, and what brace style to use. Whitesmiths is a brace style, that's what I was pointing out. The decision to include braces or not is a different thing. I tend to omit braces in obvious situations, but am not beholden to it.
Re:Deconstructing "K&R C" by Roboprog on Wednesday January 11, 2012 @03:58PM (#3453647)
Exactly. Fully bracketed syntax prevents this kind of maintenance breakage.

Weird contrast: Perl, for all its klugy syntax, requires fully bracketed syntax on the statement bodies of "if", "while" & the like, so this never happens. Of course, you get other readability issues there...

Re:Deconstructing "K&R C" by Roboprog on Tuesday January 10, 2012 @11:59PM (#3450505)
Steve McConnell makes a pretty good case for Whitesmith's formatting in this book (at least the first edition) in the code formatting chapter:

http://books.google.com/books/about/Code_complete.html?id=Qn...

Of course, he presents most of the major styles, pretending to be "fair and balanced", but if you read between the lines of the "abstract block" argument, it's clear that the other styles which align keywords or grouping symbols at a different level other than the compound statements they delimit are brain damaged, or at least visually misleading.

Re:Deconstructing "K&R C" by glitch on Tuesday January 10, 2012 @07:02PM (#3449760)
SeanLuke, that is exactly how I'd write it too! ✼Whitesmith fistbump✼ — Related: I love the Wikipedia talk section on it: http://en.wikipedia.org/wiki/Talk:Indent_style#Whitesmiths_s...
Re:Deconstructing "K&R C" by NickPollard on Wednesday January 11, 2012 @06:41AM (#3451341)
I'm currently experimenting with a hybrid of this and K&R style, that would look like this:

  while ((len = getline(line, MAXLINE)) > 0)
      if (len > max) {
          max = len;
          copy(longest, line);
          }
    
      if (max > 0) /* there was a line */
          printf("%s", longest);
So far I'm finding that it gives good readability, doesn't waste too many lines on braces, and doesn't run into many issues of accidentally leaving braces out.

I started it after having started writing lisp and python, and I'm getting used to just ignoring the braces and going by indentation, which this style emphasises.

Re:Deconstructing "K&R C" by cbs on Tuesday January 10, 2012 @06:43PM (#3449691)
Easiest to read? The middle. The way I would write it for my own use is closest to the top, but the easiest to read is the middle.

Always using braces, and giving them their own lines makes nesting as unambiguous as possible and the extra white space reduces density making foreign code easier to digest.

Re:Deconstructing "K&R C" by zedshaw on Tuesday January 10, 2012 @11:52PM (#3450493)
> Braces have a cognitive load, particularly if they're given whole lines to themselves. Which is easier to read,

Checking braces has a higher load when they aren't there and when they aren't consistently used. But, without evidence both of our points are pointless. So, in my book I'm pushing the standards of always use braces except in a few little cases.

Re:Deconstructing "K&R C" by mindcrime on Wednesday January 11, 2012 @12:14AM (#3450550)
I'll take the 2nd one, please. And twice on Sunday.
Re:Deconstructing "K&R C" by nessus42 on Tuesday January 10, 2012 @08:06PM (#3449976)
All this talk about where the right place to put braces is, makes me pine for Python.
Re:Deconstructing "K&R C" by Teckla on Wednesday January 11, 2012 @05:50PM (#3454076)
When it comes to assignment and comparison in constructs such as while loops, I think this variation adds readability:

while (len = getline(line, MAXLINE), len > 0) ...

I've never seen anyone else use it, though.

Re:Deconstructing "K&R C" by lurker14 on Tuesday January 10, 2012 @05:43PM (#3449478)
The middle version uses braces nicely to add whitespace to the function to help the reader flow through the code, instead of chasing matched-parens and wondering if each line is a continuation of the previous.