if, for, or while with an empty body, use empty braces instead of a semi-colon

google-closure-compiler, javascript

Solution

This guards against things like:

if(condition);
{
    // oops, I really meant for this to be conditional.
    // now it always executes, regardless of condition.
}

The error raised forces you to explicitly put the empty `{}` before the `;` so it's clear that that's what you intended.

Problem

Using google closure compiler I get the following warning message: ``` WARNING - If this if/for/while really shouldnt have a body, use {}. ``` Here's what the documentation says: This warning means that you have a semi-colon immediately following an if, for, or while statement. For example: ``` // Produces JSC_SUSPICIOUS_SEMICOLON warning: if (true); else alert('no'); ``` Is this just a code convention thing or is `{}` better than `;` for some reason?

Original source