Hacker News new | ask | show | jobs
by johnl87 5700 days ago
Little known fact...the only reason the brace is on the same line as the function declaration is because K&R just wanted to save space in the code listings.
2 comments

Um, no, K&R braces after function declaration are in the next line, because they are special.

Braces after if, for, while, switch, etc. are on the same line, and yes, it was to save space on the 80x24 screen.

Get a copy of "The C Programming Language, Second Edition". It's right in there. It's the only technical computer book I know that is still accurate after more than twenty years.

I also recommend reading the Linux kernel style guide http://www.kernel.org/doc/Documentation/CodingStyle . It's fun to read and has some good points. Sticks quite close to K&R.

I agree it's fun to read and has some good points, but it's obvious it's meant only for really imperative-styled C that you'd expect to find in the kernel. e.g.:

> ... if you need more than 3 levels of indentation, you're screwed anyway, and should fix your program.

  class A:
    def foo(self):
      while cond:
        if othercond:
          bar()
Uh oh, four indentation levels!
Well, yes. But the style guide was written for plain C, in which the program logic starts at the first indentation in functions.

In Python you have very different characteristics, and have to adapt styles.

You should not get deeper than 3 logic indentations on function level there either, though.

And from my experience, 8 character tabs (with the article you cited actually starts with) are still workable in Python, at least for me.

Opening braces on the same line means that you can always open a new line for editing with O and o in vi and it will be a valid line for placing code on.

Very handy.