Rule Definition
The 'else' part is executed when none of the when part is executed. Having no else keyword means that there is no backup. The cases that are "impossible" today are those most likely to be the causes of untraceable bugs in the future.
Software should follow the principles of structured programming or else it degrades comprehensibility.
Remediation
Add else block to every when statement or expression
Violation Code Sample
fun main(args: Array<String>){
println("hello")
val x = 20
when (x) {
1 -> print("x == 1")
2 -> print("x == 2")
}
}
Fixed Code Sample
fun main(args: Array<String>){
println("hello")
val x = 20
when (x) {
1 -> print("x == 1")
2 -> print("x == 2")
else -> println("x is neither 1 not 2")
}
}
Reference
CISQ rule: ASCMM-MNT-1.
Related Technologies
Technical Criterion
PCI-DSS4-Requirement-6.2.4 - Software engineering techniques or other methods are defined and in use by software development personnel to prevent or mitigate common software attacks and related vulnerabilities
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.