1

The following code is giving me a Cyclomatic complexity of 35.

public void updateGUIInProgress(StatusLabelDTO statusLabelDTO) {
    Display.getDefault().asyncExec(new Runnable() {

        @Override
        public void run() {
            label1.setText(statusLabelDTO.getIterationStr());
            label2.setMaximum(statusLabelDTO.getTotalCount());
            label3.setSelection(statusLabelDTO.getExeIndex());
            label4.setText(statusLabelDTO.getStepStr());
            label5.setText(statusLabelDTO.getPassStr());
            label6.setText(statusLabelDTO.getFailStr());
        }
    });
}

I tried moving all the setting lines to a method. However it did not work for me. How can I reduce the complexity?

Hulk
  • 6,399
  • 1
  • 30
  • 52
  • By most definitions of Cyclomatic Complexity that I know of, 35 is way too high for this method. Which tool are you using? Is it possible that the metric is outdated/refers to some other code? – Hulk Jan 22 '19 at 08:46
  • we are using source monitor V3.3 tool –  Jan 22 '19 at 09:04
  • @Pooja this one: http://www.campwoodsw.com/sourcemonitor.html? That says `Version 3.3 [July 24, 2012]` - perhaps you want to update to a more recent version? – Hulk Jan 22 '19 at 09:39
  • 1
    Perhaps you are facing the following Bug: `199 Java: not parsing methods in anonymous classes properly. March 19, 2013; Fix in V3.4.0.283`. See their [List of Bugs](http://www.campwoodsw.com/smbugs.html) – Hulk Jan 22 '19 at 09:45
  • I appreciate the quick comeback! – GhostCat Jan 22 '19 at 11:58
  • @Hulk yes, you are right. I was not aware about the version before, cause some other team is used to test code. Thankyou:) –  Jan 23 '19 at 04:26

1 Answers1

2

Without knowing the tool that computes the cyclomatic complexity for you, that is really hard. In the end, your code isn't doing much.

You could refactor it like this:

someDisplayYouAcquiredPreviously.asyncExec(new SpecificRunnable());

Obviously doing so requires you to store that Display object before, and it also requires you to use a distinct named class instead of that anonymous inner class.

But the real answer is: look into your tooling. Wikipedia tells us about cyclomatic complexity:

The cyclomatic complexity of a section of source code is the number of linearly independent paths within it. For instance, if the source code contained no control flow statements (conditionals or decision points), the complexity would be 1, since there would be only a single path through the code.

There is exactly one path through your code, so that value should be 1, not 35.

In other words: your tool seems to compute wrong numbers, probably it doesn't understand java syntax. Therefore the real answer is to step back and look at the setup / tooling you are using.

And yes, I find it most likely that user Hulk is correct, and you should upgrade your tool to a newer version, as this is probably bug 199 in the "sourcecode monitor" application.

And hint: you know want to step back and check the versions of all your other tools in your environment. It is one thing to be conservative about updates, but using 7 year old versions isn't "conservative" any more, that is grossly negligent.

GhostCat
  • 137,827
  • 25
  • 176
  • 248