Cool Code with Brackets

May 31st, 2006
[ General ]

I recently switched bracket styles in code. Here’s what I used to do:

private void writeBugEventLatest() {

string sql = “update bug set ixbugeventlatest….”;
reader.writeOut(sql);
int rowsAffected = cmd.ExecuteNonQuery();

}

It’s clean and doesn’t take up a lot of lines. What I’ve never explicitly done is strived for a code style that makes errors, bad code etc easier to spot. I’ve made the simple switch to this style, which is most likely the more common of the two:

private void writeBugEventLatest()
{

string sql = “update bug set ixbugeventlatest….”;
reader.writeOut(sql);
int rowsAffected = cmd.ExecuteNonQuery();

}

The result is easier to scan and see the opening and closing braces. Actually I really don’t even have to scan, I can just see them. Technically I like the look of the other style better but I agree the latter is easier to work with so I’ve given up on cool and made the switch.

3 Responses to “Cool Code with Brackets”

  1. Chris Says:

    When I was in school it was taught that the *only* way to do it was the latter way. It was instructed that it is much easier to understand and follow that way.
    My first few jobs, that is how I coded, and how all the co-workers coded too.

    Now, maybe because it’s java, (versus C/C++) we’re all about the former.
    What annoys me, are the single line if/else statements that don’t have braces.
    if (ExecuteNonQuery)
    System.out.println(”Where’s my brace?”);
    else
    System.out.println(”No kidding.”);

  2. Chris Says:

    Mind you, both examples are better than:

    private void writeBugEventLatest(){string sql = “update bug set ixbugeventlatest….”;reader.writeOut(sql);int rowsAffected = cmd.ExecuteNonQuery();}

    Word.

  3. Curt Says:

    2 years later but…

    As Chris said above it was literally drilled into our heads to use the 2nd way. It’s the way we were taught to do it with C/C++ and it’s the only way I do it now.

    You have to keep the code readable!

Leave a Reply