Click to See Complete Forum and Search --> : Steve McConnell's coding styles, braces


hyperslug
September 1st, 2004, 10:48 AM
In Steve McConnell's Code Complete he discusses the pros and cons of different layout styles using braces. Here are 2 styles that are mentioned:

example 1

if ( x == 0 )
{
statement;
statement;
statement;
}


example 2

if ( x == 0 )
{
statement;
statement;
statement;
}


McConnell claims that example 2 is superior because it is a less complicated structure and it shows subordinate instructions better. You sort of have to see the pictures he draws to understand this.

The book was recently revised into a 2nd edition but most notably, his opinion on this has not changed even though VS.NET (and probably most of Microsoft) by default uses example 1.

What do all of you think about example 2? Regardless of what we all use or prefer, do you think McConnell could be right? His expertise is legendary and his research for this book is the best I've ever seen for a computer book (maybe any book).

Joe Nellis
September 1st, 2004, 11:40 AM
There are more important things in life than where you tab indent your braces. If you agree then don't worry about it, otherwise buy all your books from Steve McConnell.

panayotisk
September 3rd, 2004, 01:00 PM
I agree with Joe. Others brace code like:
if (x==0){
statement1;
statement2;
statement3;
}

I am used to normal indenting:
if ( x == 0 )
{
statement;
statement;
statement;
}

but I have to be able to read code by other programmers that may prefer another style... There are much more important things in programming (and in life).

Davey
September 4th, 2004, 04:36 PM
Its one of those questions that keeps on coming around and can incite loud views from all sides of the table.

I've seen all these styles used and have probably used them all myself.

I think what is more important is to be consistent with your current project. If the rest of your project team uses one style, then it makes sense to stick with it. If you're a single developer, then stick with what you are comfortable with.

cilu
September 6th, 2004, 07:58 AM
I think as long as you stay consitant, i.e. always use the same style, any would do. As for me, I prefer the first one:

if ( x == 0 )
{
statement;
statement;
statement;
}


I don't like the Java style

if ( x == 0 ) {
statement;
statement;
statement;
}

because when there are too many blocks imbricated, I tend to lose the track of them, especially where they begin.

marsh_pottaye
September 9th, 2004, 04:00 AM
I don't like the Java style

if ( x == 0 ) {
statement;
statement;
statement;
}

because when there are too many blocks imbricated, I tend to lose the track of them, especially where they begin.
Additionally, the Javamen cannot overload the operators. This is also a reason from that sometimes a Java line of code can look like a long, long salami. :D