Skip to content

Commit

Permalink
[EC82][Java] Variable can be made constant
Browse files Browse the repository at this point in the history
  • Loading branch information
PREISNER Julien committed May 30, 2024
1 parent df4c4e2 commit 2c75496
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Deleted

## [1.5.5]

### Added
- [#TODO](https://github.com/green-code-initiative/ecoCode/pull/TODO) [EC82] [Java] Variable can be made constant

### Changed

### Deleted

## [1.5.4] - 2024-05-24

### Added
Expand Down
2 changes: 1 addition & 1 deletion RULES.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Some are applicable for different technologies.
| EC78 | 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. | || 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 |
| EC79 | Free resources | try-with-resources Statement needs to be implemented for any object that implements the AutoCloseable interface, it save computer resources. | || 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 |
| EC81 | Specify struct layouts | When possible, specify struct layouts to optimize their memory footprint | | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 || 🚫 |
| EC82 | Make variable constant | A variable is never reassigned and can be made constant | | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 || 🚫 |
| EC82 | Make variable constant | A variable is never reassigned and can be made constant | | 🚧 | 🚀 | 🚀 | 🚀 | 🚀 || 🚫 |
| EC83 | Replace Enum ToString() with nameof | When no string format is applied, use nameof instead of ToString() for performance | | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 || 🚫 |
| EC84 | Avoid async void methods | Use async Task methods instead, for performance, stability and testability | | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 || 🚫 |
| EC85 | Make type sealed | Seal types that don't need inheritance for performance reasons | | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 || 🚫 |
Expand Down
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);
}
}
```

0 comments on commit 2c75496

Please sign in to comment.