Sometimes when I read someone else's code, I cringe... the spacing and indenting seems so foreign, it takes twice as long to read a block of that code than some of my own code. Both programs compile, of course, but a C program could be smashed together on one single line and it would still compile
Got me wondering... what style do most people use here? To describe these admittedly rather unfamiliar types, I turn to Wikipedia...
- K&R Style
[K&R style] keeps the first opening brace on the same line as the control statement, indents the statements within the braces, and puts the closing brace on the same indentation level as the control statement (on a line of its own). Functions, however, are braced distinctly from statements; an opening function brace is placed on the line following the declaration, at the same indentation level as the declaration.
int main(int argc, char *argv[])
{
...
while (x == y) {
something();
somethingelse();
if (some_error)
do_correct();
else
continue_as_usual();
}
finalthing();
...
}
- Variant: 1TBS
In this style, the constructs that allow insertions of new code lines are on separate lines, and constructs that prohibit insertions are on a single line. This principle is amplified by bracing every if, else, while, etc.—even single-line conditionals—so that insertion of a new line of code anywhere is always "safe".
if (x < 0) {
printf("Negative");
negative(x);
} else {
printf("Non-negative");
nonnegative(x);
}
- Allman Style
This style puts the brace associated with a control statement on the next line, indented to the same level as the control statement. Statements within the braces are indented to the next level.
while (x == y)
{
something();
somethingelse();
}
finalthing();
- GNU Style
Like the Allman and Whitesmiths styles, GNU style puts braces on a line by themselves, indented by 2 spaces, except when opening a function definition, where they are not indented.[7] In either case, the contained code is indented by 2 spaces from the braces.
static char *
concat (char *s1, char *s2)
{
while (x == y)
{
something ();
somethingelse ();
}
finalthing ();
}
- Lisp Style
A programmer may even go as far as to insert closing brackets in the last line of a block. This style makes indentation the only way of distinguishing blocks of code, but has the advantage of containing no uninformative lines.
for (i = 0; i < 10; i++) {
if (i % 2 == 0) {
doSomething (i); }
else {
doSomethingElse (i); } }
I think everyone has their own style, but probably falls primarily under one of these definitions. Personally, my style is most like K&R, where I try to collapse the (visually pointless) curly braces onto their relevant line as much as possible - that is, since a curly brace defines a data array, or a function, it should be on the same line as what defined it (like "void myFunction (byte parm1) {"). Most styles seem to put the brace on its own line all by itself, which I see a lot as well. For readability, sometimes I expand a long control statement into multiple lines, or expand one-liner "if/else" statements to their own lines.
Thoughts?