Information Technology Reference
In-Depth Information
If there aren't any associated test cases, this method is wildly at
risk and you should write some tests immediately. Some may think it's
time to refactor; however, that would break the first rule of refactoring:
Write a test case before you change anything. 6 Once test cases are in
place, you can begin to lower your risk by refactoring. The most effec-
tive way to reduce cyclomatic complexity is to apply the extract
method technique 7 and distribute the complexity into smaller, more
manageable, and therefore more testable, methods. Of course, then the
next step after creating each smaller method is to write inspectors and
tests for it.
In a CI environment, evaluating a method's complexity over time
becomes possible. The first time you run the inspection report, this
method's complexity value can be monitored in subsequent inspec-
tions for any growth (or decline). If you see growth, you can then take
appropriate action.
If a method's CCN value keeps growing, teams can
• Ensure a healthy number of related tests are present to reduce
risk
• Evaluate the possibility of refactoring the method to reduce any
long-term maintenance issues
Because JavaNCSS also reports on documentation trends, these
values can be monitored for organizational standards. The tool reports
single-line comments and multiline comments that occur in addition to
Javadocs. In some software circles, the mere presence of a high count
of inline code comments is an indication of complexity.
JavaNCSS isn't the only tool that can facilitate complexity reporting
in the Java platform. PMD, another open source project that analyzes
Java source files, has a series of rules that report on complexity, includ-
ing cyclomatic complexity, long classes, and long methods. Check-
style is another open source project with similar rules. Both PMD and
Checkstyle have Ant tasks and Maven plug-ins like JavaNCSS.
6. See the section entitled The Value of Self-testing Code in Chapter 4 of Martin
Fowler's book, Refactoring.
7. See www.refactoring.com/catalog/extractMethod.html.
Search WWH ::




Custom Search