Type: New Feature
Affects Version/s: None
Fix Version/s: 1.2
Number of attachments :
If ... else if constructs shall be terminated with an else clause
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:
- Violation message
Add a final else to this If ... else if construct
- Default severity
Part of Sonar Way profile