Reputation:
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?
Upvotes: 1
Views: 761
Reputation: 140525
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.
Upvotes: 2