-
Notifications
You must be signed in to change notification settings - Fork 79
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
[EC2][Java] Problem with detecting applicable cases #232
Comments
@dedece35 said:
I feel like I need to clarify my intents for this issue: I agree with the rule, but there are implementation errors. About the issue itself
Why it mattersFalse positives are huge issue, even more with a plugin this young. ecoCode is still very young and needs to convince its audience. This is something we won't achieve if, when we add the plugin to existing projects, we have thousands of warnings for code we cannot fix because the rule generates false positives. This is what happened when we added ecoCode to our SonarQube instance, by the way. Why I made this an issueBecause I won't have time to submit a PR anytime soon, for personal and professional reasons. Still, I thought my input and experience could be of value, so I share what I saw and know. This is more a "hey guys, maybe we could add this on a roadmap" than "this needs fixing, it's broken." Though in its current state, my client deactivated this rules due to the amount of false positives. How I expect this can be solvedI give basic samples of valid code. My way to go about it would be to go TDD: prepare the tests with those examples of valid code, and then fix the rule until all tests are green. We can disagreeI'm fallible and ready to hear it. I'm always happy to learn. Just explain and prove me; show me specific data, or counter-examples. I can also hear a "You might be right, but we won't do it because…" On the other hand, I have trouble hearing a flat "You're wrong" or "No." Please explain your answers when I went through the trouble of providing detailed data or explanation. Cases I agree should be switchesEquality tests for one variableif (a == value1) {
} else if (a == value2) {
} else {
} This is basically the definition of a switch case before Java 17, if a is a primitive or an enum. Cases that are detected but I think are valid codeOnly applies to simple equality conditionsif (checkSomethingAbout(a)) {}
// could probably be written as switch, starting Java 17, but not before if (a == null && b == valueB || c != valueC) {} Not all condition types can be rewritten as switches (at least not before Java 17). If the condition is a call to a method or a complex condition with AND or OR operators, it cannot be written as a switch. == nullif (a == null) {} One condition type must be excluded from this rule: switch does not allow null, as far as I know. Conditions on different elementsif (a == valueA) {
} else if (b == valueB) {
} switch tests different values for one argument, so the code above cannot be replaced with a switch and therefore should not raise an issue. Cases I'm not sure aboutIf or if/else conditionsif (a == valueA) {} if (a == valueA) {} else {} I see no gain here in writing a switch, but I may be wrong. |
The rule exists in several Java IDEs Example for IntelliJ For information The opposite rule to respect java:1301 : Minimum 3 cases for using switch)
switch (variable) {
case 0:
doSomething();
break;
default:
doSomethingElse();
break;
}
if (variable == 0) {
doSomething();
} else {
doSomethingElse();
} |
I second @cyChop here. There is a lot of false positives on this one in my concern. Other examples that rise this issue : if (a == null) {
a = initA();
if (a == null) {
throw new Exception()
} else {
doSomething
} public int compare(MyObject o1, MyObject o2) {
if (o1 == null && o2 == null) {
return 0;
} else {
if (o1 == null) {
return -1;
} else if (o2 == null) {
return 1;
} else {
return o1.getSomething().compareTo(o2.getSomething());
}
} if (a != null && a.size() == 1) {
doSomething();
} else {
if (a == null || a.isEmpty()) {
doSomethingElse();
} else {
throw new Exception();
}
} Even if those pieces of code are not the prettiest, I don't think they should raise this issue. |
Describe the bug
EC2 shows issues for successive "ifs" even when the condition is not related, even without an else condition, and switch is not applicable.
Also, the rule explanation doesn't even once refer to switch in compliant code examples.
Action suggestions (splitting into several issues might make tracking easier)
if (a == a1) {...} else if (a == a2) {...} else {...}
(at least for Java < 17)To Reproduce
Expected behavior
The issue should only show when there is a proven way to reduce the number of conditionals.
Screenshots
If applicable, add screenshots to help explain your problem.
Software Versions
The text was updated successfully, but these errors were encountered: