1

There's a metrics plugin in eclipse that measures cyclomatic complexity by counting the decisions (independent paths), specifically:

 (if, for, while, do, case, catch and the ?: ternary operator, as well as the && and || conditional logic operators in expressions)

For example, here's a method that would score 14. (1 + each decision path)

  String getMonthName (int month) {
        switch (month) {
                case 0: return "January";
                case 1: return "February";
                case 2: return "March";
                case 3: return "April";
                case 4: return "May";
                case 5: return "June";
                case 6: return "July";
                case 7: return "August";
                case 8: return "September";
                case 9: return "October";
                case 10: return "November";
                case 11: return "December";
                default: throw new IllegalArgumentException();
        }
}

I'm wondering if there's a way to have decisions without the mentioned branches in java, which would undermine the assessment. Basically, I want to have different decisions without the plugin detecting them as such. The metrics would show a lower cyclomatic complexity (independent paths/decisions) than there actually are.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Carlo Otto
  • 47
  • 1
  • 5
  • You want to write an `if` without writing `if`? – Tunaki Oct 23 '15 at 19:16
  • Are you looking to reduce the cyclomatic complexity of your code? – Luiggi Mendoza Oct 23 '15 at 19:19
  • You could probably "fool" the plugin by creating methods like `myOwnIf(boolean b, SomeFunctionalInterface trueBranch, SomeFunctionalInterface falseBranch)` and feeding lamdbas to it instead of using an `if` statement (of course that method would still need one `if`), but that wouldn't have much practical use. Rather, you should optimize your code to decrease cyclomatic complexity properly. – Cinnam Oct 23 '15 at 19:30
  • Well, basically I am trying to prove, or "fool" the plugin by writing a complex method but the plugin is not detecting the decision paths. – Carlo Otto Oct 23 '15 at 19:33
  • Can you please edit your question to explain what do you want/need to achieve and provide relevant code to understand your issue? – Luiggi Mendoza Oct 23 '15 at 19:36
  • @LuiggiMendoza I have added an example. – Carlo Otto Oct 23 '15 at 19:46

1 Answers1

2

In order to reduce cyclomatic complexity, you should decouple your code or refactor it to have a better implementation.

For the example provided, you can use an array:

static final String[] MONTHS = {"January", "February", ... };
static String getMonthName(int month) {
    if (month < 0 || month >= MONTHS.length) {
        throw new IllegalArgumentException(String.format("Month %d doesn't exist", month));
    }
    return MONTHS[month];
}

Even the method above can be decoupled more:

static final String[] MONTHS = {"January", "February", ... };
static String getMonthName(int month) {
    checkBoundaries(month, 0, MONTHS.length - 1, String.format("Month %d doesn't exist", month));
    return MONTHS[month];
}
static void checkBoundaries(int n, int lowerBound, int upperBound, String errorMessage) {
    if (n < lowerBound || n > upperBound) {
        throw new IllegalArgumentException(errorMessage);
    }
}
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332