
Inline adoc files when they are included exactly once. Also fix language tags because this inlining gives us better information on what language the code is written in.
85 lines
1.7 KiB
Plaintext
85 lines
1.7 KiB
Plaintext
== Why is this an issue?
|
|
|
|
When ``++switch++`` statements have large sets of case clauses, it is usually an attempt to map two sets of data. A ``++Dictionary++`` should be used instead to make the code more readable and maintainable.
|
|
|
|
=== Noncompliant code example
|
|
|
|
With a "Maximum number of case" set to 4
|
|
|
|
[source,csharp]
|
|
----
|
|
public class TooManyCase
|
|
{
|
|
public int switchCase(char ch)
|
|
{
|
|
switch(ch) { // Noncompliant
|
|
case 'a':
|
|
return 1;
|
|
case 'b':
|
|
case 'c':
|
|
return 2;
|
|
case 'd':
|
|
return 3;
|
|
case 'e':
|
|
return 4;
|
|
case 'f':
|
|
case 'g':
|
|
case 'h':
|
|
return 5;
|
|
default:
|
|
return 6;
|
|
}
|
|
}
|
|
}
|
|
----
|
|
|
|
=== Compliant solution
|
|
|
|
[source,csharp]
|
|
----
|
|
using System.Collections.Generic;
|
|
|
|
public class TooManyCase
|
|
{
|
|
Dictionary<char, int> matching = new Dictionary<char, int>()
|
|
{
|
|
{'a', 1}, {'b', 2}, {'c', 2}, {'d', 3},
|
|
{'e', 4}, {'f', 5}, {'g', 5}, {'h', 5}
|
|
};
|
|
|
|
public int withDictionary(char ch)
|
|
{
|
|
int value;
|
|
if (this.matching.TryGetValue(ch, out value)) {
|
|
return value;
|
|
} else {
|
|
return 6;
|
|
}
|
|
}
|
|
}
|
|
----
|
|
|
|
=== Exceptions
|
|
|
|
This rule ignores ``++switch++``es over ``++Enum++``s and empty, fall-through cases.
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Consider reworking this "switch" to reduce the number of "case"s from {} to at most {}.
|
|
|
|
|
|
include::../parameters.adoc[]
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
include::../comments-and-links.adoc[]
|
|
endif::env-github,rspecator-view[]
|