Ar Rakin <
rakinar2@onesoftnet.eu.org> writes:
Hello there,
>
I've been writing C code for a long time, in different styles, but
always wanted to know which code style most people prefer. This is all
about that question. Which one of the following do you prefer the
most?
>
[...]
>
3. Other Styles?
>
Please show an example!
The question being asked is mostly about indentation and placement
of braces. There are a few points here that I think are worth
making.
One, simply asking for a preference or an opinion is a waste of
time. The question is not what choices are preferred but what
factors motivate the choices made (example to be given below).
Two, it's just as useless to say a particular layout is "more clear"
or "more readable".
Three, the idea that it's all just opinions or personal preferences
is cowardly. In effect the message there is that any opinion is
equally valuable. That is not the case.
Four, there are lots of different layout choices used widely enough
to have recognized communities of adherents. Here is a list
constructed from the wikipedia page on indentation style:
1TBS aka One True Brace Style
BSD aka Allman
BSD KNF ("kernel normal form")
"FORTRAN" (no indentation)
GNU
Horstmann
Java-like
K&R
Linux Kernel
"Lispish" (C as it might be written by a Lisp programmer)
Pico
Ratliff
Stroustrup
Whitesmiths
Note: as best I can tell the principal difference between K&R
and 1TBS is K&R allows single-statement bodies of if() and
while(), etc, to be given indented on the next line, without
any braces, whereas 1TBS insists on braces in such cases.
The layout style I personally prefer is closest to 1TBS. As it
turns out I tried lots of different layout choices, and ended up
on a 1TBS-like pattern, years before I ever learned C. That's a
data point but it doesn't really convey much information, in line
with my earlier statement in point One.
Five, an example. Consider a decision with only one degree of
freedom: for the start of a function definition, should we write
this
size_t length_of_string( const char *s ){
or this
size_t
length_of_string( const char *s ){
(ignoring other layout choices as being incidental to the
question here).
There are three areas of consideration:
(a) effects on development-time activity
(b) effects on code-reading or code-inspection activity
(c) effects on the source code itself.
For (c), the one-line style uses one fewer lines, but has a
higher chance of needing to split the function parameters across
a line boundary.
For (a), the two-line style lends itself to searching using
standard editor tools
For (b), the two-line style
(1) makes it easy to find both names of functions and types
of functions using just my eyes (fast thinking) without
having to parse the symbols involved and separate them
(slow thinking); slow thinking uses more energy and
mental effort than fast thinking
(2) related to (1), the same holds true for reading on
hardcopy rather than a display
(3) is easier to process using simple tools such as grep
or awk
(4) gives an increase in code size, probably in the range
of 5 to 10 percent, which means programs are longer
and take up more "space" on an output medium (which
may affect reading time)
Of the factors listed, probably the largest effect is due to
being able to find function names and types visually, rather than
having to use higher level brain functions. The cost of longer
program source is significant, but in my experience that is more
than outweighed by the savings in energy and mental effort used
when reading and developing.