Skip to content

Commit 450246c

Browse files
committed
Add new Java rule GCI82 - Make non reassigned variables constants
1 parent dc0d3c3 commit 450246c

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12+
- Add new Java rule GCI82 - Make non reassigned variables constants
13+
1214
### Changed
1315

1416
- update developers list + update maven plugins versions

RULES.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Some are applicable for different technologies.
5858
| GCI78 | Const parameter in batch update | Don't set const parameter in batch update => Put its in query. Creating this parameter and destroying it consumes CPU cycles and RAM unnecessarily. | || 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 |
5959
| GCI79 | Free resources | try-with-resources Statement needs to be implemented for any object that implements the AutoCloseable interface, it save computer resources. | || 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 |
6060
| GCI81 | Specify struct layouts | When possible, specify struct layouts to optimize their memory footprint | | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 || 🚫 |
61-
| GCI82 | Make variable constant | A variable is never reassigned and can be made constant | | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 || 🚫 |
61+
| GCI82 | Make variable constant | A variable is never reassigned and can be made constant | | | 🚀 | 🚀 | 🚀 | 🚀 || 🚫 |
6262
| GCI83 | Replace Enum ToString() with nameof | When no string format is applied, use nameof instead of ToString() for performance | | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 || 🚫 |
6363
| GCI84 | Avoid async void methods | Use async Task methods instead, for performance, stability and testability | | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 || 🚫 |
6464
| GCI85 | Make type sealed | Seal types that don't need inheritance for performance reasons | | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 || 🚫 |
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
:!sectids:
2+
3+
Variable can be made constant.
4+
5+
== Why is this an issue ?
6+
7+
Unlike variables, constant values are known at compile time and are injected as is in the code, requiring no runtime processing and therefore reducing the environmental footprint.
8+
Although good compilers will const eligible variables by themselves, it is still good practice to declare them constant, as it makes the code intent clearer.
9+
10+
=== When can it be ignored ?
11+
12+
This rule should not be ignored.
13+
14+
== Non compliant examples
15+
16+
[source, java]
17+
----
18+
public class MyClass {
19+
20+
private final Logger logger = Logger.getLogger("");
21+
22+
private Object myNonFinalAndNotReassignedObject = new Object(); // Noncompliant, object initialized but neither used nor reassigned
23+
private String varDefinedInClassNotReassigned = "0"; // Noncompliant, object initialized and used but not reassigned
24+
private String varDefinedInClassNotUsed = "0"; // Noncompliant, simple type object initialized but neither used nor reassigned
25+
26+
void localVariableNotReassigned() {
27+
String y4 = "10"; // Noncompliant, local variable initialized but not reassigned
28+
29+
logger.info(y4);
30+
logger.info(varDefinedInClassNotReassigned);
31+
}
32+
33+
}
34+
----
35+
36+
== Compliant examples
37+
38+
[source, java]
39+
----
40+
public class MyClass {
41+
42+
private final Logger logger = Logger.getLogger(""); // Compliant, object initialized and used (never reassigned but ok because "final" keyword used)
43+
44+
private Object myNonFinalAndReassignedObject = new Object(); // Compliant, reassigned in method
45+
private final Object myFinalAndNotReassignedObject = new Object(); // Compliant, object initialized and never reassigned but 'final' keyword used
46+
47+
private static final String CONSTANT = "toto"; // Compliant, because 'static' and 'final' keywords
48+
private String varDefinedInClassReassigned = "0"; // Compliant, reassigned in method
49+
private String varDefinedInConstructorReassigned = "1"; // Compliant, reassigned in constructor
50+
51+
public MyClass() {
52+
varDefinedInConstructorReassigned = "3";
53+
logger.info(varDefinedInConstructorReassigned);
54+
}
55+
56+
void localVariableReassigned() {
57+
String y1 = "10"; // Compliant, reassigned above
58+
final String PI = "3.14159"; // Compliant, not reassigned but 'final' keyword used
59+
60+
y1 = "titi";
61+
62+
logger.info(y1);
63+
logger.info(PI);
64+
}
65+
66+
void localVariableIncrement() {
67+
String y2 = "10"; // Compliant, reassigned above
68+
y2 += "titi";
69+
logger.info(y2);
70+
}
71+
72+
void localIntVariableIncrement() {
73+
int y3 = 10; // Compliant, reassigned above
74+
++y3;
75+
logger.info(y3+"");
76+
}
77+
78+
void classVariableReassigned() {
79+
varDefinedInClassReassigned = "1";
80+
81+
logger.info(varDefinedInClassReassigned);
82+
logger.info(varDefinedInClassNotReassigned);
83+
logger.info(CONSTANT);
84+
}
85+
86+
void classVariableReassignedBis() {
87+
varDefinedInClassReassigned = "2"; // method to avoid sonarqube error asking for moving class variable "varDefinedInClassReassigned" to local variable method
88+
myNonFinalAndReassignedObject = new Object();
89+
90+
logger.info(varDefinedInClassReassigned);
91+
logger.info(myNonFinalAndReassignedObject);
92+
logger.info(myFinalAndNotReassignedObject);
93+
}
94+
95+
}
96+
----

0 commit comments

Comments
 (0)