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
|
<!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 - Groovyism 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">
<strong>Groovyism Rules</strong>
</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">
<a href="codenarc-rules-size.html" title="Size/Complexity Rules">Size/Complexity Rules</a>
</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>Groovy-ism Rules ("<i>rulesets/groovyism.xml</i>")<a name="Groovy-ism_Rules_rulesetsgroovyism.xml"></a></h2><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>These are rules covering Groovy idiomatic usage, and Groovy-specific bad practices.</p>
<div class="section">
<h3><a name="AssignCollectionSort">AssignCollectionSort</a> Rule<a name="AssignCollectionSort_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>New in CodeNarc 0.15</i></p>
<p>The Collections.sort() method mutates the list and returns the list as a value. If you are assigning the result of sort() to a variable, then you probably don't realize that you're also modifying the original list as well. This is frequently the cause of subtle bugs. This violation is triggered when a sort() method call appears as the right hand side of an assignment, or when it appears as the first method call in a series of chained method calls.</p>
<p>Example of violations:</p>
<div>
<pre> def a = myList.sort()
def b = myList.sort() { it }
def c = myList.sort().findAll { x < 1 }
</pre></div></div>
<div class="section">
<h3><a name="AssignCollectionUnique">AssignCollectionUnique</a> Rule<a name="AssignCollectionUnique_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>New in CodeNarc 0.15</i></p>
<p>The Collections.unique() method mutates the list and returns the list as a value. If you are assigning the result of unique() to a variable, then you probably don't realize that you're also modifying the original list as well. This is frequently the cause of subtle bugs. This violation is triggered when a unique() method call appears as the right hand side of an assignment, or when it appears as the first method call in a series of chained method calls.</p>
<p>Example of violations:</p>
<div>
<pre> def a = myList.unique()
def b = myList.unique() { it }
def c = myList.unique().findAll { x < 1 }
</pre></div></div>
<div class="section">
<h3><a name="ClosureAsLastMethodParameter">ClosureAsLastMethodParameter</a> Rule<a name="ClosureAsLastMethodParameter_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>New in CodeNarc 0.14</i></p>
<p>If a method is called and the last parameter is an inline closure then it can be declared outside of the method call parentheses.</p>
<p>Example of violations:</p>
<div>
<pre> // creates violation: poor Groovy style
[1,2,3].each({ println it })
// no violation
[1,2,3].each { println it }
</pre></div></div>
<div class="section">
<h3><a name="CollectAllIsDeprecated">CollectAllIsDeprecated</a> Rule<a name="CollectAllIsDeprecated_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>New in CodeNarc 0.16</i></p>
<p>The <tt>collectAll</tt> method is deprecated since Groovy 1.8.1. Use <tt>collectNested</tt> instead.</p>
<p>Example of violations:</p>
<div>
<pre>def list = [1, 2, [3, 4, [5, 6]], 7]
list.collectAll { it * 2 } // deprecated
list.collectNested { it * 2 } // replacement
</pre></div></div>
<div class="section">
<h3><a name="ConfusingMultipleReturns">ConfusingMultipleReturns</a> Rule<a name="ConfusingMultipleReturns_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>New in CodeNarc 0.16</i></p>
<p>Multiple return values can be used to set several variables at once. To use multiple return values, the left hand side of the assignment must be enclosed in parenthesis. If not, then you are not using multiple return values, you're only assigning the last element.</p>
<p>Example of violations:</p>
<div>
<pre>def a, b = [1, 2] // bad, b is null
def c, d, e = [1, 2, 3] // bad, c and d are null
class MyClass {
def a, b, c = [1, 2, 3] // bad, a and b are null
}
def x = 1 // ok
def (f, g) = [1, 2] // ok
(a, b, c) = [1, 2, 3] // ok
</pre></div></div>
<div class="section">
<h3><a name="ExplicitArrayListInstantiation">ExplicitArrayListInstantiation</a> Rule<a name="ExplicitArrayListInstantiation_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.11</i></p>
<p>This rule checks for explicit calls to the no-argument constructor of <tt>ArrayList</tt>. In Groovy, it is best to write <tt>new ArrayList() as []</tt>, which creates the same object.</p></div>
<div class="section">
<h3><a name="ExplicitCallToAndMethod">ExplicitCallToAndMethod</a> Rule<a name="ExplicitCallToAndMethod_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.11</i></p>
<p>This rule detects when the <tt>and(Object)</tt> method is called directly in code instead of using the <tt>&</tt> operator. A groovier way to express this: <tt>a.and(b)</tt> is this: <tt>a & b</tt>. This rule can be configured to ignore <tt>this.and(Object)</tt> using the <i>ignoreThisReference</i> property. It defaults to <i>true</i>, so even <tt>and(x)</tt> will not trigger a violation. The default is <i>true</i> because <tt>and</tt> appears commonly in Grails criteria.</p>
<p>This rule also ignores all calls to <tt>super.and(Object)</tt>.</p></div>
<div class="section">
<h3><a name="ExplicitCallToCompareToMethod">ExplicitCallToCompareToMethod</a> Rule<a name="ExplicitCallToCompareToMethod_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.11</i></p>
<p>This rule detects when the <tt>compareTo(Object)</tt> method is called directly in code instead of using the <=>, >, >=, <, and <= operators. A groovier way to express this: <tt>a.compareTo(b)</tt> is this: <tt>a <=> b</tt>, or using the other operators. Here are some other ways to write groovier code:</p>
<div>
<pre> a.compareTo(b) == 0 // can be replaced by: a == b
a.compareTo(b) // can be replaced by: a <=> b
a.compareTo(b) > 0 // can be replaced by: a > b
a.compareTo(b) >= 0 // can be replaced by: a >= b
a.compareTo(b) < 0 // can be replaced by: a < b
a.compareTo(b) <= 0 // can be replaced by: a <= b
</pre></div>
<p>This rule can be configured to ignore <tt>this.compareTo(Object)</tt> using the <i>ignoreThisReference</i> property. It defaults to <tt>false</tt>, so even <tt>compareTo(x)</tt> will trigger a violation.</p>
<p>This rule also ignores all calls to <tt>super.compareTo(Object)</tt>.</p></div>
<div class="section">
<h3><a name="ExplicitCallToDivMethod">ExplicitCallToDivMethod</a> Rule<a name="ExplicitCallToDivMethod_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.11</i></p>
<p>This rule detects when the <tt>div(Object)</tt> method is called directly in code instead of using the <tt>/</tt> operator. A groovier way to express this: <tt>a.div(b)</tt> is this: <tt>a / b</tt>. This rule can be configured to ignore <tt>div.xor(Object)</tt> using the <i>ignoreThisReference</i> property. It defaults to <i>false</i>, so even <tt>div(x)</tt> will trigger a violation.</p>
<p>This rule also ignores all calls to <tt>super.div(Object)</tt>.</p></div>
<div class="section">
<h3><a name="ExplicitCallToEqualsMethod">ExplicitCallToEqualsMethod</a> Rule<a name="ExplicitCallToEqualsMethod_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.11</i></p>
<p>This rule detects when the <tt>equals(Object)</tt> method is called directly in code instead of using the <tt>==</tt> or <tt>!=</tt> operator. A groovier way to express this: <tt>a.equals(b)</tt> is this: <tt>a == b</tt> and a groovier way to express : <tt>!a.equals(b)</tt> is: <tt>a != b</tt>. This rule can be configured to ignore <tt>this.equals(Object)</tt> using the <i>ignoreThisReference</i> property. It defaults to <i>false</i>, so even <tt>equals(x)</tt> will trigger a violation.</p>
<p>This rule also ignores all calls to <tt>super.equals(Object)</tt>.</p></div>
<div class="section">
<h3><a name="ExplicitCallToGetAtMethod">ExplicitCallToGetAtMethod</a> Rule<a name="ExplicitCallToGetAtMethod_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.11</i></p>
<p>This rule detects when the <tt>getAt(Object)</tt> method is called directly in code instead of using the <tt>[]</tt> index operator. A groovier way to express this: <tt>a.getAt(b)</tt> is this: <tt>a[b]</tt>. This rule can be configured to ignore <tt>this.getAt(Object)</tt> using the <i>ignoreThisReference</i> property. It defaults to <i>false</i>, so even <tt>getAt(x)</tt> will trigger a violation.</p>
<p>This rule also ignores all calls to <tt>super.getAt(Object)</tt>.</p></div>
<div class="section">
<h3><a name="ExplicitCallToLeftShiftMethod">ExplicitCallToLeftShiftMethod</a> Rule<a name="ExplicitCallToLeftShiftMethod_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.11</i></p>
<p>This rule detects when the <tt>leftShift(Object)</tt> method is called directly in code instead of using the << operator. A groovier way to express this: <tt>a.leftShift(b)</tt> is this: <tt>a << b</tt>. This rule can be configured to ignore <tt>this.leftShift(Object)</tt> using the <i>ignoreThisReference</i> property. It defaults to <i>false</i>, so even <tt>leftShift(x)</tt> will trigger a violation.</p>
<p>This rule also ignores all calls to <tt>super.leftShift(Object)</tt>.</p></div>
<div class="section">
<h3><a name="ExplicitCallToMinusMethod">ExplicitCallToMinusMethod</a> Rule<a name="ExplicitCallToMinusMethod_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.11</i></p>
<p>This rule detects when the <tt>minus(Object)</tt> method is called directly in code instead of using the <tt>-</tt> operator. A groovier way to express this: <tt>a.minus(b)</tt> is this: <tt>a - b</tt>. This rule can be configured to ignore <tt>minus.xor(Object)</tt> using the <i>ignoreThisReference</i> property. It defaults to <i>false</i>, so even <tt>minus(x)</tt> will trigger a violation.</p>
<p>This rule also ignores all calls to <tt>super.minus(Object)</tt>.</p></div>
<div class="section">
<h3><a name="ExplicitCallToMultiplyMethod">ExplicitCallToMultiplyMethod</a> Rule<a name="ExplicitCallToMultiplyMethod_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.11</i></p>
<p>This rule detects when the <tt>multiply(Object)</tt> method is called directly in code instead of using the <tt>*</tt> operator. A groovier way to express this: <tt>a.multiply(b)</tt> is this: <tt>a * b</tt>. This rule can be configured to ignore <tt>this.multiply(Object)</tt> using the <i>ignoreThisReference</i> property. It defaults to <i>false</i>, so even <tt>multiply(x)</tt> will trigger a violation.</p>
<p>This rule also ignores all calls to <tt>super.multiply(Object)</tt>.</p></div>
<div class="section">
<h3><a name="ExplicitCallToModMethod">ExplicitCallToModMethod</a> Rule<a name="ExplicitCallToModMethod_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.11</i></p>
<p>This rule detects when the <tt>mod(Object)</tt> method is called directly in code instead of using the <tt>%</tt> operator. A groovier way to express this: <tt>a.mod(b)</tt> is this: <tt>a % b</tt>. This rule can be configured to ignore <tt>this.mod(Object)</tt> using the <i>ignoreThisReference</i> property. It defaults to <i>false</i>, so even <tt>mod(x)</tt> will trigger a violation.</p>
<p>This rule also ignores all calls to <tt>super.mod(Object)</tt>.</p></div>
<div class="section">
<h3><a name="ExplicitCallToOrMethod">ExplicitCallToOrMethod</a> Rule<a name="ExplicitCallToOrMethod_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.11</i></p>
<p>This rule detects when the <tt>or(Object)</tt> method is called directly in code instead of using the <tt>|</tt> operator. A groovier way to express this: <tt>a.or(b)</tt> is this: <tt>a | b</tt>. This rule can be configured to ignore <tt>this.or(Object)</tt> using the <i>ignoreThisReference</i> property. It defaults to <i>true</i>, so even <tt>or(x)</tt> will not trigger a violation. This is the default because it is commonly used in Grails criteria.</p>
<p>This rule also ignores all calls to <tt>super.or(Object)</tt>.</p></div>
<div class="section">
<h3><a name="ExplicitCallToPlusMethod">ExplicitCallToPlusMethod</a> Rule<a name="ExplicitCallToPlusMethod_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.11</i></p>
<p>This rule detects when the <tt>plus(Object)</tt> method is called directly in code instead of using the <tt>+</tt> operator. A groovier way to express this: <tt>a.plus(b)</tt> is this: <tt>a + b</tt>. This rule can be configured to ignore <tt>this.plus(Object)</tt> using the <i>ignoreThisReference</i> property. It defaults to <i>false</i>, so even <tt>plus(x)</tt> will trigger a violation.</p>
<p>This rule also ignores all calls to <tt>super.plus(Object)</tt>.</p></div>
<div class="section">
<h3><a name="ExplicitCallToPowerMethod">ExplicitCallToPowerMethod</a> Rule<a name="ExplicitCallToPowerMethod_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.11</i></p>
<p>This rule detects when the <tt>power(Object)</tt> method is called directly in code instead of using the <tt>**</tt> operator. A groovier way to express this: <tt>a.power(b)</tt> is this: <tt>a ** b</tt>. This rule can be configured to ignore <tt>this.power(Object)</tt> using the <i>ignoreThisReference</i> property. It defaults to <i>false</i>, so even <tt>power(x)</tt> will trigger a violation.</p>
<p>This rule also ignores all calls to <tt>super.power(Object)</tt>.</p></div>
<div class="section">
<h3><a name="ExplicitCallToRightShiftMethod">ExplicitCallToRightShiftMethod</a> Rule<a name="ExplicitCallToRightShiftMethod_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.11</i></p>
<p>This rule detects when the <tt>rightShift(Object)</tt> method is called directly in code instead of using the >> operator. A groovier way to express this: <tt>a.rightShift(b)</tt> is this: <tt>a >> b</tt>. This rule can be configured to ignore <tt>this.rightShift(Object)</tt> using the <i>ignoreThisReference</i> property. It defaults to <i>false</i>, so even <tt>rightShift(x)</tt> will trigger a violation.</p>
<p>This rule also ignores all calls to <tt>super.rightShift(Object)</tt>.</p></div>
<div class="section">
<h3><a name="ExplicitCallToXorMethod">ExplicitCallToXorMethod</a> Rule<a name="ExplicitCallToXorMethod_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.11</i></p>
<p>This rule detects when the <tt>xor(Object)</tt> method is called directly in code instead of using the <tt>^</tt> operator. A groovier way to express this: <tt>a.xor(b)</tt> is this: <tt>a ^ b</tt>. This rule can be configured to ignore <tt>this.xor(Object)</tt> using the <i>ignoreThisReference</i> property. It defaults to <i>false</i>, so even <tt>xor(x)</tt> will trigger a violation.</p>
<p>This rule also ignores all calls to <tt>super.xor(Object)</tt>.</p></div>
<div class="section">
<h3><a name="ExplicitHashMapInstantiation">ExplicitHashMapInstantiation</a> Rule<a name="ExplicitHashMapInstantiation_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.11</i></p>
<p>This rule checks for explicit calls to the no-argument constructor of <tt>HashMap</tt>. In Groovy, it is best to replace <tt>new HashMap()</tt> with <tt>[:]</tt>, which creates (mostly) the same object. <tt>[:]</tt> is technically a LinkedHashMap but it is very rare that someone absolutely needs an instance of <tt>HashMap</tt> and not a subclass.</p></div>
<div class="section">
<h3><a name="ExplicitLinkedHashMapInstantiation">ExplicitLinkedHashMapInstantiation</a> Rule<a name="ExplicitLinkedHashMapInstantiation_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since in CodeNarc 0.14</i></p>
<p>This rule checks for the explicit instantiation of a <tt>LinkedHashMap</tt> using the no-arg constructor. In Groovy, it is best to replace <tt>new LinkedHashMap()</tt> with <tt>[:]</tt>, which creates the same object.</p></div>
<div class="section">
<h3><a name="ExplicitHashSetInstantiation">ExplicitHashSetInstantiation</a> Rule<a name="ExplicitHashSetInstantiation_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.11</i></p>
<p>This rule checks for explicit calls to the no-argument constructor of <tt>HashSet</tt>. In Groovy, it is best to replace <tt>new HashSet()</tt> with <tt>[] as Set</tt>, which creates the same object.</p></div>
<div class="section">
<h3><a name="ExplicitLinkedListInstantiation">ExplicitLinkedListInstantiation</a> Rule<a name="ExplicitLinkedListInstantiation_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.11</i></p>
<p>This rule checks for explicit calls to the no-argument constructor of <tt>LinkedList</tt>. In Groovy, it is best to replace <tt>new LinkedList()</tt> with <tt>[] as Queue</tt>, which creates the same object.</p></div>
<div class="section">
<h3><a name="ExplicitStackInstantiation">ExplicitStackInstantiation</a> Rule<a name="ExplicitStackInstantiation_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.11</i></p>
<p>This rule checks for explicit calls to the no-argument constructor of <tt>Stack</tt>. In Groovy, it is best to replace <tt>new Stack()</tt> with <tt>[] as Stack</tt>, which creates the same object.</p></div>
<div class="section">
<h3><a name="ExplicitTreeSetInstantiation">ExplicitTreeSetInstantiation</a> Rule<a name="ExplicitTreeSetInstantiation_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.11</i></p>
<p>This rule checks for explicit calls to the no-argument constructor of <tt>TreeSet</tt>. In Groovy, it is best to replace <tt>new TreeSet()</tt> with <tt>[] as SortedSet</tt>, which creates the same object.</p></div>
<div class="section">
<h3><a name="GetterMethodCouldBeProperty">GetterMethodCouldBeProperty</a> Rule<a name="GetterMethodCouldBeProperty_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>New in CodeNarc 0.16</i></p>
<p>If a class defines a <tt>public</tt> method that follows the Java getter notation and that returns a constant, then it is cleaner to provide a Groovy property for the value rather than a Groovy method.</p>
<p>Example of violations:</p>
<div>
<pre> interface Parent {
String getSomething()
String getSomethingElse()
}
class Child extends Parent {
static VALUE = 'value'
@Override
String getSomething() {
'something' // this could be simplified
}
@Override
String getSomethingElse() {
VALUE // this could be simplified
}
int getOtherValue() {
123
}
static String getName() {
'MyName'
}
}
class Child2 extends Parent {
static VALUE = 'value'
final String something = 'something' // this is cleaner
final String somethingElse = VALUE // this is cleaner
final int otherValue = 123 // this is cleaner
static final String name = 'MyName' // this is cleaner
}
</pre></div></div>
<div class="section">
<h3><a name="GroovyLangImmutable">GroovyLangImmutable</a> Rule<a name="GroovyLangImmutable_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>New in CodeNarc 0.13</i></p>
<p>The <tt>groovy.lang.Immutable</tt> annotation has been deprecated and replaced by <tt>groovy.transform.Immutable</tt>. Do not use the <tt>Immutable</tt> in <tt>groovy.lang</tt>.</p>
<p>Example of violations:</p>
<div>
<pre> @Immutable
class Person { }
@groovy.lang.Immutable
class Person { }
import groovy.lang.Immutable as Imtl
@Imtl
class Person { }
// the following code is OK
@groovy.transform.Immutable
class Person { }
import groovy.transform.Immutable
@Immutable
class Person { }
import groovy.transform.*
@Immutable
class Person { }
import groovy.transform.Immutable as Imtl
@Imtl
class Person { }
</pre></div></div>
<div class="section">
<h3><a name="GStringAsMapKey">GStringAsMapKey</a> Rule<a name="GStringAsMapKey_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.11</i></p>
<p>A GString should not be used as a map key since its <i>hashcode</i> is not guaranteed to be stable. Consider calling <tt>key.toString()</tt>.</p>
<p>Here is an example of code that produces a violation:</p>
<div>
<pre> Map map = ["${someRef}" : 'invalid' ] // violation
</pre></div></div>
<div class="section">
<h3><a name="GStringExpressionWithinString">GStringExpressionWithinString</a> Rule<a name="GStringExpressionWithinString_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.19</i></p>
<p>Check for regular (single quote) strings containing a GString-type expression ($<a name="a..">..</a>).</p>
<p>Example of violations:</p>
<div>
<pre> def str1 = 'total: ${count}' // violation
def str2 = 'average: ${total / count}' // violation
def str3 = "abc ${count}" // ok; GString
def str4 = '$123' // ok
def str5 = 'abc {123}' // ok
</pre></div></div>
<div class="section">
<h3><a name="UseCollectMany">UseCollectMany</a> Rule<a name="UseCollectMany_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>New in CodeNarc 0.16</i></p>
<p>In many case <tt>collectMany()</tt> yields the same result as <tt>collect<a name="a"></a>.flatten()</tt>. It is easier to understand and more clearly conveys the intent.</p>
<p>Example of violations:</p>
<div>
<pre>def l = [1, 2, 3, 4]
l.collect{ [it, it*2] }.flatten() // suboptimal
l.collectMany{ [it, it*2] } // same functionality, better readability
</pre></div></div>
<div class="section">
<h3><a name="UseCollectNested">UseCollectNested</a> Rule<a name="UseCollectNested_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>New in CodeNarc 0.16</i></p>
<p>Instead of nested <tt>collect<a name="a"></a></tt> calls use <tt>collectNested<a name="a"></a></tt>.</p>
<p>Example of violations:</p>
<div>
<pre>def list = [1, 2, [3, 4, 5, 6], [7]]
println list.collect { elem ->
if (elem instanceof List)
elem.collect {it *2} // violation
else elem * 2
}
println list.collect([8]) {
if (it instanceof List)
it.collect {it *2} // violation
else it * 2
}
println list.collectNested { it * 2 } // same functionality, better readability
</pre></div></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>
|