rspec/rules/S4524/go/rule.adoc
2020-12-23 14:59:06 +01:00

40 lines
936 B
Plaintext

``switch`` can contain a ``default`` clause for various reasons: to handle unexpected values, to show that all the cases were properly considered.
For readability purpose, to help a developer to quickly find the default behavior of a ``switch`` statement, it is recommended to put the ``default`` clause at the beginning or the end of the ``switch`` statement. This rule raises an issue if the ``default`` clause is not the first or the last one of the ``switch``'s cases.
== Noncompliant Code Example
----
switch tag {
case 0, 1, 2, 3:
foo()
default: // Noncompliant; default case should be the first or last one
qix()
case 4, 5, 6, 7:
bar()
}
----
== Compliant Solution
----
switch tag {
default:
qix() // Compliant; default is the first one
case 0, 1, 2, 3:
foo()
case 4, 5, 6, 7:
bar()
}
----
----
switch tag {
case 0, 1, 2, 3:
foo()
case 4, 5, 6, 7:
bar()
default:
qix() // Compliant; default is the last one
}
----