I would like to suggest a new rule similar to java:S5261. I know, the issue from 5261 is a bit harder, but anyways the rule described here should lead to cleaner code.
Description:
Avoid unbraced then block, if else has curly braces or vice versa.
If a selection has one of the blocks with more than a single line of code, it needs curly braces for that block. But if the other block is a one liner, this technically doesn’t need braces. But it just looks odd and is hard to read.
Always brace both blocks or none.
Noncompliant code:
if (conditionA)
doSomething();
else {
doSomethingElse();
doMore();
}
if (conditionA) {
doSomething();
doMore();
} else
doSomethingElse();
Complient Code:
if (conditionA) {
doSomething();
} else {
doSomethingElse();
doMore();
}
if (conditionA) {
doSomething();
doMore();
} else {
doSomethingElse();
}
- type: Code Smell (Major)
- Tags: SonarQube, Java, SonarLint