polas wrote:
Hi - I have a quick question. In C code it seems that some authors
will prefer to use indenting to signify a block, and some will use
braces - for instance one author may use
if (test)
do some action;
whereas another author may use
if (test)
{
do some action;
}
The first form is more susceptible to allowing certain kinds of errors
than the second one. I'm very error-prone, but despite that fact, for
some reason I tend not to make the kinds of errors that the second form
avoids. As a result, I tend to favor the first form, as being less
cluttered.
Personally I would use the second way (although can see from a style
POV its disadvantages), but is there an established convention of what
to use, and whats the standard say about it?
Yes. There's a convention; in fact, there's many conventions, each
conflicting with the others. The standard says only that both forms (and
several others) are legal.
There's a program called 'indent', available on some platforms, which
will reformat C source code to match a specified convention. If you have
access to it, I recommend reading the documentation for it. Almost every
option comes in both a positive form and a negative one. Both forms
exist because for each form, there are some people who prefer it over
the opposite form. When you look at how many options it has, you'll get
a completely inadequate concept of just how many different perfectly
legal ways there are to write C code.
I recently investigated indent in detail, and determined that my
personal preferred coding style is mostly covered by the following
'indent' options:
-bad -bap -nbbb -bbo -nbc -bfda -bfde -bl -bli0 -bls -nbs -ncdb -cdw
-nce -ci4 -ncs -di16 -fca -fc1 -hnl -i4 -ip8 -l78 -nlp -nlps -npcs
-nprs -npsl -nsaf -nsai -nsaw -sc -sob -nss -ut
That option list doesn't correspond at all closely to any of the three
common coding styles indent supports; it's possible that my code can be
uniquely identified by this combination of options, just like a fingerprint.