Whitespace and coding standards

March 18, 2008 – 9:36 pm

Today I would like to touch a subject which is often trivialized by many software craftsmen - use of white space and coding standards compliance.

I work in an environment where two (or more) subcontractors often work on the same project, many times concurrently; where coding standards and clean coding style really start to shine. Big time. And yet these companies don’t seem to see the benefit in the standards — I have to do fine grained code reviews on every delivered change because if I don’t, I get something like this:

    String configSql = "{ ? = call x_int_bit_pkg.gettpnum (?)}";
    CallableStatement cstmt = con.prepareCall(configSql);
    cstmt.registerOutParameter(1, OracleTypes.CURSOR);
    cstmt.setInt(2, sp.getObjid());
    cstmt.execute();
    ResultSet rs = ((OracleCallableStatement) cstmt).getCursor(1);
    if (rs.next()) {
        result = new CPartAttr();
        result.setXValue(rs.getString("x_value"));
    }
    rs.close();
    cstmt.close();
    return result;

What makes me wonder is that those subcontractors are big and world-class recognized companies. I’m starting to suspect it’s their companies’ policies to make software in quick-and-dirty mode and forgo all good programming habits. All in all, every modification costs more and more with such approach, since the code starts to look like a big mess after some time.

But this is only my paranoia speaking.

Why can’t they see that good practices, like placing additional white space, make code more readable? And as such easier to work with after some time. And coding standards? When there are groups of people working on the same code base it’s a lot easier and faster to get a big picture when you name your variables, methods or objects in the same way:

    String configSql = "{ ? = call x_int_bit_pkg.gettpnum (?)}";
    CallableStatement cstmt = con.prepareCall(configSql);

    cstmt.registerOutParameter(1, OracleTypes.CURSOR);
    cstmt.setInt(2, sitePart.getId());

    cstmt.execute();

    ResultSet rs = ((OracleCallableStatement) cstmt).getCursor(1);

    if (rs.next()) {
        partAttribute = new ClarifyPartAttribute();
        partAttribute.setValue(rs.getString("x_value"));
    }

    rs.close();
    cstmt.close();

    return partAttribute;

Isn’t this far more readable? Take a look at the return statement of the first and second code snippet. Which is easier to grasp? I thought so.

When you code in accordance to standards you’re making next persons life easier. And probably the most important thing to remember is that this next person could be you.

Furthermore, you have a better overall code quality and delivery times which both make the customer happier. But this, in itself, is another story…

  1. 2 Responses to “Whitespace and coding standards”

  2. I dont quite agree on this, not really sure how the second snip is more readable than the first, on the contrary I hate when people add extra whitespaces and poorly indent their code.

    My two cents, stick with the sun convention or if you’re a eclipse user (ctrl+shift+f)

    ..and as for your big company consultants, provide them with your conventions and ask them to stick to it :)

    By NS on Dec 27, 2008

  3. Actually, it’s not only the whitespace. It’s the ClarifyPartAttribute, a method and a variable which are far more important. I know it’s obvious for such a short method but it pays off when your class is a lot more complicated than this.

    There was a time I also have been writing a very concise code. But one time I have stumbled upon very well-written framework which used a lot of whitespace. My first impression was similar to yours - I was repelled by it. But when I looked at it in more detail, I have been amazed by how easier it was to comprehend the code when it was organized in semantic blocks separated by whitespace.

    And when it comes to enforcing conventions - you still have to check the code for compliance. Anyway, thanks for the comment :)

    By Michał Minicki on Dec 28, 2008

Post a Comment