-
Notifications
You must be signed in to change notification settings - Fork 20
Add more exceptions to the EmptyBlock rule #221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi @Indigo744, Thanks for re-raising! Definitely open to changing some of this rule's behaviour. My two cents: I think there's two mindsets that this rule can be approached from. On one hand, the comment indicates that the empty block is intentional and serves some purpose. Who are we to disagree with the code author? On the other hand, disagreeing with the code author when best practice is not followed is the stated goal of the Sonar ecosystem. I do think that an empty block is usually a code smell, even with a comment. For instance, I would want this rule to catch your example, since an if condition with no body is a no-op that adds visual and cognitive noise. That being said, I agree that there are cases where empty blocks can help clarity, particularly with comments. For example: if A then
DoAThing();
else if B then
DoBThing();
else if C then
// Do nothing - C is ignored
else
DoDefaultThing();
end; While this could be rewritten without the empty block, I would not say there's a code quality issue here. My opinion is that this rule should sit in a middle ground between ignoring the author's intention and dogmatically following it, and it might not have hit the right balance yet. Interested to hear other's thoughts as well. Do you have any other examples of cases you think the rule should exclude? |
Hello @fourls I understand my example was a bit contrived. Your example is more representative of a reasonable empty block. if TestA then begin
DoSomething();
else then begin
// Comment explaining why nothing needs to be done here
end; The strongest signal I can think of is that every implementation of this rule in other languages ignore code blocks with comments. I linked to the C# one, but the exception is the same in Java, C, Python, and so on... I'm not saying that you should blindly follow what others are doing, but it seems that this exception is commonly accepted everywhere in the Sonar ecosystem (and it is not even customizable). Maybe adding it as rule customization would help here, since it would be a breaking change and some may be against. For instance, a "strictness" option:
|
Worth noting are the exceptions to the exception: The I think the main reason all of these different Sonar language plugins came to the same conclusion about exceptions for empty blocks is... they were all written by SonarSource. The fact that all of them have this exception only proves to me that they consistently followed the description given in the jira issue. |
I'm inclined to agree - we do care about consistency with the other plugins, especially with a general rule like this that has no reason to be significantly different in Delphi.
I'm not too worried about rule changes that reduce overall issue count - I'd be more worried if we were making it stricter and users suddenly had to deal with thousands of new issues. In general we try to avoid complex rule options - the closest we've got to a feature toggle like this is probably the For people who want stricter behaviour, we could perhaps have a separate rule, but that's probably unnecessary unless it's heavily requested.
Good catch. That being said, these exceptions make the rule even more relaxed than SonarSource's "baseline" (they do not require comments at all in these cases). This is swaying me even further towards relaxing our rule to match Sonar's. |
Yeah, I didn't note the other exceptions of some rules, as it was even more relaxed. I think allowing empty block with comments is largely enough. Just to add another example, here is the ESLint rule: https://eslint.org/docs/latest/rules/no-empty
Anyway, if we all agree on this, moving forward I could try to submit a PR. |
Hi @Indigo744, Agreed - let's make this rule match the other analyzers. A PR would be very welcome! The main files you'd need to take a look at are:
Looking forward to seeing it. |
Prerequisites
Rule to improve
EmptyBlock
Improvement description
The EmptyBlock rule is currently defined as is:
I think the last exception can be made broader: any blocks that are empty apart from a comment.
Example that triggers a finding related to this rule, but should not:
Rationale
This is the way other scanner are implementing this rule.
And I think it makes sense! Comments are part of the code (and act as documentation).
For reference: https://sonarsource.github.io/rspec/#/rspec/S108/csharp
The text was updated successfully, but these errors were encountered: