[prog] C and curly braces - question of style?

Jimen Ching jching at flex.com
Sun Aug 22 16:55:54 EST 2004


On Mon, 23 Aug 2004, Jacinta Richardson wrote:
>1. curlies aligned with construct keyword.  ie:
>2. starting curly on key line, ending aligned with construct keyword.  ie:
>3. curlies aligned with indent level.  ie:

You forgot 4, ie:

while (...)
    {
	a = b;
	b = c;
    }

Emacs sometimes does this automatically for you.

>In my opinion either 1 or 2 is fine and learning to change between the
>two is easy.  2 has the advantage that less whitespace is used, 1 has
>the advantage (at least for beginners) that it's really easy to see both
>curlies.  I started off with style 1 and but now tend to mostly use 2.
>Too much maintenance of other people's code.

Which is not a good reason to select a style.  One problem I've seen more
than once with 2 is with long lines.  For C++ and templates, if/while/for
statements could get really long.  Modern IDE's usually don't cause long
lines to wrap-around.  Thus, when you have a brace at the end of the line,
you can't see it.  On multiple occasions, colleagues accidentally added a
character after the brace and got compile errors.  After spending half an
hour staring at the code and couldn't figure out what is wrong, they ask
me over to proof read and I noticed the horizontal scroll bar.  Moving the
scroll bar over reveals the culprit.

This is why I don't use 2 myself.  But if the file I'm editing uses it, I
reluctently conform...

>Also in my opinion, anyone who uses 3 should be forced to change, or
>perhaps shot ;) (joking).  I'm very glad that only a few people do it.
>My biggest issue with this one is that everyone who uses either format 1
>or 2 expect that the close curly will be (in this case) on the left
>margin.  Not having it there will have these programmers assume that its
>missing and put it in.  In my experience (when teaching C) most students
>who adopted the 3rd style here had greater difficulty debugging their
>programs than the others because of these expectations (which they often
>had too) about the final curly.

Let me get this straight.  Because people in group 1 and 2 aren't smart
enough to notice the brace style, so the people in group 3 should be shot?

And people in group 3 are even less smart than people in groups 1 and 2
because they don't realize they are in group 3 themselves?  For this
reason, we shouldn't have group 3 at all?  I guess that makes sense, in
some environments...

>Indentation and consistent bracing is a huge step towards minimising
>obvious syntax errors and spending more time coding rather than debugging.

Although these are very good advice, I would like to point out an
observation.  I noticed people who are excellent programmers don't care
about the brace style or any coding styles.  They can use any style, and
don't have any problem debugging code using all styles, mixed styles, and
just simply crazy styles.  For some reason, these programmers are able to
understand code written with just about any coding style without problems.

This is not a scientific observation.  It's just something I notice.

--jc
-- 
Jimen Ching (WH6BRR)      jching at flex.com     wh6brr at uhm.ampr.org


More information about the Programming mailing list