4

The C# plugin within Sonar flags methods where the cyclomatic complexity is greater than 10 (CSharpsquid:S1541 - Methods should not be too complex). This is great for 'real' code, but my team finds it annoying when a method containing just a simple 'switch' statement with 5 cases (used to translate one enum type into another enum type) is flagged as being too complex.

Is it possible to suppress the flag/rule on individual methods within the code-base? If so, how?

David Razzetti
  • 167
  • 2
  • 11

1 Answers1

3

You cannot to do this automatically.

Maybe instead of suppress rule you can use IMap. You can create simple class which can translate enums:

public class Enum1ToEnum2Translator {

   private static IMap<Enum1, Enum2> MAP = new Map<Enum1, Enum2>();

   static {
      MAP.add(Enum1.VAL1, Enum2.VAL1);
      MAP.add(Enum1.VAL2, Enum2.VAL2);
      MAP.add(Enum1.VAL3, Enum3.VAL3);
   }

   public Enum2 translate(Enum1 enum) {
      return MAP.get(enum1);
   }
}
agabrys
  • 8,728
  • 3
  • 35
  • 73
  • 1
    Thanks for the reply. The map solution works but other posts elsewhere demonstrate that there is a performance hit when replacing a switch implementation with a map lookup implementation. We are hitting the relevant methods many millions of times per day and need to be a performant as possible. – David Razzetti Sep 25 '15 at 13:56
  • 1
    Ok. So you can only disable it manual from SonarQube Web GUI or by using `pragma` (see [Is there a way to suppress warnings in C# similar to Java's @SuppressWarnings annotation?](http://stackoverflow.com/questions/1378634/is-there-a-way-to-suppress-warnings-in-c-sharp-similar-to-javas-suppresswarnin)) – agabrys Sep 29 '15 at 17:37