Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Draft][EC82][Java] Variable can be made constant #328

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
5 changes: 4 additions & 1 deletion ecocode-rules-specifications/src/main/rules/EC82/EC82.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
"eco-design",
"ecocode"
],
"defaultSeverity": "Minor"
"defaultSeverity": "Minor",
"compatibleLanguages": [
"JAVA"
]
}
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);
}
}
```
Loading