C coding style
6 messages in this thread
It's interesting what a mental block this is for me. I work with Chris on a
lot of C code. He writes in standard K&R style like you. I prefer George and
Dave's approach more or less (I indent 3, put braces on seperate lines, and
place the braces at the level of the block indent). Chris grumbles about my
"peculiar style" but doesn't have any trouble reading it. I grumble about his
style (but not loudly, because he's the one following the standard), but have
one HECK of a time following his code without drawing guidelines all over the
place.
Of course I try to keep in mind that he's doing it by the book and I'm the
maverick; it's my responsibility to read his style, not vice versa. But it
does make things difficult!
I agree with the comment about it being vital that all people on a team use the
same or similar styles. I also feel your point about wasted vertical space is
valid. I dunno, I realize I'm making work/trouble for myself, but the
structure just seems to be more visible to me when the opening and closing
braces are in the same column. When the opening brace is at the end of a
statement, I get into a "Where the heck does this block start!" mentality
(which is admittedly MY problem).
But then it goes along so nicely with all my others. 8D
Rick
Rick,
By using the larger indents (but not 8 chars…I use 5), the block
structure becomes very clear (to me) and the "look up rule" where the closing
brace is on the same level as the opening statement makes it clear (again to
me) where the block starts and ends.
Yes, the opening brace can get "lost" visualy if you've got a comment
on the same line like this:
if (something) { /* comment here "hides" the open brace */
foo();
bar();
baz(); /* comments here hide nothing */
} /* hopefully obvious close of the "if" */
But even with the visual losing of the open brace, the block structure
of the code enforced by the indents should make it clear what comprises the
block and by looking up in the same column as the close, it should also be
obvious that I'm closing the "if".
But again, it is all what you're used to. I prefer this style because
it's the way I learned, and it seems intuitive to me. If you and Chris can
live with each other…fine. That's all that's important..that each of you can
read the other's code.
Don
Don,
I hate it when lines end up longer than will fit on my 80-column display.
Using only 3 spaces alleviates most of the problems.
Dave @OTG
(An old ed habit? :-))
For programming on the Amiga, or UNIX, or, I suspect, most other computer
systems nowadays, using a 4 character indent will save you a fair amount of
file space. Each pair of indents is then a single tab, which is one byte
instead of 8. If you use 3 or 5 as indents, it doesn't work out as well.
Um, but then again I picked up the 3 character indent habit from ed. Can't
teach an old dog … 🙂
Dave @OTG
You know, this is really interesting. I wonder what the preferences say about
our psyches which we can't see…
You prefer not to have both opening and closing braces on seperate lines, cited
as one reason the wasted vertical space. I, on the other hand, wouldn't want
to indent five because of the wasted HORIZONTAL space. I'm always running out
of room across the screen and/or printed page (though I agree that it's more
difficult to follow code which doesn't fit all on one screen).
Sometimes I wish I had learned K&R standard; I actually don't know where I
picked up my style. But I'm happy with it and, unless I decide to put in the
effort to change, I'm stuck with it. So far it hasn't caused TOO many
problems; when that starts to be the case, it's time to change my spots (er,
braces! 8)
Rick