Is there an existing Java rule that may be by default turned off to deal with double if blocks that use the same parameters but checked differently, instead of alternatively using âelse ifâ?
If there is additional code in between two if blocks with the same parameters, this rule should not trigger by default. Comments in between two if blocksâŚopen to suggestion, my opinion is to still trigger.
Welcome in this forum. Sorry, I somehow missed your thread (weeks agoâŚ) while looking at new topics targeting Java (it wasnât using the tags and Iâm not reading all the threads).
I searched a bit in our existing rules, and could not match one matching to your requirements.
I know that itâs an example, but note that in snippet I would expect the rule âCondition always Trueâ to trigger on the second condition (you donât have the choice regarding nullness).
In a way, I feel that what you suggest would be close from what rule squid:S1066 (Collapsible âifâ statements should be merged) is already doing⌠But itâs not really the same use case.
Iâm however wondering, while your example is quite simple, what would you expect when having more complex conditions?
if (a && b) {
// ...
}
if (b || (c && a)) {
// ...
}
I however agree that as soon as there is code in between the two if, we canât say anything. Would you expect the rule to only trigger for simplest cases (single variable, with multiple states)?
Finally, Iâm also wondering what would be the ultimate goal of such rule. Question of style? Grouping tests on same variables together? Some would argue that itâs not necessarily âeasier to readâ, and I would appreciate your opinion!