Details
-
Type:
New Feature
-
Status:
Closed
-
Priority:
Major
-
Resolution: Fixed
-
Affects Version/s: None
-
Fix Version/s: JAVASCRIPT-1.2
-
Component/s: JavaScript
-
Labels:None
-
Number of attachments :
Description
- Title
If ... else if constructs shall be terminated with an else clause
- Description
This rule applies whenever an if statement is followed by one or more else if statements; the final else if shall be followed by an else statement. In the case of a simple if statement then the else statement need not be included.
The requirement for a final else statement is defensive programming. 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.
The following code snippet illustrates this rule:
int x = 0; /* This "if" statement is fine, since it has no "else if" clause, no "else" is required */ if (x == 0) { x = 42; } /* This "if" statement is also fine */ if (x == 0) { x = 42; } else { x = -42; } /* This "if" statement is bad, because it has an "else if" but no "else" clause */ if (x == 0) /* Non-Compliant */ { x = 42; } else if (x == 1) { x = -42; }
- Violation message
Add a final else to this If ... else if construct
- Default severity
Major
Part of Sonar Way profile
Issue Links
- is related to
-
SONARPLUGINS-2515
Update the SQALE mapping for new rules
-
Works perfectly.
Could you just remove it from "Sonar Way"? (this rule collides with "Avoid empty block" when the last "else" must intentionally be left empty)