1. Home
  2. Computing & Technology
  3. C / C++ / C#
photo of David Bolton
David's C / C++ / C# Blog

By David Bolton, About.com Guide to C / C++ / C#

C Programming Best Practises

Friday June 19, 2009
Most programmers i know aspire to write good code but sometimes it's not always clear what's best. If it's your own code then that's one thing but in a team or code that others will have to maintain is another. An interesting article on embedded.com by the former editor in chief listed ten practices he thought best for C Programming.

But his number one rule about always using braces after if, else, switch, while, do, and for is one I don't always agree with. Why? Because too many braces can be hard to comprehend, even if formatted neatly, and I've seen applications with functions nesting 5 or 6 levels deep and being way too long. Pascal for all its sins let you nest functions and in C++, classes are better for logically grouping related variables and functions.

So do you prefer?

if (something)
  dosomethingelse;

or

if (something) {
  dosomethingelse;
}

Comments
June 28, 2009 at 6:09 am
(1) dum8d0g says:

Well, I prefer the second way – I do really white-space style like..

void
Object::method ( const int & _size )
{
return ( _size * ( 3 – 2 ) ) ;
}

because I think it is really clear to read.. But both of the ways have some advantages and disadvantages. Probably the most difficult thing to format with second style is do-while :-)

July 2, 2009 at 2:22 am
(2) SteveC says:

I prefer the first way, because,
http://lxr.linux.no/linux/Documentation/CodingStyle

Leave a Comment

Line and paragraph breaks are automatic. Some HTML allowed: <a href="" title="">, <b>, <i>, <strike>

Discuss
Community Forum
Explore C / C++ / C#
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. C / C++ / C#

©2009 About.com, a part of The New York Times Company.

All rights reserved.