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

[EC63] Deprecation of rule EC63 for java #258

Merged
merged 4 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Deleted

- [#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

### Added
Expand Down
11 changes: 6 additions & 5 deletions RULES.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ Some are applicable for different technologies.
| 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/) | ✅ | 🚀 | 🚀 | 🚀 | 🚀 |
| 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/) | 🚀 | ✅ | 🚀 | 🚫 <br>[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/) | ✅ | ✅ | 🚀 | 🚫 | 🚫 |
| EC69 | Calling a function in the declaration of a for loop | Avoid calling the function each time the loop is iterated. | [cnumr best practices (3rd edition) BP_069 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | ✅ | ✅ | 🚀 | ✅ | 🚀 |
Expand Down Expand Up @@ -60,10 +59,12 @@ Some are applicable for different technologies.

This table lists rules proposed by the community but deprecated in ecoCode plugins with the justification. These rules will be completely deleted in next releases and moved to bottom deleted rules array.

| Rule key | Language | Name | Description | Invalidation |
|----------|---------------------|-------------------------------------|----------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------|
| 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)|
| 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) |
| Rule key | Language | Name | Description | Invalidation |
|----------|---------------------|----------------------------------------------|----------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------|
| 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)|
| EC63 | Java | Unnecessarily assigning values to variables | There are already 3 native SonarQube rules. | [Github discussion with sources](https://github.com/green-code-initiative/ecoCode/pull/230) |
| 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) |


## Refused / Deleted rules

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
*This rule is deprecated because there are already Sonarqube native rules for the same things, and will be removed soon.*

== Why is this an issue?

Do not unnecessarily assign values to variables. It increases the use of RAM memory.

## Noncompliant Code Example
== Noncompliant Code Example

```java
[source,java]
----
String var1 = getValue();
return var1;

String var2 = "hello"
var2 = "world" //Non compliant cause never assigned
```
----

## Compliant Solution
== Compliant Solution

```java
[source,java]
----
return getValue();
```
----
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"status": "deprecated"
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@
import javax.annotation.CheckForNull;
import java.util.*;

/**
* @deprecated not applicable because of existing Sonarqube native rules :
* - unused variable : https://rules.sonarsource.com/java/tag/unused/RSPEC-1481/
* - useless assignment : https://rules.sonarsource.com/java/tag/unused/RSPEC-1854
* - immediately return : https://rules.sonarsource.com/java/RSPEC-1488/
*/
@Deprecated(forRemoval = true)
@Rule(key = "EC63")
@DeprecatedRuleKey(repositoryKey = "greencodeinitiative-java", ruleKey = "S63")
public class UnnecessarilyAssignValuesToVariables extends BaseTreeVisitor implements JavaFileScanner {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.junit.jupiter.api.Test;
import org.sonar.java.checks.verifier.CheckVerifier;

@Deprecated
class UnnecessarilyAssignValuesToVariablesTest {

@Test
Expand Down