Tag Archives: single line statements

goto fail

If you’ve ever worked with me (or had me review your code), you’ll know that I am a stickler for good coding standards1. I consider a Checkstyle failure a build failure and it does cause the build to break. The reason for this is that we’re human, we make mistakes, and by adhering to good coding standards we can minimise the number of mistakes we make.

One of my absolute bugbears is single line statements with no braces2. My preference is:

if (condition) {
    action();
}

Since this isn’t a post about the ongoing Holy War on where the braces should go I will also accept:

if (condition)
{
    action();
}

I will grudgingly accept:

if (condition) { action(); }

I’m more inclined to accept the above with languages like Javascript where this is a commonly used convention.

Unless you’re coding in Python or similar I will not accept:

if (condition)
    action();

Why not? Well it’s just asking for failure. It’s so easy to just add another line and suddenly have something accidentally be executed that you didn’t want. Python gets round this with indention, which is significant, other languages just trip you up.

How bad can it be, I hear you ask? Well lets consider the following snippet of C:

if ((err = SSLHashSHA1.update(&hashCtx, &signedParams)) != 0)
    goto fail;
    goto fail;

We are quite literally going to fail. This is a snippet from sslKeyEchange.c which is responsible for this little nasty. Ignoring the fact that the code is a whole series of WTF’s piled on one another it’s a bug that could have been so easily spotted by just using braces.


1 The actual phrase used would imply that my policies on coding belong in WWII era Germany.

2 For languages that support them.