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
|
<!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 - Design 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">
<strong>Design Rules</strong>
</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>Design Rules ("<i>rulesets/design.xml</i>")<a name="Design_Rules_rulesetsdesign.xml"></a></h2><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<div class="section">
<h3><a name="AbstractClassWithPublicConstructor">AbstractClassWithPublicConstructor</a> Rule<a name="AbstractClassWithPublicConstructor_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.14</i></p>
<p>Checks for <tt>abstract</tt> classes that define a <tt>public</tt> constructor, which is useless and confusing.</p>
<p>The following code produces a violation:</p>
<div>
<pre> abstract class MyClass {
MyClass() { }
}
</pre></div></div>
<div class="section">
<h3><a name="AbstractClassWithoutAbstractMethod">AbstractClassWithoutAbstractMethod</a> Rule<a name="AbstractClassWithoutAbstractMethod_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.12</i></p>
<p>The abstract class does not contain any abstract methods. An abstract class suggests an incomplete implementation, which is to be completed by subclasses implementing the abstract methods. If the class is intended to be used as a base class only (not to be instantiated directly) a protected constructor can be provided prevent direct instantiation.</p>
<p>Example:</p>
<div>
<pre> public abstract class MyBaseClass {
void method1() { }
void method2() { }
// consider using abstract methods or removing
// the abstract modifier and adding protected constructors
}
</pre></div>
<p>The following examples all pass:</p>
<div>
<pre> abstract class MyClass extends AbstractParent {
// OK because parent is named Abstract.*
}
abstract class MyClass extends BaseParent{
// OK because parent is named Base.*
}
</pre></div></div>
<div class="section">
<h3><a name="BooleanMethodReturnsNull">BooleanMethodReturnsNull</a> Rule<a name="BooleanMethodReturnsNull_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.11</i></p>
<p>Checks for a method with <tt>Boolean</tt> return type that returns an explicit <tt>null</tt>. A method that returns either <tt>Boolean.TRUE</tt>, <tt>Boolean.FALSE</tt> or <tt>null</tt> is an accident waiting to happen. This method can be invoked as though it returned a value of type <tt>boolean</tt>, and the compiler will insert automatic <i>unboxing</i> of the <tt>Boolean</tt> value. If a <tt>null</tt> value is returned, this will result in a <tt>NullPointerException</tt>.</p></div>
<div class="section">
<h3><a name="BuilderMethodWithSideEffects">BuilderMethodWithSideEffects</a> Rule<a name="BuilderMethodWithSideEffects_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>New in CodeNarc 0.16</i></p>
<p>A builder method is defined as one that creates objects. As such, they should never be of void return type. If a method is named build, create, or make, then it should always return a value.</p>
<p>This rule has one property: <tt>methodNameRegex</tt>. The default value is (make.*|create.*|build.*). Update this property if you have some other naming convention for your builder methods.</p>
<p>Example of violations:</p>
<div>
<pre>
class MyClass {
void make() { /* ... */ }
void makeSomething() { /* ... */ }
void create() { /* ... */ }
void createSomething() { /* ... */ }
void build() { /* ... */ }
void buildSomething() { /* ... */ }
}
</pre></div></div>
<div class="section">
<h3><a name="CloneableWithoutClone">CloneableWithoutClone</a> Rule<a name="CloneableWithoutClone_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>Checks for classes that implement the <tt>java.lang.Cloneable</tt> interface without implementing the <tt>clone()</tt> method.</p>
<p>Here is an example of code that produces a violation:</p>
<div>
<pre> class BadClass implements Cloneable {
def someMethod()
}
</pre></div></div>
<div class="section">
<h3><a name="CloseWithoutCloseable">CloseWithoutCloseable</a> Rule<a name="CloseWithoutCloseable_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.12</i></p>
<p>If a class defines a "void close()" then that class should implement java.io.Closeable.</p></div>
<div class="section">
<h3><a name="CompareToWithoutComparable">CompareToWithoutComparable</a> Rule<a name="CompareToWithoutComparable_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.12</i></p>
<p>If you implement a compareTo method then you should also implement the <tt>Comparable</tt> interface. If you don't then you could possibly get an exception if the Groovy == operator is invoked on your object. This is an issue fixed in Groovy 1.8 but present in previous versions.</p>
<p>Here is an example of code that produces a violation:</p>
<div>
<pre> class BadClass {
int compareTo(Object o) { ... }
}
</pre></div></div>
<div class="section">
<h3><a name="ConstantsOnlyInterface">ConstantsOnlyInterface</a> Rule<a name="ConstantsOnlyInterface_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.12</i></p>
<p>An interface should be used only to model a behaviour of a class: using an interface as a container of constants is a poor usage pattern. Example:</p>
<div>
<pre> public interface ConstantsInterface {
public static final int CONSTANT_1 = 0
public static final String CONSTANT_2 = "1"
}
</pre></div></div>
<div class="section">
<h3><a name="EmptyMethodInAbstractClass">EmptyMethodInAbstractClass</a> Rule<a name="EmptyMethodInAbstractClass_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.12</i></p>
<p>An empty method in an abstract class should be abstract instead, as developer may rely on this empty implementation rather than code the appropriate one.</p>
<div>
<pre> abstract class MyClass {
def couldBeAbstract_1() {
return null // Should be abstract method
}
void couldBeAbstract_2() {
// Should be abstract method
}
}
</pre></div></div>
<div class="section">
<h3><a name="FinalClassWithProtectedMember">FinalClassWithProtectedMember</a> Rule<a name="FinalClassWithProtectedMember_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.12</i></p>
<p>This rule finds classes marked final that contain <tt>protected</tt> members. If a class is <tt>final</tt> then it may not be subclassed, and there is therefore no point in having a member with <tt>protected</tt> visibility. Either the class should not be <tt>final</tt> or the member should be private or protected.</p></div>
<div class="section">
<h3><a name="ImplementationAsType">ImplementationAsType</a> Rule<a name="ImplementationAsType_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>Checks for use of the following concrete classes when specifying the type of a method parameter, closure parameter, constructor parameter, method return type or field type. The corresponding interfaces should be used to specify the type instead.</p>
<ul>
<li>java.util.ArrayList</li>
<li>java.util.GregorianCalendar</li>
<li>java.util.HashMap</li>
<li>java.util.HashSet</li>
<li>java.util.Hashtable</li>
<li>java.util.LinkedHashMap</li>
<li>java.util.LinkedHashSet</li>
<li>java.util.LinkedList</li>
<li>java.util.TreeMap</li>
<li>java.util.TreeSet</li>
<li>java.util.Vector</li>
<li>java.util.concurrent.ArrayBlockingQueue</li>
<li>java.util.concurrent.ConcurrentHashMap</li>
<li>java.util.concurrent.ConcurrentLinkedQueue</li>
<li>java.util.concurrent.CopyOnWriteArrayList</li>
<li>java.util.concurrent.CopyOnWriteArraySet</li>
<li>java.util.concurrent.DelayQueue</li>
<li>java.util.concurrent.LinkedBlockingQueue</li>
<li>java.util.concurrent.PriorityBlockingQueue</li>
<li>java.util.concurrent.PriorityQueue</li>
<li>java.util.concurrent.SynchronousQueue</li></ul>
<p>Here are examples of code that produces violations:</p>
<div>
<pre> // Method parameter
void myMethod(ArrayList list) { // violation
...
}
// Constructor parameter
class MyClass {
MyClass(java.util.HashSet set) { // violation
...
}
}
// Closure parameter
def closure = { PriorityQueue queue -> ... } // violation
// Method return type
GregorianCalendar calculateDate(int num) { // violation
...
}
// Field type
class MyClass {
Hashtable map // violation
}
</pre></div></div>
<div class="section">
<h3><a name="Instanceof">Instanceof</a> Rule<a name="Instanceof_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.22</i></p>
<p>Checks for use of the <tt>instanceof</tt> operator. Prefer using <i>polymorphism</i> instead.</p>
<p>Use the <tt>ignoreTypeNames</tt> property to configure ignored type names (the class name specified as the right-hand expression of the <tt>instanceof</tt>). It defaults to ignoring <tt>instanceof</tt> checks against exception classes.</p>
<p>Here are a couple references that discuss the problems with using <tt>instanceof</tt> and the preference for using <i>polymorphism</i> instead:</p>
<ul>
<li><a class="externalLink" href="http://www.javapractices.com/topic/TopicAction.do?Id=31">Beware of instanceof operator</a></li>
<li><a class="externalLink" href="http://stackoverflow.com/questions/4192837/how-does-one-use-polymorphism-instead-of-instanceof-and-why">How does one use polymorphism instead of instanceof? (And why?)</a></li></ul>
<p>By default, the rule does not analyze test files. This rule sets the default value of the <i>doNotApplyToFilesMatching</i> property to ignore file names ending in 'Test.groovy', 'Tests.groovy' or 'TestCase.groovy'.</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">ignoreTypeNames</td>
<td align="left">Specifies one or more (comma-separated) class names that should be ignored (i.e., that should not cause a rule violation). The names may optionally contain wildcards (*,?).</td>
<td align="left">*Exceptions</td></tr></table>
<p>Example of violations:</p>
<div>
<pre> class MyClass {
boolean isRunnable = this instanceof Runnable // violation
}
</pre></div></div>
<div class="section">
<h3><a name="LocaleSetDefault">LocaleSetDefault</a> Rule<a name="LocaleSetDefault_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.20</i></p>
<p>Checks for calls to <tt>Locale.setDefault()</tt>, or <tt>Locale.default = Xxx</tt>, which sets the Locale across the entire JVM. That can impact other applications on the same web server, for instance.</p>
<p>From the java.util.Locale javadoc for <tt>setDefault</tt>: <i>Since changing the default locale may affect many different areas of functionality, this method should only be used if the caller is prepared to reinitialize locale-sensitive code running within the same Java Virtual Machine.</i></p>
<p>Example of violations:</p>
<div>
<pre> Locale.setDefault(Locale.UK) // violation
java.util.Locale.setDefault(Locale.FRANCE) // violation
Locale.setDefault(Locale.Category.DISPLAY, Locale.JAPAN) // violation
Locale.default = Locale.UK // violation
</pre></div></div>
<div class="section">
<h3><a name="NestedForLoop">NestedForLoop</a> Rule<a name="NestedForLoop_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.23</i></p>
<p>Reports classes with nested for loops.</p>
<p>Example of violations:</p>
<div>
<pre>for (int i = 0; i < 100; ++i) {
for (int j = 0; j < 100; ++j) { // violation
println i + j
}
}
for (int i = 0; i < 100; ++i) {
for (int j = 0; j < 100; ++j) { // violation
println i + j
}
for (int j = 0; j < 100; ++j) { // violation
println i + j
}
}
for (int i = 0; i < 100; ++i) {
for (int j = 0; j < 100; ++j) { // violation
for (int k = 0; k < 100; ++k) { // violation
println i + j + k
}
}
}
</pre></div></div>
<div class="section">
<h3><a name="PrivateFieldCouldBeFinal">PrivateFieldCouldBeFinal</a> Rule<a name="PrivateFieldCouldBeFinal_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.17</i></p>
<p>This rule finds <tt>private</tt> fields that are only set within a <i>constructor</i> or <i>field initializer</i>. Such fields can safely be made <tt>final</tt>.</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">ignoreFieldNames</td>
<td align="left">Specifies one or more (comma-separated) field names that should be ignored (i.e., that should not cause a rule violation). The names may optionally contain wildcards (*,?).</td>
<td align="left"><i>null</i></td></tr>
<tr class="a">
<td align="left">ignoreJpaEntities</td>
<td align="left">Specifies whether fields defined inside classes annotated with @Entity or @MappedSuperclass JPA annotations should be ignored (i.e., that should not cause a rule violation).</td>
<td align="left">false</td></tr></table></div>
<div class="section">
<h3><a name="PublicInstanceField">PublicInstanceField</a> Rule<a name="PublicInstanceField_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>New in CodeNarc 0.14</i></p>
<p>Using public fields is considered to be a bad design. Use properties instead.</p>
<p>Example of violations:</p>
<div>
<pre> class Person {
public String name
}
</pre></div></div>
<div class="section">
<h3><a name="ReturnsNullInsteadOfEmptyArray">ReturnsNullInsteadOfEmptyArray</a> Rule<a name="ReturnsNullInsteadOfEmptyArray_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.11</i></p>
<p>If you have a method or closure that returns an array, then when there are no results return a zero-length (empty) array rather than <tt>null</tt>. It is often a better design to return a zero-length array rather than a <tt>null</tt> reference to indicate that there are no results (i.e., an <i>empty</i> list of results). This way, no explicit check for <tt>null</tt> is needed by clients of the method.</p></div>
<div class="section">
<h3><a name="ReturnsNullInsteadOfEmptyCollection">ReturnsNullInsteadOfEmptyCollection</a> Rule<a name="ReturnsNullInsteadOfEmptyCollection_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.11</i></p>
<p>If you have a method or closure that returns a collection, then when there are no results return a zero-length (empty) collection rather than <tt>null</tt>. It is often a better design to return a zero-length collection rather than a <tt>null</tt> reference to indicate that there are no results (i.e., an <i>empty</i> list of results). This way, no explicit check for <tt>null</tt> is needed by clients of the method.</p></div>
<div class="section">
<h3><a name="SimpleDateFormatMissingLocale">SimpleDateFormatMissingLocale</a> Rule<a name="SimpleDateFormatMissingLocale_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.12</i></p>
<p>Be sure to specify a <tt>Locale</tt> when creating a new instance of <tt>SimpleDateFormat</tt>; the class is locale-sensitive. If you instantiate <tt>SimpleDateFormat</tt> without a <tt>Locale</tt> parameter, it will format the date and time according to the default <tt>Locale</tt>. Both the pattern and the <tt>Locale</tt> determine the format. For the same pattern, <tt>SimpleDateFormat</tt> may format a date and time differently if the Locale varies.</p>
<div>
<pre> // violation, missing locale
new SimpleDateFormat('pattern')
// OK, includes locale
new SimpleDateFormat('pattern', Locale.US)
// OK, includes a variable that perhaps is a locale
new SimpleDateFormat('pattern', locale)
</pre></div></div>
<div class="section">
<h3><a name="StatelessSingleton">StatelessSingleton</a> Rule<a name="StatelessSingleton_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>New in CodeNarc 0.14</i></p>
<p>There is no point in creating a stateless Singleton because there is nothing within the class that needs guarding and no side effects to calling the constructor. Just create new instances of the object or write a Utility class with static methods. In the long term, Singletons can cause strong coupling and hard to change systems.</p>
<p>If the class has any fields at all, other than a self reference, then it is not considered stateless. A self reference is a field of the same type as the enclosing type, or a field named instance or _instance. The field name self reference is a property named instanceRegex that defaults to the value 'instance|_instance'</p>
<p>Example of violations:</p>
<div>
<pre> @groovy.lang.Singleton
class Service {
// violation: the class has no fields but is marked Singleton
void processItem(item){
}
}
class Service {
// violation: the class has no fields other than 'instance' but is marked Singleton
static instance
void processItem(item){
}
}
class Service { // violation
static Service service
void processItem(item){
}
}
</pre></div></div>
<div class="section">
<h3><a name="ToStringReturnsNull">ToStringReturnsNull</a> Rule<a name="ToStringReturnsNull_Rule"></a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><i>Since CodeNarc 0.21</i></p>
<p>Checks for <tt>toString()</tt> methods that return <tt>null</tt>. This is unconventional and could cause unexpected <tt>NullPointerExceptions</tt> from normal or implicit use of <tt>toString()</tt>.</p>
<p>Example of violations:</p>
<div>
<pre> class MyClass {
String toString() {
if (foo()) {
return 'MyClass'
} else {
return null // violation
}
}
}
class MyClass {
String toString() {
calculateStuff()
null // violation
}
}
class MyClass {
String toString() { // violation - implicit return of null
}
}
</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>
|