File: codenarc-rules-basic.html

package info (click to toggle)
codenarc 0.23-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 11,312 kB
  • sloc: java: 2,530; xml: 1,088; makefile: 21
file content (866 lines) | stat: -rw-r--r-- 42,076 bytes parent folder | download | duplicates (3)
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
<!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 - Basic 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>
                  &nbsp;| <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">
            <strong>Basic Rules</strong>
          </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">
                          <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>Basic Rules (&quot;<i>rulesets/basic.xml</i>&quot;)<a name="Basic_Rules_rulesetsbasic.xml"></a></h2><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<div class="section">
<h3>AssertWithinFinallyBlock<a name="AssertWithinFinallyBlock"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>Checks for <i>assert</i> statements within a <i>finally</i> block. An <i>assert</i> can throw an exception, hiding the original exception, if there is one.</p>
<p>Here is an example of code that produces a violation:</p>
<div>
<pre>    int myMethod(int count) {
        try {
            doSomething()
        } finally {
            assert count &gt; 0        // violation
        }
    }
</pre></div></div>
<div class="section">
<h3><a name="AssignmentInConditional">AssignmentInConditional</a> Rule<a name="AssignmentInConditional_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>New in CodeNarc 0.13</i></p>
<p>An assignment operator (=) was used in a conditional test. This is usually a typo, and the comparison operator (==) was intended.</p>
<p>Example of violations:</p>
<div>
<pre>    if ((value = true)) {
        // should be ==
    }

    while (value = true) {
        // should be ==
    }

    (value = true) ? x : y
    (value = true) ?: x

    // the following code has no violations
    if (value == true) {
    }

    value == true ? x : y
    value == true ?: x
</pre></div></div>
<div class="section">
<h3><a name="BigDecimalInstantiation">BigDecimalInstantiation</a> Rule<a name="BigDecimalInstantiation_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>Checks for calls to the <tt>java.math.BigDecimal</tt> constructors that take a <tt>double</tt> value as the first parameter. As described in the <tt>BigDecimal</tt> javadoc, the results from these constructors can be somewhat unpredictable, and their use is generally not recommended. This is because some numbers, such as 0.1, cannot be represented exactly as a <tt>double</tt>.</p>
<p>For instance, executing <tt>println new BigDecimal(0.1)</tt> prints out <tt>0.1000000000000000055511151231257827021181583404541015625</tt>.</p>
<p>Here is an example of code that produces a violation:</p>
<div>
<pre>    def b1 = new BigDecimal(0.1)               // violation
    def b2 = new java.math.BigDecimal(23.45d)  // violation
</pre></div></div>
<div class="section">
<h3><a name="BitwiseOperatorInConditional">BitwiseOperatorInConditional</a> Rule<a name="BitwiseOperatorInConditional_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>New in CodeNarc 0.15</i></p>
<p>Checks for bitwise operations in conditionals. For instance, the condition <tt>if (a | b)</tt> is almost always a mistake and should be <tt>if (a || b)</tt>. If you need to do a bitwise operation then it is best practice to extract a temp variable.</p>
<p>Example of violations:</p>
<div>
<pre>    if (a | b) { }
    if (a &amp; b) { }
</pre></div></div>
<div class="section">
<h3><a name="BooleanGetBoolean">BooleanGetBoolean</a> Rule<a name="BooleanGetBoolean_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>New in CodeNarc 0.13</i></p>
<p>This rule catches usages of java.lang.Boolean.getBoolean(String) which reads a boolean from the System properties. It is often mistakenly used to attempt to read user input or parse a String into a boolean. It is a poor piece of API to use; replace it with System.properties['prop&#x344;'].</p>
<p>Example of violations:</p>
<div>
<pre>    // produces violation
    Boolean.getBoolean(value)

    // zero or two parameters is OK, must be different method
    Boolean.getBoolean(value, 1)
    Boolean.getBoolean()
