New rule to add missing curly braces to outer blocks

I would like to suggest a new rule similar to java:S5261. I know, the siiue from 5261 is a bit harder, but anyways the rule described here should lead to cleaner code.

Description:
Avoid unbraced blocks with more than one level of code inside.

If a block is technically a one liner and hence doesn’t need curly braces, but in fact has more then a level of code inside and hence more than one line of code, it should be braced. Visually only the indentation leads to the code inside the block to be executed within the block. And this can lead to misunderstood code or even wrong execution.

At least this is hard to read when the inner block becomes larger.

Noncompliant code:

for (int i = 0; i < 10; i++)
    if (i == 5)
        doSomething();

if (conditionA)
    for (int i = 0; i < 10; i++) {
        doSomething();
        ...
        doMore();
        ...
        doFinalSteps();
    }

while (conditionA)
    if (conditionB)
        doSomething();
    else {
        doSomethingElse();
        ...
        doMore();
        ...
        doFinalSteps();
    }

Complient Code:

for (int i = 0; i < 10; i++) {
    if (i == 5)
        doSomething();
}

if (conditionA) {
    for (int i = 0; i < 10; i++) {
        doSomething();
        ...
        doMore();
        ...
        doFinalSteps();
    }
}

while (conditionA) {
    if (conditionB)
        doSomething();
    else {
        doSomethingElse();
        ...
        doMore();
        ...
        doFinalSteps();
    }
}

Of course this applies to any other block building language feature as well.

  • type: Code Smell (Major)
  • Tags: SonarQube, Java, SonarLint
1 Like

Hello @mfroehlich, thank you for proposing this rule and the very detailed explanation and examples, it’s very much appreciated.

Regarding the topic of unbraced blocks, it is always preferable and recommended to use brackets, and there are a few solutions to enforce this:

  1. Use the IDE to apply a desired code style and have, for instance, brackets on your statements like if-else, try-catch, and while/for loops. For example in IntelliJ you can configure code style schemes.

  2. Use Git-Hooks to automatically check that your commits fulfill a specific code style and even to apply it. Try googling “apply code style with git pre-commit hook”, and check also google-java-format.

I will follow up as soon as I manage to discuss your rule proposal with the team. My feeling although is that, due to the available solutions that I mentioned before, the rule may have little value to the users.

Cheers

Well, I thought, due to the similarity to S5261 this would be an easy go.

Btw. this here would be a clear exception to the rule:

Compliant:

for (int i = 0; i < 10; i++)
    doSomething(i);

if (conditionA)
    doSomething(i);
else
    doSomethingElse(i);

A pure one liner still doesn’t need to be braced.

1 Like

Hello @mfroehlich, you can now check the progress for the rule you proposed by checking SONARJAVA-4655.

2 Likes

Lovely! Thanks a lot.