Skip to content

Commit 68cca1f

Browse files
SONARJAVA-6007 Update rule metadata for S2301 (#5408)
1 parent fbc6581 commit 68cca1f

File tree

1 file changed

+15
-18
lines changed
  • sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java

1 file changed

+15
-18
lines changed

sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S2301.html

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,30 @@ <h2>Why is this an issue?</h2>
66
<p>Instead, separate methods should be written.</p>
77
<p>This rule finds methods with a <code>boolean</code> that’s used to determine which path to take through the method.</p>
88
<h3>Noncompliant code example</h3>
9-
<pre>
10-
public String tempt(String name, boolean ofAge) {
11-
if (ofAge) {
12-
offerLiquor(name);
9+
<pre data-diff-id="1" data-diff-type="noncompliant">
10+
public String feed(String name, boolean isHuman) {
11+
if (isHuman) {
12+
// implementation for human
1313
} else {
14-
offerCandy(name);
14+
// implementation for animal
1515
}
1616
}
1717

18-
// ...
19-
public void corrupt() {
20-
tempt("Joe", false); // does this mean not to temp Joe?
21-
}
18+
// Intent is not clear at call site
19+
feed("Max", false); // does this mean not to feed Max?
2220
</pre>
2321
<h3>Compliant solution</h3>
24-
<pre>
25-
public void temptAdult(String name) {
26-
offerLiquor(name);
22+
<pre data-diff-id="1" data-diff-type="compliant">
23+
public void feedHuman(String name) {
24+
offerSushi(name);
2725
}
2826

29-
public void temptChild(String name) {
30-
offerCandy(name);
27+
public void feedAnimal(String name) {
28+
offerCarrot(name);
3129
}
3230

33-
// ...
34-
public void corrupt() {
35-
age &lt; legalAge ? temptChild("Joe") : temptAdult("Joe");
36-
}
31+
// Clear intent at call site
32+
feedHuman("Joe");
33+
feedAnimal("Max");
3734
</pre>
3835

0 commit comments

Comments
 (0)