Menu

#984 Cyclomatic complexity should treat constructors like methods

PMD-5.0.3
closed
pmd (543)
PMD
3-Major
Bug
2013-04-05
2011-11-23
AlphaBeta
No

The cyclomatic complexity class (net.sourceforge.pmd.rules.CyclomaticComplexity) has three class members: reportLevel, showClassesComplexity, and showMethodsComplexity. The default for the show*Complexity members is true.

When (showMethodsComplexity==false) the net effect should be to show classes whose average complexity meets or exceeds the reportLevel. However, constructors are not being treated as methods, thus constructors with cyclomatic complexities equal to or greather than the reportLevel are displayed instead of just classes whose average complexity is too high.

The fix is easy, wrap the code within the visit(ASTConstructorDeclaration node, Object data) method in the same IF statement [ if ( showMethodsComplexity ) { ... } ] used within the visit(ASTMethodDeclaration node, Object data) method.

Alternatively, add a showConstructorComplexity member to the class and update the constructor visit() method to use its value.

Discussion

  • Romain PELISSE

    Romain PELISSE - 2012-02-24

    Hi,

    Could provide a patch for your fix ? It would be far more easier than trying to re implement based on your description...

    Thanks !

     
  • Andreas Dangel

    Andreas Dangel - 2013-03-16
    • status: pending --> in-progress
    • assigned_to: Andreas Dangel
    • module: --> PMD
    • milestone: --> PMD-5.0.3
    • priority: 5 --> 3-Major
    • type: --> Bug
    • affects_version: -->
     
  • Andreas Dangel

    Andreas Dangel - 2013-03-17
    • status: in-progress --> closed
     

Log in to post a comment.