1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- Generated by Apache Maven Doxia Site Renderer 1.4 at 18 Feb 2015 -->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>CodeNarc - CodeNarc - Size and Complexity Rules</title>
<style type="text/css" media="all">
@import url("./css/maven-base.css");
@import url("./css/maven-theme.css");
@import url("./css/site.css");
</style>
<link rel="stylesheet" href="./css/print.css" type="text/css" media="print" />
<meta name="Date-Revision-yyyymmdd" content="20150218" />
<meta http-equiv="Content-Language" content="en" />
</head>
<body class="composite">
<div id="banner">
<a href="./" id="bannerLeft">
<img src="images/codenarc-logo.png" alt="CodeNarc" />
</a>
<a href="http://github.com/CodeNarc" id="bannerRight">
<img src="images/forkme_right_red_aa0000.png" alt="Fork me on GitHub" />
</a>
<div class="clear">
<hr/>
</div>
</div>
<div id="breadcrumbs">
<div class="xleft">
<span id="publishDate">Last Published: 18 Feb 2015</span>
| <span id="projectVersion">Version: 0.23</span>
</div>
<div class="xright">
</div>
<div class="clear">
<hr/>
</div>
</div>
<div id="leftColumn">
<div id="navcolumn">
<h5>General</h5>
<ul>
<li class="none">
<a href="index.html" title="Home">Home</a>
</li>
<li class="none">
<a href="https://sourceforge.net/project/showfiles.php?group_id=250145" class="externalLink" title="Downloads">Downloads</a>
</li>
<li class="none">
<a href="apidocs/index.html" title="Javadocs">Javadocs</a>
</li>
<li class="none">
<a href="http://sourceforge.net/mail/?group_id=250145" class="externalLink" title="Mailing Lists">Mailing Lists</a>
</li>
<li class="none">
<a href="http://sourceforge.net/tracker/?group_id=250145" class="externalLink" title="Bug Tracker">Bug Tracker</a>
</li>
<li class="none">
<a href="http://sourceforge.net/projects/codenarc" class="externalLink" title="SourceForge Project">SourceForge Project</a>
</li>
<li class="none">
<a href="http://github.com/CodeNarc" class="externalLink" title="GitHub Project">GitHub Project</a>
</li>
</ul>
<h5>Running</h5>
<ul>
<li class="none">
<a href="codenarc-ant-task.html" title="Ant Task Usage">Ant Task Usage</a>
</li>
<li class="none">
<a href="codenarc-command-line.html" title="Command-Line">Command-Line</a>
</li>
<li class="none">
<a href="codenarc-run-as-a-test.html" title="Run as a Test">Run as a Test</a>
</li>
<li class="none">
<a href="codenarc-other-tools-frameworks.html" title="Other Tools/Frameworks">Other Tools/Frameworks</a>
</li>
</ul>
<h5>Using</h5>
<ul>
<li class="none">
<a href="codenarc-creating-ruleset.html" title="Creating a RuleSet">Creating a RuleSet</a>
</li>
<li class="none">
<a href="codenarc-creating-rule.html" title="Creating a Rule">Creating a Rule</a>
</li>
<li class="none">
<a href="codenarc-configuring-rules.html" title="Configuring Rules">Configuring Rules</a>
</li>
<li class="none">
<a href="StarterRuleSet-AllRulesByCategory.groovy.txt" title="Starter RuleSet (All)">Starter RuleSet (All)</a>
</li>
</ul>
<h5>Report Types</h5>
<ul>
<li class="none">
<a href="codenarc-HtmlReportWriter.html" title="HTML Report">HTML Report</a>
</li>
<li class="none">
<a href="codenarc-XmlReportWriter.html" title="XML Report">XML Report</a>
</li>
<li class="none">
<a href="codenarc-TextReportWriter.html" title="Text and IDE Reports">Text and IDE Reports</a>
</li>
</ul>
<h5>Sample Reports</h5>
<ul>
<li class="none">
<a href="SampleCodeNarcHtmlReport.html" title="Sample HTML Report">Sample HTML Report</a>
</li>
<li class="none">
<a href="SampleCodeNarcXmlReport.xml" title="Sample XML Report">Sample XML Report</a>
</li>
</ul>
<h5>Rules</h5>
<ul>
<li class="none">
<a href="codenarc-rule-index.html" title="Rule Index">Rule Index</a>
</li>
<li class="none">
<a href="codenarc-rules-basic.html" title="Basic Rules">Basic Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-braces.html" title="Braces Rules">Braces Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-concurrency.html" title="Concurrency Rules">Concurrency Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-convention.html" title="Convention Rules">Convention Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-design.html" title="Design Rules">Design Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-dry.html" title="DRY Rules">DRY Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-enhanced.html" title="Enhanced Rules">Enhanced Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-exceptions.html" title="Exceptions Rules">Exceptions Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-formatting.html" title="Formatting Rules">Formatting Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-generic.html" title="Generic Rules">Generic Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-grails.html" title="Grails Rules">Grails Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-groovyism.html" title="Groovyism Rules">Groovyism Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-imports.html" title="Imports Rules">Imports Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-jdbc.html" title="JDBC Rules">JDBC Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-junit.html" title="JUnit Rules">JUnit Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-logging.html" title="Logging Rules">Logging Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-naming.html" title="Naming Rules">Naming Rules</a>
</li>
<li class="none">
<strong>Size/Complexity Rules</strong>
</li>
<li class="none">
<a href="codenarc-rules-security.html" title="Security Rules">Security Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-serialization.html" title="Serialization Rules">Serialization Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-unnecessary.html" title="Unnecessary Rules">Unnecessary Rules</a>
</li>
<li class="none">
<a href="codenarc-rules-unused.html" title="Unused Rules">Unused Rules</a>
</li>
</ul>
<h5>Developing</h5>
<ul>
<li class="none">
<a href="codenarc-developer-guide.html" title="Developer Guide">Developer Guide</a>
</li>
</ul>
<h5>Project Documentation</h5>
<ul>
<li class="collapsed">
<a href="project-info.html" title="Project Information">Project Information</a>
</li>
<li class="collapsed">
<a href="project-reports.html" title="Project Reports">Project Reports</a>
</li>
</ul>
<a href="http://sourceforge.net" title="Hosted on SourceForge.net" class="poweredBy">
<img class="poweredBy" alt="Hosted on SourceForge.net" src="http://sflogo.sourceforge.net/sflogo.php?group_id=208647&type=2" />
</a>
<a href="http://maven.apache.org" title="Build with Maven 2" class="poweredBy">
<img class="poweredBy" alt="Build with Maven 2" src="images/logos/maven-feather.png" />
</a>
</div>
</div>
<div id="bodyColumn">
<div id="contentBox">
<div class="section">
<h2>Size and Complexity Rules ("<i>rulesets/size.xml</i>")<a name="Size_and_Complexity_Rules_rulesetssize.xml"></a></h2><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<div class="section">
<h3><a name="AbcComplexity">AbcComplexity</a> Rule<a name="AbcComplexity_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<table border="1" class="bodyTable">
<tr class="a">
<td align="left"><b>NOTE:</b> This rule has been <b>DEPRECATED</b>, and disabled by default (i.e., by setting its <i>enabled</i> property to <i>false</i>). Use the <a href="#AbcMetric">AbcMetric</a> rule instead.</td></tr></table>
<p>Calculates the <i>ABC</i> metric for methods/classes and checks against configured threshold values.</p>
<p>The <b>maxMethodComplexity</b> property holds the threshold value for the ABC complexity value (score) for each method. If this value is non-zero, a method with a complexity value greater than this value is considered a violation.</p>
<p>The <b>maxClassAverageMethodComplexity</b> property holds the threshold value for the average ABC complexity value (score) for each class. If this value is non-zero, a class with an average complexity value greater than this value is considered a violation.</p>
<p>This rule treats "closure fields" as methods. If a class field is initialized to a Closure (ClosureExpression), then that Closure is analyzed and checked just like a method.</p>
<table border="1" class="bodyTable">
<tr class="a">
<td align="left"><b>Property</b></td>
<td align="left"><b>Description</b></td>
<td align="left"><b>Default Value</b></td></tr>
<tr class="b">
<td align="left">maxMethodComplexity</td>
<td align="left">The maximum <i>ABC complexity</i> value (score) allowed for a single method (or "closure field"). If zero or <i>null</i>, then do not check method-level complexity.</td>
<td align="left">60</td></tr>
<tr class="a">
<td align="left">maxClassAverageMethodComplexity</td>
<td align="left">The maximum <i>ABC complexity</i> average value (score) allowed for a class, calculated as the average complexity of its methods or "closure fields". If zero or <i>null</i>, then do not check class-level average complexity.</td>
<td align="left">60</td></tr>
<tr class="b">
<td align="left">maxClassComplexity</td>
<td align="left">The maximum <i>ABC complexity</i> value (score) allowed for a class, calculated as the total complexity of its methods or "closure fields". If zero or <i>null</i>, then do not check class-level complexity.</td>
<td align="left">0</td></tr>
<tr class="a">
<td align="left">ignoreMethodNames</td>
<td align="left">Specifies one or more (comma-separated) method names that that should not cause a rule violation. The names may optionally contain wildcards (*,?). Note that the ignored methods still contribute to the class complexity value.</td>
<td align="left"><tt>null</tt></td></tr></table>
<div class="section">
<h4>ABC Size/Complexity Metric Calculation Rules<a name="ABC_SizeComplexity_Metric_Calculation_Rules"></a></h4><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>The <i>ABC complexity</i> value (score) is calculated as follows: The <i>ABC</i> metric measures size/complexity by counting the number of Assignments (A), Branches (B) and Conditions (C) and assigns a single numerical score calculated as:</p>
<p><tt> |ABC| = sqrt((A*A)+(B*B)+(C*C)) </tt></p>
<p>The <i>ABC Metric</i> calculation rules for Groovy:</p>
<ul>
<li>Add one to the <i>assignment</i> count for each occurrence of an assignment operator, excluding constant declarations: = *= /= %= += <b>= </b>= &= |= ^= >>>=</li>
<li>Add one to the <i>assignment</i> count for each occurrence of an increment or decrement operator (prefix or postfix): ++ --</li>
<li>Add one to the <i>branch</i> count for each function call or class method call.</li>
<li>Add one to the <i>branch</i> count for each occurrence of the new operator.</li>
<li>Add one to the <i>condition</i> count for each use of a conditional operator: == != <i>= </i>= <i> </i> <i>=</i> =~ ==~</li>
<li>Add one to the <i>condition</i> count for each use of the following keywords: else case default try catch ?</li>
<li>Add one to the <i>condition</i> count for each unary conditional expression.</li></ul></div>
<div class="section">
<h4>Notes<a name="Notes"></a></h4><!-- ~~~~~~~ -->
<ul>
<li>See the <a class="externalLink" href="http://www.softwarerenovation.com/ABCMetric.pdf">ABC Metric specification</a></li>
<li>See the <a class="externalLink" href="http://jakescruggs.blogspot.com/2008/08/whats-good-flog-score.html">Blog post</a> describing guidelines for interpreting an ABC score</li>
<li>This <a class="externalLink" href="https://servicios.excentia.es/confluence/display/QAX/SONAR+ABC+Metric+Plugin">(Spanish) blog post</a> about the eXcentia Sonar ABC Metric Plugin (for Java) includes a table of risk classifications for ABC scores for both methods and classes.</li>
<li>See the <a class="externalLink" href="http://gmetrics.sourceforge.net/gmetrics-AbcMetric.html">GMetrics ABC metric</a>. This includes a discussion of guidelines for interpreting <i>ABC</i> scores.</li>
<li>This rule requires Groovy 1.6 (or later).</li>
<li>This rule requires the GMetrics jar on the classpath. See <a class="externalLink" href="http://gmetrics.sourceforge.net/">GMetrics</a>.</li></ul></div></div>
<div class="section">
<h3><a name="AbcMetric">AbcMetric</a> Rule<a name="AbcMetric_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>Calculates the <i>ABC</i> size metric for methods/classes and checks against configured threshold values.</p>
<p>The <b>maxMethodAbcScore</b> property holds the threshold value for the ABC score for each method. If this value is non-zero, a method with an ABC score greater than this value is considered a violation. The value does not have to be an integer (e.g., 1.7 is allowed).</p>
<p>The <b>maxClassAverageMethodAbcScore</b> property holds the threshold value for the average ABC score for each class. If this value is non-zero, a class with an average ABC score value greater than this value is considered a violation. The value does not have to be an integer.</p>
<p>The <b>maxClassAbcScore</b> property holds the threshold value for the total ABC score value for each class. If this value is non-zero, a class with a total ABC score greater than this value is considered a violation. The value does not have to be an integer.</p>
<p>This rule treats "closure fields" as methods. If a class field is initialized to a Closure (ClosureExpression), then that Closure is analyzed and checked just like a method.</p>
<table border="1" class="bodyTable">
<tr class="a">
<td align="left"><b>Property</b></td>
<td align="left"><b>Description</b></td>
<td align="left"><b>Default Value</b></td></tr>
<tr class="b">
<td align="left">maxMethodAbcScore</td>
<td align="left">The maximum <i>ABC</i> score allowed for a single method (or "closure field"). If zero or <i>null</i>, then do not check method-level scores.</td>
<td align="left">60</td></tr>
<tr class="a">
<td align="left">maxClassAverageMethodAbcScore</td>
<td align="left">The maximum average <i>ABC</i> score allowed for a class, calculated as the average score of its methods or "closure fields". If zero or <i>null</i>, then do not check class-level average scores.</td>
<td align="left">60</td></tr>
<tr class="b">
<td align="left">maxClassAbcScore</td>
<td align="left">The maximum <i>ABC</i> score allowed for a class, calculated as the total ABC score of its methods or "closure fields". If zero or <i>null</i>, then do not check class-level scores.</td>
<td align="left">0</td></tr>
<tr class="a">
<td align="left">ignoreMethodNames</td>
<td align="left">Specifies one or more (comma-separated) method names that that should not cause a rule violation. The names may optionally contain wildcards (*,?). Note that the ignored methods still contribute to the class complexity value.</td>
<td align="left"><tt>null</tt></td></tr></table>
<div class="section">
<h4>ABC Size Metric Calculation Rules<a name="ABC_Size_Metric_Calculation_Rules"></a></h4><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>The <i>ABC</i> score is calculated as follows: The <i>ABC</i> metric measures size by counting the number of Assignments (A), Branches (B) and Conditions (C) and assigns a single numerical score calculated as:</p>
<p><tt> |ABC| = sqrt((A*A)+(B*B)+(C*C)) </tt></p>
<p>The <i>ABC Metric</i> calculation rules for Groovy:</p>
<ul>
<li>Add one to the <i>assignment</i> count for each occurrence of an assignment operator, excluding constant declarations: = *= /= %= += <b>= </b>= &= |= ^= >>>=</li>
<li>Add one to the <i>assignment</i> count for each occurrence of an increment or decrement operator (prefix or postfix): ++ --</li>
<li>Add one to the <i>branch</i> count for each function call or class method call.</li>
<li>Add one to the <i>branch</i> count for each occurrence of the new operator.</li>
<li>Add one to the <i>condition</i> count for each use of a conditional operator: == != <i>= </i>= <i> </i> <i>=</i> =~ ==~</li>
<li>Add one to the <i>condition</i> count for each use of the following keywords: else case default try catch ?</li>
<li>Add one to the <i>condition</i> count for each unary conditional expression.</li></ul></div>
<div class="section">
<h4>Notes<a name="Notes"></a></h4><!-- ~~~~~~~ -->
<ul>
<li>See the <a class="externalLink" href="http://www.softwarerenovation.com/ABCMetric.pdf">ABC Metric specification</a></li>
<li>See the <a class="externalLink" href="http://jakescruggs.blogspot.com/2008/08/whats-good-flog-score.html">Blog post</a> describing guidelines for interpreting an ABC score</li>
<li>This <a class="externalLink" href="https://servicios.excentia.es/confluence/display/QAX/SONAR+ABC+Metric+Plugin">(Spanish) blog post</a> about the eXcentia Sonar ABC Metric Plugin (for Java) includes a table of risk classifications for ABC scores for both methods and classes.</li>
<li>See the <a class="externalLink" href="http://gmetrics.sourceforge.net/gmetrics-AbcMetric.html">GMetrics ABC metric</a>. This includes a discussion of guidelines for interpreting <i>ABC</i> scores.</li>
<li>This rule requires Groovy 1.6 (or later).</li>
<li>This rule requires the GMetrics jar on the classpath. See <a class="externalLink" href="http://gmetrics.sourceforge.net/">GMetrics</a>.</li></ul></div></div>
<div class="section">
<h3><a name="ClassSize">ClassSize</a> Rule<a name="ClassSize_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>Checks if the size of a class exceeds the number of lines specified by the <b>maxLines</b> property.</p>
<table border="1" class="bodyTable">
<tr class="a">
<td align="left"><b>Property</b></td>
<td align="left"><b>Description</b></td>
<td align="left"><b>Default Value</b></td></tr>
<tr class="b">
<td align="left">maxLines</td>
<td align="left">The maximum number of lines allowed in a class definition.</td>
<td align="left">1000</td></tr></table></div>
<div class="section">
<h3><a name="CrapMetric">CrapMetric</a> Rule<a name="CrapMetric_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>Calculates the <a class="externalLink" href="http://www.artima.com/weblogs/viewpost.jsp?thread=210575">C.R.A.P.</a> (Change Risk Anti-Patterns) metric score for methods/classes and checks against configured threshold values.</p>
<p>The <i>CRAP</i> metric score is based on the <i>cyclomatic complexity</i> and test coverage for individual methods. A method with a <i>CRAP</i> value greater than the <b>maxMethodCrapScore</b> property causes a violation. Likewise, a class that has an (average method) <i>CRAP</i> value greater than the <b>maxClassAverageMethodCrapScore</b> property causes a violation.</p>
<p><b>NOTE:</b> This rule requires the <b>GMetrics</b>[3] jar, version 0.5 (or later), on the classpath, as well as a <b>Cobertura</b>[4]-[6] XML coverage file. If either of these prerequisites is not available, this rule logs a warning messages and exits (i.e., does nothing).</p>
<p>The <b>maxMethodCrapScore</b> property holds the threshold value for the CRAP value for each method. If this value is non-zero, a method with a cyclomatic complexity value greater than this value is considered a violation.</p>
<p>The <b>maxClassAverageMethodCrapScore</b> property holds the threshold value for the average CRAP value for each class. If this value is non-zero, a class with an average cyclomatic complexity value greater than this value is considered a violation.</p>
<p>NOTE: This rule does NOT treat <i>closure fields</i> as methods (unlike some of the other size/complexity rules).</p>
<table border="1" class="bodyTable">
<tr class="a">
<td align="left"><b>Property</b></td>
<td align="left"><b>Description</b></td>
<td align="left"><b>Default Value</b></td></tr>
<tr class="b">
<td align="left">coberturaXmlFile</td>
<td align="left">The path to the Cobertura XML coverage file for the Groovy code By default, the path is relative to the classpath. But the path may be optionally prefixed by any of the valid java.net.URL prefixes, such as "file:" (to load from a relative or absolute path on the filesystem), or "http:". This property is REQUIRED.</td>
<td align="left"><tt>null</tt></td></tr>
<tr class="a">
<td align="left">maxMethodCrapScore</td>
<td align="left">The maximum <i>CRAP</i> metric value allowed for a single method. If zero or <i>null</i>, then do not check method-level complexity.</td>
<td align="left">30</td></tr>
<tr class="b">
<td align="left">maxClassAverageMethodCrapScore</td>
<td align="left">The maximum <i>CRAP</i> average metric value allowed for a class, calculated as the average CRAP value of its methods. If zero or <i>null</i>, then do not check the average class-level CRAP value.</td>
<td align="left">30</td></tr>
<tr class="a">
<td align="left">maxClassCrapScore</td>
<td align="left">The maximum total <i>CRAP</i> metric value allowed for a class, calculated as the total CRAP value of its methods. If zero or <i>null</i>, then do not check class-level CRAP value.</td>
<td align="left">0</td></tr>
<tr class="b">
<td align="left">ignoreMethodNames</td>
<td align="left">Specifies one or more (comma-separated) method names that that should not cause a rule violation. The names may optionally contain wildcards (*,?). Note that the ignored methods still contribute to the class complexity value.</td>
<td align="left"><tt>null</tt></td></tr></table></div>
<div class="section">
<h3>CRAP Formula<a name="CRAP_Formula"></a></h3><!-- ~~~~~~~~~~~~~ -->
<p>Given a Groovy method m, C.R.A.P. for m is calculated as follows:</p>
<div class="source">
<pre> C.R.A.P.(m) = comp(m)^2 * (1 - cov(m)/100)^3 + comp(m)
</pre></div>
<p>Where <b>comp(m)</b> is the <i>cyclomatic complexity</i> of method m, and <b>cov(m)</b> is the test code coverage provided by automated tests.</p></div>
<div class="section">
<h3>References<a name="References"></a></h3><!-- ~~~~~~~~~~ -->
<ul>
<li><b>[1]</b> The original 2007 <a class="externalLink" href="http://www.artima.com/weblogs/viewpost.jsp?thread=210575">blog post</a> that defined the <b>CRAP</b> metric.</li>
<li><b>[2]</b> A 2011 <a class="externalLink" href="http://googletesting.blogspot.com/2011/02/this-code-is-crap.html">blog post</a> from Alberto Savoia (the co-creator of the <b>CRAP</b> metric with Bob Evans), describing the formula, the motivation, and the <b>CRAP4J</b> tool for calculating <b>CRAP</b> score for Java code.</li>
<li><b>[3]</b> The <a class="externalLink" href="http://gmetrics.sourceforge.net/gmetrics-CrapMetric.html">GMetrics CRAP Metric</a>.</li>
<li><b>[4]</b> <a class="externalLink" href="http://cobertura.sourceforge.net/">Cobertura</a> -- <i>Cobertura is a free Java tool that calculates the percentage of code accessed by tests. It can be used to identify which parts of your Java program are lacking test coverage.</i></li>
<li><b>[5]</b> <a class="externalLink" href="http://cobertura.sourceforge.net/anttaskreference.html">Cobertura Ant Task Reference</a></li>
<li><b>[6]</b> <a class="externalLink" href="http://mojo.codehaus.org/cobertura-maven-plugin/index.html">Cobertura Maven Plugin</a></li></ul></div>
<div class="section">
<h3><a name="CyclomaticComplexity">CyclomaticComplexity</a> Rule<a name="CyclomaticComplexity_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>Calculates the <i>Cyclomatic Complexity</i> for methods/classes and checks against configured threshold values.</p>
<p>The <b>maxMethodComplexity</b> property holds the threshold value for the cyclomatic complexity value for each method. If this value is non-zero, a method with a cyclomatic complexity value greater than this value is considered a violation.</p>
<p>The <b>maxClassAverageMethodComplexity</b> property holds the threshold value for the average cyclomatic complexity value for each class. If this value is non-zero, a class with an average cyclomatic complexity value greater than this value is considered a violation.</p>
<p>This rule treats "closure fields" as methods. If a class field is initialized to a Closure (ClosureExpression), then that Closure is analyzed and checked just like a method.</p>
<table border="1" class="bodyTable">
<tr class="a">
<td align="left"><b>Property</b></td>
<td align="left"><b>Description</b></td>
<td align="left"><b>Default Value</b></td></tr>
<tr class="b">
<td align="left">maxMethodComplexity</td>
<td align="left">The maximum <i>cyclomatic complexity</i> value allowed for a single method (or "closure field"). If zero or <i>null</i>, then do not check method-level complexity.</td>
<td align="left">20</td></tr>
<tr class="a">
<td align="left">maxClassAverageMethodComplexity</td>
<td align="left">The maximum average <i>cyclomatic complexity</i> value allowed for a class, calculated as the average complexity of its methods or "closure fields". If zero or <i>null</i>, then do not check average class-level complexity.</td>
<td align="left">20</td></tr>
<tr class="b">
<td align="left">maxClassComplexity</td>
<td align="left">The maximum total <i>cyclomatic complexity</i> value allowed for a class, calculated as the total complexity of its methods or "closure fields". If zero or <i>null</i>, then do not check total class-level complexity.</td>
<td align="left">0</td></tr>
<tr class="a">
<td align="left">ignoreMethodNames</td>
<td align="left">Specifies one or more (comma-separated) method names that that should not cause a rule violation. The names may optionally contain wildcards (*,?). Note that the ignored methods still contribute to the class complexity value.</td>
<td align="left"><tt>null</tt></td></tr></table>
<div class="section">
<h4>Cyclomatic Complexity Metric Calculation Rules<a name="Cyclomatic_Complexity_Metric_Calculation_Rules"></a></h4><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>The <i>cyclomatic complexity</i> value is calculated as follows:</p>
<p><i>Start with a initial (default) value of one (1). Add one (1) for each occurrence of each of the following:</i></p>
<ul>
<li><tt>if</tt> statement</li>
<li><tt>while</tt> statement</li>
<li><tt>for</tt> statement</li>
<li><tt>case</tt> statement</li>
<li><tt>catch</tt> statement</li>
<li><tt>&&</tt> and <tt>||</tt> boolean operations</li>
<li><tt>?:</tt> ternary operator and <tt>?:</tt> <i>Elvis</i> operator.</li>
<li><tt>?.</tt> null-check operator</li></ul></div>
<div class="section">
<h4>Notes<a name="Notes"></a></h4><!-- ~~~~~~~ -->
<ul>
<li>See the <a class="externalLink" href="http://en.wikipedia.org/wiki/Cyclomatic_complexity">Cyclomatic Complexity Wikipedia entry</a></li>
<li>See the <a class="externalLink" href="http://www.literateprogramming.com/mccabe.pdf">original paper describing Cyclomatic Complexity</a></li>
<li>See the <a class="externalLink" href="http://gmetrics.sourceforge.net/gmetrics-CyclomaticComplexityMetric.html">GMetrics Cyclomatic Complexity metric</a>. This includes a discussion of guidelines for interpreting <i>cyclomatic complexity</i> values.</li>
<li>This rule requires Groovy 1.6 (or later).</li>
<li>This rule requires the GMetrics jar on the classpath. See <a class="externalLink" href="http://gmetrics.sourceforge.net/">GMetrics</a>.</li></ul></div></div>
<div class="section">
<h3><a name="MethodCount">MethodCount</a> Rule<a name="MethodCount_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.11</i></p>
<p>Checks if the number of methods within a class exceeds the number of lines specified by the <b>maxMethod</b> property.</p>
<p>A class with too many methods is probably a good suspect for refactoring, in order to reduce its complexity and find a way to have more fine grained objects.</p>
<table border="1" class="bodyTable">
<tr class="a">
<td align="left"><b>Property</b></td>
<td align="left"><b>Description</b></td>
<td align="left"><b>Default Value</b></td></tr>
<tr class="b">
<td align="left">maxMethods</td>
<td align="left">The maximum number of methods allowed in a class definition.</td>
<td align="left">30</td></tr></table></div>
<div class="section">
<h3><a name="MethodSize">MethodSize</a> Rule<a name="MethodSize_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>Checks if the size of a method exceeds the number of lines specified by the <b>maxLines</b> property.</p>
<table border="1" class="bodyTable">
<tr class="a">
<td align="left"><b>Property</b></td>
<td align="left"><b>Description</b></td>
<td align="left"><b>Default Value</b></td></tr>
<tr class="b">
<td align="left">maxLines</td>
<td align="left">The maximum number of lines allowed in a method definition.</td>
<td align="left">100</td></tr>
<tr class="a">
<td align="left">ignoreMethodNames</td>
<td align="left">Specifies one or more (comma-separated) method names that should be ignored (i.e., that should not cause a rule violation). The names may optionally contain wildcards (*,?).</td>
<td align="left"><tt>null</tt></td></tr></table>
<p>Known Limitations:</p>
<ul>
<li>Annotations on a method are included in the size (line count) for that method.</li></ul></div>
<div class="section">
<h3><a name="NestedBlockDepthRule">NestedBlockDepthRule</a> Rule<a name="NestedBlockDepthRule_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>Checks for blocks or closures nested more deeply than a configured maximum number. Blocks include <tt>if</tt>, <tt>for</tt>, <tt>while</tt>, <tt>switch</tt>, <tt>try</tt>, <tt>catch</tt>, <tt>finally</tt> and <tt>synchronized</tt> blocks/statements, as well as closures.</p>
<p>Methods calls, constructor calls, and property access through Builder objects are ignore. For instance, this code does not cause a violation:</p>
<div>
<pre> myBuilder.root {
foo {
bar {
baz {
quix {
qux {
quaxz {
}
}
}
}
}
}
}
</pre></div>
<table border="1" class="bodyTable">
<tr class="a">
<td align="left"><b>Property</b></td>
<td align="left"><b>Description</b></td>
<td align="left"><b>Default Value</b></td></tr>
<tr class="b">
<td align="left">maxNestedBlockDepth</td>
<td align="left">The maximum number of nesting levels. A block or closure nested deeper than that number of levels is considered a violation.</td>
<td align="left">5</td></tr>
<tr class="a">
<td align="left">ignoreRegex</td>
<td align="left">Determines what is a builder call. For instance, closures nested on a method named createBuilder, a property named myBuilder, or a constructor call to object MyBuilder() do not produce violations.</td>
<td align="left">.*(b</td></tr></table></div>
<div class="section">
<h3><a name="ParameterCount">ParameterCount</a> Rule<a name="ParameterCount_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.23</i></p>
<p>Checks if the number of parameters in method/constructor exceeds the number of parameters specified by the maxParameters property.</p>
<p>Example of violations:</p>
<div>
<pre> void someMethod(int arg1, int arg2, int arg3, int arg4, int arg5, int arg6) { // violation
}
class SampleClass {
SampleClass(int arg1, int arg2, int arg3, int arg4, int arg5, int arg6, int arg7) { // violation
}
}
</pre></div>
<table border="1" class="bodyTable">
<tr class="a">
<td align="left"><b>Property</b></td>
<td align="left"><b>Description</b></td>
<td align="left"><b>Default Value</b></td></tr>
<tr class="b">
<td align="left">maxParameters</td>
<td align="left">The maximum number of parameters in method/constructor</td>
<td align="left">5</td></tr></table></div></div>
</div>
</div>
<div class="clear">
<hr/>
</div>
<div id="footer">
<div class="xright">
Copyright © 2015.
All Rights Reserved.
</div>
<div class="clear">
<hr/>
</div>
</div>
</body>
</html>
|