Page last modified 00:12, 13 Oct 2009 by JLemkul?

Block Structure

From $1

Table of contents

In order to make the source code readable always use braces around blocks, and put braces on the next line, even if it is unambiguous. Use

if (foo) 
{
    blah();
}
else 
{
    bleh();
}

for (i=0; (i<imax); i++)
{
    printf("Foo %d\n",i);
}

rather than:

if (foo)
  blah();
else
  bleh();
for(i=0;i<imax;i++) 
  printf("Foo %d\n",i)

Note that there is an empty line between the two logical blocks (if statement and for loop)

Try to add a comment before a loop or conditional statement to tell *why* you are doing something, not what you are doing. Most users hacking the code know that i++ means that you incrementing i, but why are you incrementing it?

Spaces are good - that's what the space-bar on your keyboard is so large :-) Personally I feel that e.g. using parentheses around the i<imax test in the loop above is unnecessary, but to make a point I won't alter it: if in doubt, use spaces or parentheses! --Lindahl 21:14, 27 February 2009 (CET)

Tags:
 
Comments (0)