</pre></div></div>
<div class="section">
<h3><a name="BrokenNullCheck">BrokenNullCheck</a> Rule<a name="BrokenNullCheck_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.17</i></p>
<p>Looks for faulty checks for <i>null</i> that can cause a <tt>NullPointerException</tt>.</p>
<p>Examples:</p>
<div>
<pre>    if (name != null || name.length &gt; 0) { }            // violation
    if (name != null || name.length) { }                // violation
    while (record == null &amp;&amp; record.id &lt; 10) { }        // violation
    if (record == null &amp;&amp; record.id &amp;&amp; doStuff()) { }   // violation
    def isNotValid = record == null &amp;&amp; record.id &lt; 10   // violation
    return record == null &amp;&amp; !record.id                 // violation

    if (name != null || name.size() &gt; 0) { }            // violation
    if (string == null &amp;&amp; string.equals(&quot;&quot;)) { }        // violation
    def isValid = name != null || name.size() &gt; 0       // violation
    return name != null || !name.size()                 // violation
</pre></div></div>
<div class="section">
<h3><a name="BrokenOddnessCheck">BrokenOddnessCheck</a> Rule<a name="BrokenOddnessCheck_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.13</i></p>
<p>The code uses <tt>x % 2 == 1</tt> to check to see if a value is odd, but this won't work for negative numbers (e.g., <tt>(-5) % 2 == -1)</tt>. If this code is intending to check for oddness, consider using <tt>x &amp; 1 == 1</tt>, or <tt> x % 2 != 0</tt>.</p>
<p>Examples:</p>
<div>
<pre>    if (x % 2 == 1) { }             // violation
    if (method() % 2 == 1) { }      // violation

    if (x &amp; 1 == 1) { }             // OK
    if (x % 2 != 0) { }             // OK
</pre></div></div>
<div class="section">
<h3><a name="ClassForName">ClassForName</a> Rule<a name="ClassForName_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>New in CodeNarc 0.14</i></p>
<p>Using <tt>Class.forName(...)</tt> is a common way to add dynamic behavior to a system. However, using this method can cause resource leaks because the classes can be pinned in memory for long periods of time. If you're forced to do dynamic class loading then use ClassLoader.loadClass instead. All variations of the <tt>Class.forName(...)</tt> method suffer from the same problem.</p>
<p>For more information see these links:</p>
<ul>
<li><a class="externalLink" href="http://blog.bjhargrave.com/2007/09/classforname-caches-defined-class-in.html">http://blog.bjhargrave.com/2007/09/classforname-caches-defined-class-in.html</a></li>
<li><a class="externalLink" href="http://www.osgi.org/blog/2011/05/what-you-should-know-about-class.html">http://www.osgi.org/blog/2011/05/what-you-should-know-about-class.html</a></li></ul>
<p>Example of violations:</p>
<div>
<pre>    Class.forName('SomeClassName')
    Class.forName(aClassName, true, aClassLoader)
