-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[EC82][Java] Variable can be made constant
- Loading branch information
PREISNER Julien
committed
May 30, 2024
1 parent
df4c4e2
commit b9d9228
Showing
4 changed files
with
73 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
ecocode-rules-specifications/src/main/rules/EC82/java/EC82.asciidoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
:!sectids: | ||
|
||
Variable can be made constant. | ||
|
||
## Why is this an issue ? | ||
|
||
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. | ||
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. | ||
|
||
### When can it be ignored ? | ||
|
||
This rule should not be ignored. | ||
|
||
## Non-compliant examples | ||
|
||
```java | ||
public class MyClass { | ||
private int varDefinedInClassNotReassigned = 0; // Noncompliant {{Make variable constant}} | ||
private int varDefinedInClassReassigned = 0; | ||
|
||
void changeVarDefinedInClassReassigned() { | ||
varDefinedInClassReassigned = 1; | ||
System.out.println("varDefinedInClassReassigned = " + varDefinedInClassReassigned); | ||
System.out.println("varDefinedInClassNotReassigned = " + varDefinedInClassNotReassigned); | ||
} | ||
|
||
void simpleMethod() { | ||
String varDefinedInMethodNotReassigned = "hello"; // Noncompliant | ||
String varDefinedInMethodReassigned = "hello"; | ||
varDefinedInMethodReassigned = "bye"; | ||
|
||
System.out.println("varDefinedInMethodNotReassigned = " + varDefinedInMethodNotReassigned); | ||
System.out.println("varDefinedInMethodReassigned = " + varDefinedInMethodReassigned); | ||
} | ||
} | ||
``` | ||
## Compliant examples | ||
|
||
```java | ||
public class MyClass { | ||
private static final int VAR_DEFINED_IN_CLASS_NOT_REASSIGNED = 0; // Compliant | ||
private static final int VAR_DEFINED_IN_METHOD_NOT_REASSIGNED = 0; // Compliant | ||
private int varDefinedInClassReassigned = 0; | ||
|
||
void changeVarDefinedInClassReassigned() { | ||
varDefinedInClassReassigned = 1; | ||
System.out.println("varDefinedInClassReassigned = " + varDefinedInClassReassigned); | ||
System.out.println("varDefinedInClassNotReassigned = " + VAR_DEFINED_IN_CLASS_NOT_REASSIGNED); | ||
} | ||
|
||
void simpleMethod() { | ||
String varDefinedInMethodReassigned = "hello"; | ||
varDefinedInMethodReassigned = "bye"; | ||
|
||
System.out.println("varDefinedInMethodNotReassigned = " + VAR_DEFINED_IN_METHOD_NOT_REASSIGNED); | ||
System.out.println("varDefinedInMethodReassigned = " + varDefinedInMethodReassigned); | ||
} | ||
} | ||
``` |