diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d1ce1f60..2097d1d64 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Deleted +- [#243](https://github.com/green-code-initiative/ecoCode/pull/243) Deprecate rule EC4 for Java because not applicable + ## [1.4.1] - 2023-12-04 ### Changed diff --git a/RULES.md b/RULES.md index 02b66838c..b32333e3f 100644 --- a/RULES.md +++ b/RULES.md @@ -29,7 +29,7 @@ Some are applicable for different technologies. | EC35 | Using try...catch calls (on File Not Found Exception) | When an exception is thrown, a variable (the exception itself) is created in the catch block and destroyed at the end of the block. Creating this variable and destroying it consumes CPU cycles and RAM unnecessarily. That is why it is important not to use this construction and to prefer, as much as possible, a logical test. This new rule replace old EC34 only for a particular use case (FileNotFoundException) | [cnumr best practices (3rd edition) BP_047 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | 🚀 | ✅ | 🚀 | ✅ | 🚀 | | EC22 | The use of methods for basic operations | Using methods for basic operations consumes additional system resources. The interpreter must in effect and solve the objects and then the methods, just to carry out these simple operations of the language. | [cnumr best practices (3rd edition) BP_048 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | 🚀 | ✅ | 🚀 | 🚀 | 🚀 | | ??? | Call a DOM element multiple times without caching (linter key : `@ecocode/no-multiple-access-dom-element`) | Access to the Document Object Model (DOM) is costly in terms of CPU resources (CPU cycles). Also, when you use the same DOM element from JavaScript multiple times, store its reference in a variable so that you do not go through the DOM again for the same element. | [cnumr best practices (3rd edition) BP_049](https://github.com/cnumr/best-practices/blob/main/chapters/BP_049_fr.md) | 🚫 | 🚫 | ✅ | 🚫 | 🚫 | -| EC4 | Use global variables | When using a global variable, the interpretation engine must check: 1) that it exists in the current scope, in the one above, etc. ; 2) the variable has a value; 3) ... To avoid all these checks, it is often possible to pass the useful variables as arguments of routines, making them local. This process saves computational time (CPU cycles). | [cnumr best practices (3rd edition) BP_050 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | ✅ | ✅ | 🚀 | ✅ | 🚀 | +| EC4 | Use global variables | When using a global variable, the interpretation engine must check: 1) that it exists in the current scope, in the one above, etc. ; 2) the variable has a value; 3) ... To avoid all these checks, it is often possible to pass the useful variables as arguments of routines, making them local. This process saves computational time (CPU cycles). | [cnumr best practices (3rd edition) BP_050 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | 🚫 | ✅ | 🚀 | ✅ | 🚀 | | EC53 | Using arrays in foreach loops | foreach deduplicates items in a list before starting the enumeration. It is therefore generally more economical to use a simple for loop when you have a good command of the collection. | [cnumr best practices (3rd edition) BP_053 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | ✅ | 🚀 | 🚀 | 🚫 | 🚀 | | EC7 | Rewrite native getter/setters | Overloading them lengthens the compilation and execution times of these methods, which are usually much better optimized by the language than by the developer. | [cnumr best practices (3rd edition) BP_062 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | 🚀 | 🚀 | 🚀 | ✅ | 🚀 | | EC63 | Unnecessarily assigning values to variables | Avoid declaring and using variables when it is not indis-thinkable. Indeed, each allocation corresponds to the RAM occupied. | [cnumr best practices (3rd edition) BP_063 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | ✅ | 🚀 | 🚀 | 🚀 | 🚀 | @@ -65,3 +65,4 @@ This table lists rules proposed by the community but refused in ecoCode plugins |----------|------------------------------------------|------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------| | Python | Use numpy array instead of standard list | The use of numpy library to perform array manipulation is more energy efficient than the use of the standard list functions. | [Github discussion with measures](https://github.com/green-code-initiative/ecoCode/issues/132) | | HTML | HTML page must contain a doctype tag | The difference in performance is negligible, this rule is more related to the user experience. | [Github discussion with sources](https://github.com/green-code-initiative/ecoCode/issues/103) | +| Java | Avoid using global variables | Global variables do not exist in Java. | [Github discussion with sources](https://github.com/green-code-initiative/ecoCode/issues/233) | diff --git a/ecocode-rules-specifications/src/main/rules/EC4/java/EC4.asciidoc b/ecocode-rules-specifications/src/main/rules/EC4/java/EC4.asciidoc index f8763b4ed..0aba43f63 100644 --- a/ecocode-rules-specifications/src/main/rules/EC4/java/EC4.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC4/java/EC4.asciidoc @@ -1,18 +1,21 @@ -Prefer local variables as parameters +*This rule is deprecated because not applicable to Java, and will be removed soon.* + +== Why is this an issue? When calling a global variable, the interpretation engine must check that it exists in all the scopes, that it has a value, etc. Passing global variables as arguments gives them the status of local variables inside the function, thus saving computing time (CPU cycles). -## CASE 1 (Avoid as possible) +=== CASE 1 (Avoid as possible) You are back on the service code. You see that the `func1()` uses `globalVariabl1`. Okay, but whats its value by now ? How does it change ? Who mutates the `globalVariabl1` before it comes to this function ? What have been the sequence of all these mutations ? You would have no idea. It will be quite difficult to figure all this out. -## CASE 2 (Recommended) +=== CASE 2 (Recommended) You are back to you code, and see that the `func0()` fetches something and then passes it to `func1(param1)` as a parameter. You clearly know what the data is, how does it gets here. -## Noncompliant Code Example +=== Noncompliant Code Example -```java +[source,java] +---- var aGlobal = new String('Hello'); function globalLength(){ @@ -21,11 +24,12 @@ function globalLength(){ } globalLength(); -``` +---- -## Compliant Solution +== Compliant Solution -```java +[source,java] +---- var aGlobal = new String('Hello'); function someVarLength(str){ @@ -34,4 +38,4 @@ function someVarLength(str){ } somVarLength(aGlobal); -``` +---- diff --git a/ecocode-rules-specifications/src/main/rules/EC4/java/EC4.json b/ecocode-rules-specifications/src/main/rules/EC4/java/EC4.json new file mode 100644 index 000000000..0b963543b --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC4/java/EC4.json @@ -0,0 +1,3 @@ +{ + "status": "deprecated" +} diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidUsingGlobalVariablesCheck.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidUsingGlobalVariablesCheck.java index 98bb254b4..26a6dbddc 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidUsingGlobalVariablesCheck.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/AvoidUsingGlobalVariablesCheck.java @@ -17,9 +17,6 @@ */ package fr.greencodeinitiative.java.checks; -import java.util.Arrays; -import java.util.List; - import com.google.re2j.Pattern; import org.sonar.check.Rule; import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; @@ -28,6 +25,15 @@ import org.sonar.plugins.java.api.tree.VariableTree; import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey; +import java.util.Arrays; +import java.util.List; + +/** + * Check to avoid using global variables. + * + * @deprecated because not applicable to Java language, to be removed soon + */ +@Deprecated(forRemoval = true) @Rule(key = "EC4") @DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "D4") public class AvoidUsingGlobalVariablesCheck extends IssuableSubscriptionVisitor { @@ -42,7 +48,6 @@ public List nodesToVisit() { @Override public void visitNode(Tree tree) { - if (tree.is(Kind.STATIC_INITIALIZER)) { reportIssue(tree, String.format(ERROR_MESSAGE, tree)); } @@ -57,4 +62,5 @@ public void visitNode(Tree tree) { } } } + } diff --git a/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/AvoidUsingGlobalVariablesCheckCheckTest.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/AvoidUsingGlobalVariablesCheckCheckTest.java index f9e270e6c..31e05b156 100644 --- a/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/AvoidUsingGlobalVariablesCheckCheckTest.java +++ b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/AvoidUsingGlobalVariablesCheckCheckTest.java @@ -20,6 +20,7 @@ import org.junit.jupiter.api.Test; import org.sonar.java.checks.verifier.CheckVerifier; +@Deprecated class AvoidUsingGlobalVariablesCheckCheckTest { @Test @@ -30,4 +31,4 @@ void test() { .verifyIssues(); } -} \ No newline at end of file +}