rspec/rules/S4524/go/rule.adoc

56 lines
1.2 KiB
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
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]