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

Jacinta Richardson jarich at perltraining.com.au
Mon Aug 23 13:20:58 EST 2004


Jimen Ching wrote:
> 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.

I group that in with type 3, myself

> 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 very true and why I like type 1.


> 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?

No not at all, I was joking about that.

It's not about intelligence.   It's about habit and expectations.  If 
you expect the closing curly to appear under the first letter of the 
construct keyword you will probably take some time to adjust if that's 
never the case in this bracing style.

> 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...

It's not about smarts.  If you pick a bracing style that does things 
markedly different from the styles your lecturer, tutor and 
demonstrators use then you will probably get confused.  At university 
(where I was often in the position of tutor and demonstrator) lecturers 
often used K&R style (type 2), tutors and demonstrators used either K&R 
or GNU style (type 1) and very, very few people used type 3.

As a result, examples all included closing braces to the left and we 
often mentioned that this was a good way to identify missing braces. 
This tended to confuse the students who chose type 3.  In fact, I often 
wondered where students picked type 3 braces from, as I've never seen it 
actually used in examples except in discussions like this.  That's not 
to say I haven't seen it used, just not used in code provided as examples.

> 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.

I have noticed some of this too.  However there are a few steps between 
learning and becoming a good enough programmer to be this adaptable.  :) 
  It does help, for the less experienced among us, to ensure that the 
code is readable, where some key requirements of readable are consistent 
indentation, a consistent bracing style and tidiness.  I, myself, am not 
sufficiently gifted to pick up less readable code and find bugs in it.

When I am that good, I would still hope that I write my code in such a 
way that less experienced people still find it readable.

All the best,

     Jacinta

-- 
    ("`-''-/").___..--''"`-._          |  Jacinta Richardson         |
     `6_ 6  )   `-.  (     ).`-.__.`)  |  Perl Training Australia    |
     (_Y_.)'  ._   )  `._ `. ``-..-'   |      +61 3 9354 6001        |
   _..`--'_..-_/  /--'_.' ,'           | contact at perltraining.com.au |
(il),-''  (li),'  ((!.-'              |   www.perltraining.com.au   |




More information about the Programming mailing list