From cb47ac7ec94e69f7da2aedb84301250ba699755d Mon Sep 17 00:00:00 2001 From: Hachemi ATROUNE Date: Thu, 30 May 2024 13:15:02 +0200 Subject: [PATCH] add EC82 rule: use final variable --- RULES.md | 2 +- .../src/main/rules/EC82/java/EC82.asciidoc | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 ecocode-rules-specifications/src/main/rules/EC82/java/EC82.asciidoc diff --git a/RULES.md b/RULES.md index 05310c4b0..254f83901 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/java/EC82.asciidoc b/ecocode-rules-specifications/src/main/rules/EC82/java/EC82.asciidoc new file mode 100644 index 000000000..37e7d1b16 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC82/java/EC82.asciidoc @@ -0,0 +1,31 @@ +:!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 void Ec82NonCompliant() { + String test = "test"; // Non compliant, i is never reassigned and can be made constant + System.out.println(test); +} +``` +## Compliant examples + +```java +final String test = "test"; + +public void Ec82Compliant() +{ + System.out.println(test); +} +```