</pre></div></div>
<div class="section">
<h3><a name="ComparisonOfTwoConstants">ComparisonOfTwoConstants</a> Rule<a name="ComparisonOfTwoConstants_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.14</i></p>
<p>Checks for expressions where a <i>comparison operator</i> or <tt>equals()</tt> or <tt>compareTo()</tt> is used to compare two constants to each other or two literals that contain only constant values.</p>
<p>Here are examples of code that produces a violation:</p>
<div>
<pre>    23 == 67                    // violation
    Boolean.FALSE != false      // violation
    23 &lt; 88                     // violation
    0.17 &lt;= 0.99                // violation
    &quot;abc&quot; &gt; &quot;ddd&quot;               // violation
    [Boolean.FALSE] &gt;= [27]     // violation
    [a:1] &lt;=&gt; [a:2]             // violation

    [1,2].equals([3,4])                                     // violation
    [a:123, b:true].equals(['a':222, b:Boolean.FALSE])      // violation

    [a:123, b:456].compareTo([a:222, b:567]                 // violation
    [a:false, b:true].compareTo(['a':34.5, b:Boolean.TRUE]  // violation
</pre></div></div>
<div class="section">
<h3><a name="ComparisonWithSelf">ComparisonWithSelf</a> Rule<a name="ComparisonWithSelf_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.14</i></p>
<p>Checks for expressions where a <i>comparison operator</i> or <tt>equals()</tt> or <tt>compareTo()</tt> is used to compare a variable to itself, e.g.: <tt>x == x, x != x, x &lt;=&gt; x, x &lt; x, x &gt;= x, x.equals(x) or x.compareTo(x)</tt>, where <tt>x</tt> is a variable.</p>
<p>Here are examples of code that produces a violation:</p>
<div>
<pre>    if (x == x) { }                 // violation
    if (x != x) { }                 // violation
    while (x &lt; x) { }               // violation
    if (x &lt;= x) { }                 // violation
    while (x &gt; x) { }               // violation
    if (x &gt;= x) { }                 // violation
    def c = (x &lt;=&gt; x) { }           // violation
    println isReady = x.equals(x)   // violation
    println x.compareTo(x)          // violation
</pre></div></div>
<div class="section">
<h3><a name="ConstantAssertExpression">ConstantAssertExpression</a> Rule<a name="ConstantAssertExpression_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>Checks for <i>assert</i> statements with a constant value for the <i>assert</i> boolean expression, such as <tt>true</tt>, <tt>false</tt>, <tt>null</tt>, or a literal constant value. These <i>assert</i> statements will always pass or always fail, depending on the constant/literal value. Examples of violations include:</p>
<div>
<pre>    assert true
    assert false, &quot;assertion message&quot;
    assert Boolean.TRUE
    assert Boolean.FALSE
    assert null
    assert 0
    assert 99.7
    assert &quot;&quot;
    assert &quot;abc&quot;
    assert [:]
    assert [a:123, b:456]
    assert [a, b, c]
</pre></div></div>
<div class="section">
<h3><a name="ConstantIfExpression">ConstantIfExpression</a> Rule<a name="ConstantIfExpression_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>Checks for <i>if</i> statements with a constant value for the <i>if</i> boolean expression, such as <tt>true</tt>, <tt>false</tt>, <tt>null</tt>, or a literal constant value. These <i>if</i> statements can be simplified or avoided altogether. Examples of violations include:</p>
<div>
<pre>    if (true) { .. }
    if (false) { .. }
    if (Boolean.TRUE) { .. }
    if (Boolean.FALSE) { .. }
    if (null) { .. }
    if (0) { .. }
    if (99.7) { .. }
    if (&quot;&quot;) { .. }
    if (&quot;abc&quot;) { .. }
    if ([:]) { .. }
    if ([a:123, b:456]) { .. }
    if ([a, b, c]) { .. }
</pre></div></div>
<div class="section">
<h3><a name="ConstantTernaryExpression">ConstantTernaryExpression</a> Rule<a name="ConstantTernaryExpression_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>Checks for ternary expressions with a constant value for the boolean expression, such as <tt>true</tt>, <tt>false</tt>, <tt>null</tt>, or a literal constant value. Examples of violations include:</p>
<div>
<pre>    true ? x : y
    false ? x : y
    Boolean.TRUE ? x : y
    Boolean.FALSE ? x : y
    null ? x : y
    0 ? x : y
    99.7 ? x : y
    &quot;&quot; ? x : y
    &quot;abc&quot; ? x : y
    [:] ? x : y
    [a:123, b:456] ? x : y
    [a, b, c] ? x : y
</pre></div>
<p>The rule also checks for the same types of constant values for the boolean expressions within the &quot;short&quot; ternary expressions, also known as the &quot;Elvis&quot; operator, e.g.:</p>
<div>
<pre>    true ?: y
    null ?: y
    99.7 ?: y
    &quot;abc&quot; ?: y
    [:] ?: y
    [a, b, c] ?: y
</pre></div></div>
<div class="section">
<h3><a name="DeadCode">DeadCode</a> Rule<a name="DeadCode_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.11</i></p>
<p>Dead code appears after a <tt>return</tt> statement or an exception is thrown. If code appears after one of these statements then it will never be executed and can be safely deleted.</p></div>
<div class="section">
<h3><a name="DoubleNegative">DoubleNegative</a> Rule<a name="DoubleNegative_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.11</i></p>
<p>There is no point in using a double negative, it is always positive. For instance <tt>!!x</tt> can always be simplified to <tt>x</tt>. And <tt>!(!x)</tt> can as well.</p></div>
<div class="section">
<h3><a name="DuplicateCaseStatement">DuplicateCaseStatement</a> Rule<a name="DuplicateCaseStatement_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.11</i></p>
<p>Check for duplicate <tt>case</tt> statements in a <tt>switch</tt> block, such as two equal integers or strings. Here are some examples of code that produces violations:</p>
<div>
<pre>    switch( 0 ) {
        case 1: break;
        case 2: break;
        case 2: break;          // violation
    }
    
    switch( &quot;test&quot; ) {
        case &quot;$a&quot;: break;
        case &quot;$a&quot;: break;       // ok; only flags constant values (not GStrings)
        case &quot;ab&quot;: break;
        case &quot;ab&quot;: break;       // violation
        case &quot;abc&quot;: break;
    }
</pre></div></div>
<div class="section">
<h3><a name="DuplicateMapKey">DuplicateMapKey</a> Rule<a name="DuplicateMapKey_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>New in CodeNarc 0.14</i></p>
<p>A <i>Map</i> literal is created with duplicated key. The map entry will be overwritten.</p>
<p>Example of violations:</p>
<div>
<pre>    def var1 = [a:1, a:2, b:3]        //violation
    def var2 = [1:1, 1:2, 2:3]        //violation
    def var3 = [&quot;a&quot;:1, &quot;a&quot;:2, &quot;b&quot;:3]  //violation

    // these are OK
    def var4 = [a:1, b:1, c:1]
    def var5 = [1:1, 2:1, 3:1]
    def var6 = [&quot;a&quot;:1, &quot;b&quot;:1, &quot;c&quot;:1]
</pre></div></div>
<div class="section">
<h3><a name="DuplicateSetValue">DuplicateSetValue</a> Rule<a name="DuplicateSetValue_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>New in CodeNarc 0.14</i></p>
<p>A <i>Set</i> literal is created with duplicate constant value. A set cannot contain two elements with the same value.</p>
<p>Example of violations:</p>
<div>
<pre>    def a = [1, 2, 2, 4] as Set
    def b = [1, 2, 2, 4] as HashSet
    def c = [1, 2, 2, 4] as SortedSet
    def d = [1, 2, 2, 4] as FooSet
    def e = ['1', '2', '2', '4'] as Set
    def f = ['1', '2', '2', '4'] as HashSet
    def g = ['1', '2', '2', '4'] as SortedSet
    def h = ['1', '2', '2', '4'] as FooSet

    // these are OK
    def a = [1, 2, 3, 4] as Set
    def b = ['1', '2', '3', '4'] as Set
    def c = [1, '1'] as Set
</pre></div></div>
<div class="section">
<h3><a name="EmptyCatchBlock">EmptyCatchBlock</a> Rule<a name="EmptyCatchBlock_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>Checks for empty <i>catch</i> blocks. In most cases, exceptions should not be caught and ignored (swallowed).</p>
<p>The rule has a property named <tt>ignoreRegex</tt> that defaults to the value 'ignore|ignored'. If the name of the exception matches this regex then no violations are produced.</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">ignoreRegex</td>
<td align="left">Regular expression - exception parameter names matching this regular expression are ignored and no volations are produced.</td>
<td align="left">'ignore</td></tr></table>
<p>Here is an example of code that produces a violation:</p>
<div>
<pre>    def myMethod() {
        try {
            doSomething
        } catch(MyException e) {                //violation
            // should do something here
        }
    }

    def myMethod() {
        try {
            doSomething
        } catch(MyException ignored) {
            //no violations because the parameter name is ignored
        }
    }
</pre></div></div>
<div class="section">
<h3><a name="EmptyClass">EmptyClass</a> Rule<a name="EmptyClass_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.19</i></p>
<p>Reports classes without methods, fields or properties. Why would you need a class like this?</p>
<p>This rule ignores interfaces, enums, anonymous inner classes, subclasses (extends), and classes with annotations.</p></div>
<div class="section">
<h3><a name="EmptyElseBlock">EmptyElseBlock</a> Rule<a name="EmptyElseBlock_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>Checks for empty <i>else</i> blocks. Empty <i>else</i> blocks are confusing and serve no purpose.</p>
<p>Here is an example of code that produces a violation:</p>
<div>
<pre>    def myMethod() {
        if (x==23) {
            println 'ok'
        } else {
            // empty
        }
    }
</pre></div></div>
<div class="section">
<h3><a name="EmptyFinallyBlock">EmptyFinallyBlock</a> Rule<a name="EmptyFinallyBlock_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>Checks for empty <i>finally</i> blocks. Empty <i>finally</i> blocks are confusing and serve no purpose.</p>
<p>Here is an example of code that produces a violation:</p>
<div>
<pre>    def myMethod() {
        try {
            doSomething()
        } finally {
            // empty
        }
    }
</pre></div></div>
<div class="section">
<h3><a name="EmptyForStatement">EmptyForStatement</a> Rule<a name="EmptyForStatement_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>Checks for empty <i>for</i> blocks. Empty <i>for</i> statements are confusing and serve no purpose.</p>
<p>Here is an example of code that produces a violation:</p>
<div>
<pre>    def myMethod() {
        for (int i=0; i &lt; 23; i++) {
            // empty
        }
    }
</pre></div></div>
<div class="section">
<h3><a name="EmptyIfStatement">EmptyIfStatement</a> Rule<a name="EmptyIfStatement_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>Checks for empty <i>if</i> statements. Empty <i>if</i> statements are confusing and serve no purpose.</p>
<p>Here is an example of code that produces a violation:</p>
<div>
<pre>    def myMethod() {
        if (x==23) {
            // empty
        }
    }
</pre></div></div>
<div class="section">
<h3><a name="EmptyInstanceInitializer">EmptyInstanceInitializer</a> Rule<a name="EmptyInstanceInitializer_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>New in CodeNarc 0.13</i></p>
<p>An empty class instance initializer was found. It is safe to remove it. Example:</p>
<div>
<pre>    class MyClass {
        { }     // empty instance initializer, not a closure
    }
</pre></div></div>
<div class="section">
<h3><a name="EmptyMethod">EmptyMethod</a> Rule<a name="EmptyMethod_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>New in CodeNarc 0.13</i></p>
<p>A method was found without an implementation. If the method is overriding or implementing a parent method, then mark it with the <tt>@Override</tt> annotation. This rule should not be used with Java 5 code because you cannot put <tt>@Override</tt> on a method implementing an interface. Use with Java 6 and higher.</p>
<p>Example of violations:</p>
<div>
<pre>    class MyClass {

        // violation, empty method
        public void method1() {}

        // violation, empty method
        def method2() {}

        // OK because of @Override
        @Override
        public void method3() {}
    }

    abstract class MyBaseClass {
        // OK, handled by EmptyMethodInAbstractClass Rule
        public void method() {}
    }
</pre></div></div>
<div class="section">
<h3><a name="EmptyStaticInitializer">EmptyStaticInitializer</a> Rule<a name="EmptyStaticInitializer_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>New in CodeNarc 0.13</i></p>
<p>An empty static initializer was found. It is safe to remove it. Example:</p>
<div>
<pre>    class MyClass {
        static { }
    }
</pre></div></div>
<div class="section">
<h3><a name="EmptySwitchStatement">EmptySwitchStatement</a> Rule<a name="EmptySwitchStatement_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>Checks for empty <i>switch</i> statements. Empty <i>switch</i> statements are confusing and serve no purpose.</p>
<p>Here is an example of code that produces a violation:</p>
<div>
<pre>    def myMethod() {
        switch(myVariable) {
            // empty
        }
    }
</pre></div></div>
<div class="section">
<h3><a name="EmptySynchronizedStatement">EmptySynchronizedStatement</a> Rule<a name="EmptySynchronizedStatement_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>Checks for empty <i>synchronized</i> statements. Empty <i>synchronized</i> statements are confusing and serve no purpose.</p>
<p>Here is an example of code that produces a violation:</p>
<div>
<pre>    class MyClass {
        def myMethod() {
            synchronized(lock) {
            }
        }
    }
</pre></div></div>
<div class="section">
<h3><a name="EmptyTryBlock">EmptyTryBlock</a> Rule<a name="EmptyTryBlock_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>Checks for empty <i>try</i> blocks. Empty <i>try</i> blocks are confusing and serve no purpose.</p>
<p>Here is an example of code that produces a violation:</p>
<div>
<pre>    def myMethod() {
        try {
            // empty
        } catch(MyException e) {
            e.printStackTrace()
        }
    }
</pre></div></div>
<div class="section">
<h3><a name="EmptyWhileStatement">EmptyWhileStatement</a> Rule<a name="EmptyWhileStatement_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>Checks for empty <i>while</i> statements. Empty <i>while</i> statements are confusing and serve no purpose.</p>
<p>Here is an example of code that produces a violation:</p>
<div>
<pre>    def myMethod() {
        while (!stopped) {
            // empty
        }
    }
</pre></div></div>
<div class="section">
<h3><a name="EqualsAndHashCode">EqualsAndHashCode</a> Rule<a name="EqualsAndHashCode_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>Checks that if either the <tt>boolean equals(Object)</tt> or the <tt>int hashCode()</tt> methods are overridden within a class, then both must be overridden.</p>
<p>Here is an example of code that produces a violation:</p>
<div>
<pre>    class MyClass {
        boolean equals(Object object) {
            // do something
        }
    }
</pre></div>
<p>And so does this:</p>
<div>
<pre>    class MyClass {
        int hashCode() {
            return 0
        }
    }
</pre></div></div>
<div class="section">
<h3><a name="EqualsOverloaded">EqualsOverloaded</a> Rule<a name="EqualsOverloaded_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>New in CodeNarc 0.14</i></p>
<p>The class has an <tt>equals</tt> method, but the parameter of the method is not of type <tt>Object</tt>. It is not overriding <tt>equals</tt> but instead overloading it.</p>
<p>Example of violations:</p>
<div>
<pre>    class Object1 {
        //parameter should be Object not String
        boolean equals(String other) { true }
    }

    class Object2 {
        // Overloading equals() with 2 parameters is just mean
        boolean equals(Object other, String other2) { true }
    }

    class Object3 {
        // a no-arg equals()? What is this supposed to do?
        boolean equals() { true }
    }


    // all of these are OK and do not cause violations
    class Object4 {
        boolean equals(Object other) { true }
    }

    @SuppressWarnings('EqualsOverloaded')
    class Object5 {
        boolean equals(String other) { true }
    }

    class Object6 {
        boolean equals(java.lang.Object other) { true }
    }

    class Object7 {
        boolean equals(other) { true }
    }
</pre></div></div>
<div class="section">
<h3><a name="ExplicitGarbageCollection">ExplicitGarbageCollection</a> Rule<a name="ExplicitGarbageCollection_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.12</i></p>
<p>Calls to <tt>System.gc()</tt>, <tt>Runtime.getRuntime().gc()</tt>, and <tt>System.runFinalization()</tt> are not advised. Code should have the same behavior whether the garbage collection is disabled using the option <tt>-Xdisableexplicitgc</tt> or not. Moreover, &quot;modern&quot; JVMs do a very good job handling garbage collections. If memory usage issues unrelated to memory leaks develop within an application, it should be dealt with JVM options rather than within the code itself.</p></div>
<div class="section">
<h3><a name="ForLoopShouldBeWhileLoop">ForLoopShouldBeWhileLoop</a> Rule<a name="ForLoopShouldBeWhileLoop_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>New in CodeNarc 0.14</i></p>
<p>A <tt>for</tt> loop without an init and update statement can be simplified to a <tt>while</tt> loop.</p>
<p>Example of violations:</p>
<div>
<pre>    int i = 0;
    for(; i &lt; 5;) {     // Violation
        println i++
    }

    // These are OK
    for(i in [1,2])         // OK
       println i

    for(int i = 0; i&lt;5;)    // OK
        println i++

    int i = 0;
    for(; i &lt; 5; i++)       // OK
        println i

    for (Plan p : plans) {  // OK
        println &quot;Plan=$p&quot;
    }
</pre></div></div>
<div class="section">
<h3><a name="HardCodedWindowsFileSeparator">HardCodedWindowsFileSeparator</a> Rule<a name="HardCodedWindowsFileSeparator_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>New in CodeNarc 0.15</i></p>
<p>This rule finds usages of a Windows file separator within the constructor call of a File object. It is better to use the Unix file separator or use the File.separator constant.</p>
<p>Example of violations:</p>
<div>
<pre>   new File('.\\foo\\')
   new File('c:\\dir')
   new File('../foo\\')
</pre></div></div>
<div class="section">
<h3><a name="HardCodedWindowsRootDirectory">HardCodedWindowsRootDirectory</a> Rule<a name="HardCodedWindowsRootDirectory_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>New in CodeNarc 0.15</i></p>
<p>This rule find cases where a File object is constructed with a windows-based path. This is not portable across operating systems or different machines, and using the File.listRoots() method is a better alternative.</p>
<p>Example of violations:</p>
<div>
<pre>   new File('c:\\')
   new File('c:\\dir')
   new File('E:\\dir')
</pre></div></div>
<div class="section">
<h3><a name="IntegerGetInteger">IntegerGetInteger</a> Rule<a name="IntegerGetInteger_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>New in CodeNarc 0.13</i></p>
<p>This rule catches usages of java.lang.Integer.getInteger(String, ...) which reads an Integer from the System properties. It is often mistakenly used to attempt to read user input or parse a String into an Integer. It is a poor piece of API to use; replace it with System.properties['prop'].</p>
<p>Example of violations:</p>
<div>
<pre>    // violations
    Integer.getInteger(value)
    Integer.getInteger(value, radix)

    // zero or more than 2 parameters is OK, must be different method
    Integer.getInteger()
    Integer.getInteger(value, radix, locale)
</pre></div></div>
<div class="section">
<h3><a name="MultipleUnaryOperators">MultipleUnaryOperators</a> Rule<a name="MultipleUnaryOperators_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.21</i></p>
<p>Checks for multiple consecutive unary operators. These are confusing, and are likely typos and bugs.</p>
<p>Example of violations:</p>
<div>
<pre>    int z = ~~2             // violation
    boolean b = !!true      // violation
    boolean c = !!!false    // 2 violations
    int j = -~7             // violation
    int k = +~8             // violation
</pre></div></div>
<div class="section">
<h3><a name="RandomDoubleCoercedToZero">RandomDoubleCoercedToZero</a> Rule<a name="RandomDoubleCoercedToZero_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>New in CodeNarc 0.15</i></p>
<p>The Math.random() method returns a double result greater than or equal to 0.0 and less than 1.0. If you coerce this result into an Integer, Long, int, or long then it is coerced to zero. Casting the result to int, or assigning it to an int field is probably a bug.</p>
<p>Example of violations:</p>
<div>
<pre>    (int) Math.random()
    (Integer) Math.random()
    int x = Math.random()
    Integer y = Math.random()
    int m() { Math.random() }
    Integer m() { Math.random() }
    (Math.random()) as int
    (Math.random()) as Integer
</pre></div></div>
<div class="section">
<h3><a name="RemoveAllOnSelf">RemoveAllOnSelf</a> Rule<a name="RemoveAllOnSelf_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.11</i></p>
<p>Don't use <tt>removeAll</tt> to clear a collection. If you want to remove all elements from a collection <tt>c</tt>, use <tt>c.clear</tt>, not <tt>c.removeAll(c)</tt>. Calling <tt>c.removeAll(c)</tt> to clear a collection is less clear, susceptible to errors from typos, less efficient and for some collections, might throw a <tt>ConcurrentModificationException</tt>.</p></div>
<div class="section">
<h3><a name="ReturnFromFinallyBlock">ReturnFromFinallyBlock</a> Rule<a name="ReturnFromFinallyBlock_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>Checks for a return from within a <i>finally</i> block. Returning from a <i>finally</i> block is confusing and can hide the original exception.</p>
<p>Here is an example of code that produces a violation:</p>
<div>
<pre>    int myMethod() {
        try {
            doSomething()
            return 0
        } catch(Exception e) {
            return -1
        } finally {
            return 99               // violation
        }
    }
</pre></div></div>
<div class="section">
<h3>ThrowExceptionFromFinallyBlock<a name="ThrowExceptionFromFinallyBlock"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>Checks for throwing an exception from within a <i>finally</i> block. Throwing an exception from a <i>finally</i> block is confusing and can hide the original exception.</p>
<p>Here is an example of code that produces a violation:</p>
<div>
<pre>    int myMethod() {
        try {
            doSomething()
            throw new Exception()
        } finally {
            println 'finally'
            throw new Exception()   // violation
        }
    }
</pre></div></div></div>
      </div>
    </div>
    <div class="clear">
      <hr/>
    </div>
    <div id="footer">
      <div class="xright">
              Copyright &#169;                   2015.
          All Rights Reserved.      
                    
                  </div>
      <div class="clear">
        <hr/>
      </div>
    </div>
  </body>
</html>