/
codenarc-rules-convention.apt
316 lines (232 loc) · 11.8 KB
/
codenarc-rules-convention.apt
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
--------------------------------------------------
CodeNarc - Convention Rules
--------------------------------------------------
Convention Rules ("<rulesets/convention.xml>")
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* {ConfusingTernary} Rule
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<Since CodeNarc 0.12>
In a ternary expression avoid negation in the test. For example, rephrase:
<<<(x != y) ? diff : same>>> as: <<<(x == y) ? same : diff>>>. Consistent use of this rule makes the code easier to read.
Also, this resolves trivial ordering problems, such as "does the error case go first?" or "does the common case go first?".
Example:
-------------------------------------------------------------------------------
(x != y) ? diff : same // triggers violation
(!x) ? diff : same // triggers violation
(x == y) ? same : diff // OK
(x) ? same : diff // OK
// this is OK, because of GroovyTruth there is no inverse of != null
(x != null) ? diff : same
// this is OK, because of GroovyTruth there is no inverse of != true
(x != true) ? diff : same
// this is OK, because of GroovyTruth there is no inverse of != false
(x != false) ? diff : same
-------------------------------------------------------------------------------
* {CouldBeElvis} Rule
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<Since CodeNarc 0.15>
Catch an if block that could be written as an elvis expression.
Example of violations:
-------------------------------------------------------------------------------
if (!x) { // violation
x = 'some value'
}
if (!x) // violation
x = "some value"
if (!params.max) { // violation
params.max = 10
}
x ?: 'some value' // OK
-------------------------------------------------------------------------------
* {CouldBeSwitchStatement} Rule
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<Since CodeNarc 1.0>
Checks for three of more if statements that could be converted to a switch. Only applies to equality and instanceof.
Example of violations:
-------------------------------------------------------------------------------
if (x == 1) { // violation
y = x
} else if (x == 2) {
y = x * 2
} else if (x == 3) {
y = x * 3
} else {
y = 0
}
if (y instanceof Integer) { // violation
x = y + 1
}
if (y instanceof String) {
x = y + '1'
} else if (y instanceof Boolean) {
x = !y
} else {
x = null
}
if (x == 1) { // OK
y = x
}
if (x == 2) {
y = x * 2
} else {
y = 0
}
if (!x && y) { // OK
doSomething()
} else if (!x && z) {
doSomethingElse()
} else if (!x && i) {
doAnotherThing()
}
-------------------------------------------------------------------------------
* {HashtableIsObsolete} Rule
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<Since CodeNarc 0.17>
Checks for references to the (<effectively>) obsolete <<<java.util.Hashtable>>> class.
Use the <<Java Collections Framework>> classes instead, including <<<HashMap>>> or
<<<ConcurrentHashMap>>>. See the JDK javadoc.
Example of violations:
-------------------------------------------------------------------------------
def myMap = new Hashtable() // violation
-------------------------------------------------------------------------------
* {IfStatementCouldBeTernary} Rule
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<Since CodeNarc 0.18>
Checks for:
* An <<<if>>> statement where both the <<<if>>> and <<<else>>> blocks contain only a single <<<return>>>
statement returning a constant or literal value.
* A block where the second-to-last statement in a block is an <<<if>>> statement with no <<<else>>>, where
the block contains a single <<<return>>> statement, and the last statement in the block is a <<<return>>>
statement, and both <<<return>>> statements return a constant or literal value.
This check is disabled by setting <<<checkLastStatementImplicitElse>>> to <<<false>>>.
Example of violations:
-------------------------------------------------------------------------------
if (condition) { return 44 } else { return 'yes' } // violation
if (check()) { return [1, 2] } else { return "count=$count" } // violation
if (condition) // violation
return null
else return [a:1]
def method1() {
if (condition) { // violation
return 44
}
return 'yes'
}
-------------------------------------------------------------------------------
* {InvertedIfElse} Rule
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<Since CodeNarc 0.11>
An inverted <if-else> statement is one in which there is a single <<<if>>> statement with a single
<<<else>>> branch and the boolean test of the <<<if>>> is negated. For instance <<<if (!x) false else true>>>.
It is usually clearer to write this as <<<if (x) true else false>>>.
* {LongLiteralWithLowerCaseL} Rule
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<Since CodeNarc 0.16>
In Java and Groovy, you can specify long literals with the L or l character,
for instance 55L or 24l. It is best practice to always use an uppercase L and never
a lowercase l. This is because 11l rendered in some fonts may look like 111 instead of 11L.
Example of violations:
-------------------------------------------------------------------------------
def x = 1l
def y = 55l
-------------------------------------------------------------------------------
* {NoDef} Rule
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<Since CodeNarc 0.22>
Do not allow using the <<<def>>> keyword in code. Use a specific type instead.
*-------------------------------+----------------------------------------------------------------+---------------------+
| <<Property>> | <<Description>> | <<Default Value>> |
*-------------------------------+----------------------------------------------------------------+---------------------+
| excludeRegex | Regular expression describing names of attributes, parameters | <<<>>> |
| | or methods that could be precede by the <<<def>>> keyword. | |
*-------------------------------+----------------------------------------------------------------+---------------------+
* {NoTabCharacter} Rule
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<Since CodeNarc 0.25>
Checks that all source files do not contain the tab character.
NOTE: This is a file-based rule, rather than an AST-based rule, so the <applyToClassNames> and
<doNotApplyToClassNames> rule configuration properties are not available. See
{{{./codenarc-configuring-rules.html#Standard_Properties_for_Configuring_Rules}
Standard Properties for Configuring Rules}}.
* {ParameterReassignment} Rule
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<Since CodeNarc 0.17>
Checks for a method or closure parameter being reassigned to a new value within the body of the method/closure,
which is a confusing and questionable practice. Use a temporary variable instead.
Example of violations:
-------------------------------------------------------------------------------
void myMethod(int a, String b) {
println a
b = 'new value' // violation
}
def myClosure1 = { int a, b ->
a = 123 // violation
}
-------------------------------------------------------------------------------
* {TernaryCouldBeElvis} Rule
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<Since CodeNarc 0.17>
Checks for ternary expressions where the <boolean> and <true> expressions are the same.
These can be simplified to an <Elvis> expression.
Example of violations:
-------------------------------------------------------------------------------
x ? x : false // violation; can simplify to x ?: false
foo() ? foo() : bar() // violation; can simplify to foo() ?: bar()
foo(1) ? foo(1) : 123 // violation; can simplify to foo(1) ?: 123
(x == y) ? same : diff // OK
x ? y : z // OK
x ? x + 1 : x + 2 // OK
x ? 1 : 0 // OK
x ? !x : x // OK
!x ? x : null // OK
foo() ? bar() : 123 // OK
foo() ? foo(99) : 123 // OK
foo(x) ? foo() : 123 // OK
foo(1) ? foo(2) : 123 // OK
-------------------------------------------------------------------------------
NOTE: If the <boolean> and <true> expressions are the same method call, and that method
call has <side-effects>, then converting it to a <Elvis> expression may produce <different>
behavior. The method will only be called <once>, rather than <twice>. But relying on those
<side-effects> as part of a ternary expression behavior is confusing, error-prone and just a
bad idea. In any case, that code should be refactored to move the reliance on the side-effects
out of the ternary expression.
* {TrailingComma} Rule
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<Since CodeNarc 0.25>
Check whether list and map literals contain optional trailing comma.
Rationale: Putting this comma in make is easier
to change the order of the elements or add new elements on the end.
*--------------------+---------------------------------------------------------------------+---------------------+
| <<Property>> | <<Description>> | <<Default Value>> |
*--------------------+---------------------------------------------------------------------+---------------------+
| checkList | To disable checking List literals, set this property to <<<false>>> | <<<true>>> |
*--------------------+---------------------------------------------------------------------+---------------------+
| checkMap | To disable checking Map literals, set this property to <<<false>>> | <<<true>>> |
*--------------------+---------------------------------------------------------------------+---------------------+
This is valid code:
-------------------------------------------------------------------------------
int[] array1 = [] // one line declaration
int[] array2 = [ // empty list
]
int[] array3 = [1,2,3] // one line declaration
int[] array4 = [1,
2,
3, // contains trailing comma
]
-------------------------------------------------------------------------------
Example of violations:
-------------------------------------------------------------------------------
int[] array2 = [1,
2 // there is no trailing comma
]
-------------------------------------------------------------------------------
* {VectorIsObsolete} Rule
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<Since CodeNarc 0.17>
Checks for references to the (<effectively>) obsolete <<<java.util.Vector>>> class.
Use the <<Java Collections Framework>> classes instead, including <<<ArrayList>>> or
<<<Collections.synchronizedList()>>>. See the JDK javadoc.
Example of violations:
-------------------------------------------------------------------------------
def myList = new Vector() // violation
-------------------------------------------------------------------------------