Skip to content
This repository was archived by the owner on Oct 3, 2024. It is now read-only.

Commit 9dafdfe

Browse files
Rule S126: "if ... else if" constructs should end with "else" clauses (#239)
1 parent 345151b commit 9dafdfe

File tree

6 files changed

+672
-0
lines changed

6 files changed

+672
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Rules in this category aim to find places in code which have a high chance of be
2424
Code Smells, or maintainability issues, are raised for places of code which might be costly to change in the future. These rules also help to keep the high code quality and readability. And finally some rules report issues on different suspicious code patters.
2525

2626
* Cognitive Complexity of functions should not be too high ([`cognitive-complexity`])
27+
* "if ... else if" constructs should end with "else" clauses ([`elseif-without-else`]) (*disabled*)
2728
* "switch" statements should not have too many "case" clauses ([`max-switch-cases`])
2829
* Collapsible "if" statements should be merged ([`no-collapsible-if`])
2930
* Collection sizes and array length comparisons should make sense ([`no-collection-size-mischeck`])
@@ -46,6 +47,7 @@ Code Smells, or maintainability issues, are raised for places of code which migh
4647
* A "while" loop should be used instead of a "for" loop ([`prefer-while`]) (:wrench: *fixable*)
4748

4849
[`cognitive-complexity`]: ./docs/rules/cognitive-complexity.md
50+
[`elseif-without-else`]: ./docs/rules/elseif-without-else.md
4951
[`generator-without-yield`]: ./docs/rules/generator-without-yield.md
5052
[`max-switch-cases`]: ./docs/rules/max-switch-cases.md
5153
[`no-all-duplicated-branches`]: ./docs/rules/no-all-duplicated-branches.md

docs/rules/elseif-without-else.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# elseif-without-else
2+
3+
This rule applies whenever an `if` statement is followed by one or more `else if` statements; the final `else if` should be followed by an `else` statement.
4+
5+
The requirement for a final `else` statement is defensive programming.
6+
7+
The `else` statement should either take appropriate action or contain a suitable comment as to why no action is taken. This is consistent with the requirement to have a final `default` clause in a `switch` statement.
8+
9+
## Noncompliant Code Example
10+
11+
```javascript
12+
if (x == 0) {
13+
doSomething();
14+
} else if (x == 1) {
15+
doSomethingElse();
16+
}
17+
```
18+
## Compliant Solution
19+
20+
```javascript
21+
if (x == 0) {
22+
doSomething();
23+
} else if (x == 1) {
24+
doSomethingElse();
25+
} else {
26+
throw "Unexpected value for x";
27+
}
28+
```
29+
30+
## See
31+
32+
<ul>
33+
<li> <a href="https://wiki.sei.cmu.edu/confluence/display/c/MSC01-C.+Strive+for+logical+completeness">CERT, MSC01-C.</a> - Strive for logical completeness </li>
34+
<li> <a href="https://wiki.sei.cmu.edu/confluence/display/java/MSC57-J.+Strive+for+logical+completeness">CERT, MSC57-J.</a> - Strive for logical completeness </li>
35+
</ul>

0 commit comments

Comments
 (0)