-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreference-ruleset.xml
More file actions
56 lines (43 loc) · 2.19 KB
/
Copy pathreference-ruleset.xml
File metadata and controls
56 lines (43 loc) · 2.19 KB
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
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="Default"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.github.io/ruleset_2_0_0.xsd">
<description>
Advanced rules
</description>
<rule name="AvoidVarForShortType"
language="java"
minimumLanguageVersion="10"
since="7.27"
message="The declared type ''{0}'' is not long enough (<{1}) to justify the use of var"
class="software.xdev.pmd.rule.AvoidVarForShortTypeRule">
<description>
The `var` keyword can be useful to minimize writing type declarations, however it should only be used when
required - usually on very long types - because:
* Reading code is more important than writing code
* Code should be clearly understandable without IDE support (e.g. during review)
* Most IDEs provide support for variable extraction that automatically adds the correct type
Further information can be found in the [OpenJDK's Local Variable Type Inference
CSG](https://openjdk.org/projects/amber/guides/lvti-style-guide).
A violation will be triggered when the length of the detected type declaration is below the specified
`lengthLimit`.
</description>
<priority>4</priority>
<example>
<![CDATA[
String msg = "Hello world"; // ok
var msg = "Hello world"; // bad: Could have just used `String`
byte flag = 0x2; // ok
var flag = 0x2; // bad: Will create an `int`
List<String> DEFAULT_LIST = List.of(); // ok: Type is automatically inferred
var DEFAULT_LIST = List.of(); // bad: Accidentally forgot <String>; will create List<Object>
var customers = db.execQuery("SELECT ..."); // bad: What is customers? Maybe a List? a Set? an Array?
// A dozen lines of other code...
customers.map(Customer::name).toList(); // bad: it looks like it's probably a Stream
Stream<Customer> customers = db.execQuery("SELECT ..."); // ok: You can immediately see that this is a Stream
var iterator = (Iterator<Map.Entry<? extends MyCacheKey, ? extends MyValue>>)create(); // ok: Type is very long
]]>
</example>
</rule>
</ruleset>