From b9d92285c01f0d713224bd85d270950b579125fa Mon Sep 17 00:00:00 2001 From: PREISNER Julien Date: Thu, 30 May 2024 13:47:46 +0200 Subject: [PATCH] [EC82][Java] Variable can be made constant --- CHANGELOG.md | 9 +++ RULES.md | 2 +- .../src/main/rules/EC82/EC82.json | 5 +- .../src/main/rules/EC82/java/EC82.asciidoc | 59 +++++++++++++++++++ 4 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 ecocode-rules-specifications/src/main/rules/EC82/java/EC82.asciidoc diff --git a/CHANGELOG.md b/CHANGELOG.md index 21a7ee4b..bdc68db3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/RULES.md b/RULES.md index 05310c4b..8d4014d7 100644 --- a/RULES.md +++ b/RULES.md @@ -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 | | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | ✅ | 🚫 | diff --git a/ecocode-rules-specifications/src/main/rules/EC82/EC82.json b/ecocode-rules-specifications/src/main/rules/EC82/EC82.json index dba18615..4fdcf46d 100644 --- a/ecocode-rules-specifications/src/main/rules/EC82/EC82.json +++ b/ecocode-rules-specifications/src/main/rules/EC82/EC82.json @@ -10,5 +10,8 @@ "eco-design", "ecocode" ], - "defaultSeverity": "Minor" + "defaultSeverity": "Minor", + "compatibleLanguages": [ + "JAVA" + ] } diff --git a/ecocode-rules-specifications/src/main/rules/EC82/java/EC82.asciidoc b/ecocode-rules-specifications/src/main/rules/EC82/java/EC82.asciidoc new file mode 100644 index 00000000..80f6e432 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC82/java/EC82.asciidoc @@ -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); + } +} +```