5 ms·
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.
by johnl87 16y 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.
- Seth_Kriticos 16y agoUm, 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 http://www.kernel.org/doc/Documentation/CodingStyle . It's fun to read and has some good points. Sticks quite close to K&R.
- Jach 16y agoI 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!
- deleted 16y ago[deleted]
- Seth_Kriticos 16y agoWell, 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.
- burgerbrain 16y agoOpening 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.