Tuesday, September 18, 2012

Cyclomatic Complexity and What it Means to Your Java Code

Cyclomatic complexity, put simply, is the number of linear paths through your code. You can measure the cyclomatic complexity within an individual method or calculate it throughout your application. The greater the number of paths throughout your code, the more complicated it is, and as a result, the more likely it is to produce a run-time error. Also, the greater the complexity, the greater the number of test cases that will be required to test all paths throughout your code.


It can be a charge to write complicated code, but you should always strive to keep your code as simple as possible, while still delivering the desired outcome. The problem I run into is that unless it's right in front of me as I'm developing, it's easy to get caught up writing code without paying much attention to the cyclomatic complexity. I'm always looking for ways to automate tasks and simplify my work. So, to make my life easier, the Eclipse metrics plugin comes in handy to continuously verify that my code isn't running away with complexity. Let's look at this plug-in in action and see how it can help to remind us to keep our code as simple as possible, without over-engineering the solution with unnecessary complexity.

To begin with, follow these steps to install the Eclipse metrics plugin:

1. From within your Eclipse IDE, while in your Java perspective, click on the Help menu and then click "Install New Software". In the "Work With" text area, type http://metrics.sourceforge.net/update, select the Metrics Plugin checkbox and click the Next button.

2. Click on the Window menu, select "Show View", and then click "Metrics View".

 3. Right click on your Java project and click on Properties. Select the option on the left pane that says "Metrics". Make sure that the Enable Metrics check box on the right pane is selected and click the OK button.

4. You'll notice that a new Window will appear in your Eclipse IDE. Select the tab for that new window and you'll see a plethora of code metrics listed. Scroll through that list and you'll see that one of the options is McCabe's Cyclomatic Complexity.

To see how you can measure the complexity of your code, look at the following simple test application that contains a single if-then-else statement. The cyclomatic complexity is 2 because there are 2 paths through the source code.
Let's add a loop outside of our if-then-else statement and we'll see that our cyclomatic complexity is now 3 instead of 2.

There are varying beliefs on the accepted levels of cyclomatic complexity. However, the main thing to remember is to keep it as low as possible, while still achieving the purpose of your code. I like to keep this plugin visible while I'm working on applications as a reminder to do just that. The greater the complexity, the more unit tests you will need to write to cover your code, as well as the greater the chance of a runtime error. Keep it simple and try not to over-engineer a solution where possible.