diff --git a/CHANGELOG.md b/CHANGELOG.md index e3f9a8643..5e0a9dcc7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Deleted +- [#240](https://github.com/green-code-initiative/ecoCode/issues/240) Deprecate rule EC53 for Java because of no good arguments and not enough green measures - [#258](https://github.com/green-code-initiative/ecoCode/pull/258) Deprecate rule EC63 for Java because there are already 3 native Sonarqube rules that cover the same use cases ## [1.4.2] - 2023-12-05 diff --git a/RULES.md b/RULES.md index f930d6169..ad214de2d 100644 --- a/RULES.md +++ b/RULES.md @@ -30,7 +30,6 @@ Some are applicable for different technologies. | 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/) | 🚫 | ✅ | 🚀 | ✅ | 🚀 | -| 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/) | 🚀 | 🚀 | 🚀 | ✅ | 🚀 | | EC66 | Use single quote (') instead of quotation mark (") | The shape using the quotation marks allows the developer to insert variables that will be substituted at run time. But if the string does not have a variable, use quotes instead. Thus, language will not look for variables to subtituture, which will reduce the consumption of CPU cycles. | [cnumr best practices (3rd edition) BP_066 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | 🚀 | ✅ | 🚀 | 🚫
[see](https://github.com/green-code-initiative/ecoCode-python/issues/4) | 🚀 | | EC67 | Use the $i++ variable during an iteration | The $i++ form has the disadvantage of generating a tem-porary variable during incrementation, which is not the case with the ++$i form. | [cnumr best practices (3rd edition) BP_067 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | ✅ | ✅ | 🚀 | 🚫 | 🚫 | @@ -63,6 +62,7 @@ This table lists rules proposed by the community but deprecated in ecoCode plugi |----------|---------------------|---------------------------------------------|----------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | EC4 | 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) | | EC34 | Java / Python / PHP | Using try...catch...finally calls | Implementation is too simple (only detection of presence of "try" statement) AND replaced by `EC35` rule | Github discussion with measures : [general/java](https://github.com/green-code-initiative/ecoCode/pull/128) / [python](https://github.com/green-code-initiative/ecoCode-python/pull/6) / [php](https://github.com/green-code-initiative/ecoCode-php/pull/10) | +| EC53 | Java | Using arrays in foreach loops | No good arguments and not enough green measures. | [Github discussion with sources](https://github.com/green-code-initiative/ecoCode/issues/240) | | EC63 | Java | Unnecessarily assigning values to variables | There are already 3 native SonarQube rules for Java. | [Github discussion with sources](https://github.com/green-code-initiative/ecoCode/pull/258) | ## Refused / Deleted rules diff --git a/ecocode-rules-specifications/src/main/rules/EC53/java/EC53.asciidoc b/ecocode-rules-specifications/src/main/rules/EC53/java/EC53.asciidoc index 924efda63..1a13d3c7c 100644 --- a/ecocode-rules-specifications/src/main/rules/EC53/java/EC53.asciidoc +++ b/ecocode-rules-specifications/src/main/rules/EC53/java/EC53.asciidoc @@ -1,21 +1,27 @@ +*This rule is deprecated because there aren't enough good arguments and not enough green measures / benchmarks, and will be removed soon.* + +== Why is this an issue? + Using List instead of Arrays with Foreach save CPU cycles calculations and RAM consumption. -## Noncompliant Code Example +== Noncompliant Code Example -```java +[source,java] +---- private final Integer[] intArray = new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; for (Integer i : intArray) { // ... } -``` +---- -## Compliant Solution +== Compliant Solution -```java +[source,java] +---- private final List intList = Arrays.asList(new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); for (Integer i : intList) { // ... } -``` +---- diff --git a/ecocode-rules-specifications/src/main/rules/EC53/java/EC53.json b/ecocode-rules-specifications/src/main/rules/EC53/java/EC53.json new file mode 100644 index 000000000..cf4002b76 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC53/java/EC53.json @@ -0,0 +1,3 @@ +{ + "status": "deprecated" +} \ No newline at end of file diff --git a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/UseCorrectForLoop.java b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/UseCorrectForLoop.java index 05e46a4ab..35c7f677f 100644 --- a/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/UseCorrectForLoop.java +++ b/java-plugin/src/main/java/fr/greencodeinitiative/java/checks/UseCorrectForLoop.java @@ -27,6 +27,11 @@ import org.sonar.plugins.java.api.tree.Tree.Kind; import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey; +/** + * @deprecated there aren't enough good arguments and not enough green measures / benchmarks + * (check discussion on https://github.com/green-code-initiative/ecoCode/issues/240) + */ +@Deprecated(forRemoval = true) @Rule(key = "EC53") @DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "S53") public class UseCorrectForLoop extends IssuableSubscriptionVisitor { diff --git a/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/UseCorrectLoopCheckTest.java b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/UseCorrectLoopCheckTest.java index 815d097a1..a853a6f52 100644 --- a/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/UseCorrectLoopCheckTest.java +++ b/java-plugin/src/test/java/fr/greencodeinitiative/java/checks/UseCorrectLoopCheckTest.java @@ -20,6 +20,7 @@ import org.junit.jupiter.api.Test; import org.sonar.java.checks.verifier.CheckVerifier; +@Deprecated class UseCorrectLoopCheckTest { @Test