Often disappointed with placement of {


#1

Not to be snobby or even sound like I am. Admittedly, this has been a pet peeve of mine since college.

Understandably, most teachers and authors (in an effort to save space on the chalk board and in books) have always placed the opening brace or bracket of any logical block, to the right of whatever conditional or object comes before it.

For example:
If (such and such) {
// do something
}

Trouble is, the braces don’t line up well. The reason authors and teachers do this is to save space. Still, it’s a shame the Big Nerd Ranch folks haven’t decided to adopt the method of lining up the braces as in:

if (such and such)
{
// do something
}

In a trivial example such as this, there is perhaps no real benefit, but in cases where the code becomes quite complex, it can really help.

For an example of where not doing it this way can lead to confusing code, check out page 290, a CASE IN POINT!

This it turns out became part of the errata and could be summed up as simply a missing closing brace } but it begs the question!

How did this happen? I’d venture to say it’s because of what I have described above, it the importance of using the opening { and closing } as nature intended (by lining them up).

I realize the book is already fairly long, however, my recommendation would be to consider ‘wasting’ the extra line in the book anyway. I don’t know how many lines we’re talking about here but I suspect that it would only add a few more pages to the manuscript.

Cheers,
Roark


#2

I have to say that I’m happy that you are so passionate about code style. :slight_smile: I am, too!

Believe it or not, Brian and I preferred putting the open brace on the same line even before we started writing the book. I can see the case either way, but that has always been our personal preference.

I won’t get into a defense of why we decided to go with that style, but I will say that in my case it’s not for the sake of saving space. I generally prefer two lines of clear code to one line of clever code, and I prefer 14 lines with 4 lines of whitespace to 10 unbroken lines. Code is meant to be read, after all, not written.