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 aa035865..8cae3e36 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 @@ -14,7 +14,7 @@ @Rule( key = "EC4", name = "Developpement", - description = "
Prefer local variables to globals
", + description = "Prefer local variables to globals", priority = Priority.MINOR, tags = {"bug"} ) diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC1.html b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC1.html index 673a29f9..df52ae34 100644 --- a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC1.html +++ b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC1.html @@ -1,20 +1,21 @@ -Using Spring repository in loop induced unnecessary calculation by the cpu so unless energy consumption.
-- private final List<Integer> ids = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); +The use of Spring repository in a loop induces unnecessary calculations by the CPU and therefore superfluous energy consumption.
- List<Employee> employees = new ArrayList<>(); +Example of non-compliant code
++private final List<Integer> ids = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); - for (Integer id: ids) { - Optional<Employee> employee = employeeRepository.findById(id); // Noncompliant - if (employee.isPresent()) { - employees.add(employee.get()); - } - } +List<Employee> employees = new ArrayList<>(); +for (Integer id: ids) { + Optional<Employee> employee = employeeRepository.findById(id); // Noncompliant + if (employee.isPresent()) { + employees.add(employee.get()); + } +}+Compliant Solution
- private final List<Integer> ids = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); - List<Employee> employees = employeeRepository.findAllById(ids); +private final List<Integer> ids = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); +List<Employee> employees = employeeRepository.findAllById(ids);diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC2.html b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC2.html index 7e5fcc2a..442f8939 100644 --- a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC2.html +++ b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC2.html @@ -1,31 +1,34 @@ -If we are using too many conditional if-else statements it will impact performance since JVM will have to compare the conditions. We can think of using a switch statement instead of multiple if-else if possible. Switch statement has a performance advantage over if – else.
++ If we are using too many conditional
-if
-else
statements it will impact performance since JVM will have to compare the conditions. + We can think of using a switch statement instead of multipleif
-else
if possible. + Switch statement has a performance advantage overif
-else
. +Non-compliant Code Example
+Example of non-compliant code
- int index = 1; - int nb = 2; - - if (nb > index) { - nb = nb + index; - } else { - nb = nb - 1; - } - if (nb != index + 1) { - nb = nb + index; - } else { - nb = nb - 1; - } - +int index = 1; +int nb = 2; +if (nb > index) { + nb = nb + index; +} else { + nb = nb - 1; +} +if (nb != index + 1) { + nb = nb + index; +} else { + nb = nb - 1; +}+Compliant Code Example
- int index = 1; - int nb = 2; +int index = 1; +int nb = 2; - if (nb > index) { - nb = nb + index; - } else { - nb = nb - 1; - } +if (nb > index) { + nb = nb + index; +} else { + nb = nb - 1; +}diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC27.html b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC27.html index a94d1ba2..0162c672 100644 --- a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC27.html +++ b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC27.html @@ -1,4 +1,4 @@ -Using System.arraycopy to copy arrays
+Using
System.arraycopy
to copy arraysPrograms spend most of the time in loops. These can be resource consuming, especially when they integrate heavy processing (IO access). Moreover, the size of the data and processing inside the loops will not allow full use of hardware mechanisms such as the cache or compiler optimization mechanisms.
-
For example, an array copy is potentially a non-performance source if it is poorly designed. Indeed, the use of a single copy loop can be twice as consuming as dedicated methods.
@@ -7,18 +7,20 @@ We can also usecopyOf
orclone
that are slightly less efficient.
The looping method will be outlawed.Noncompliant Code Example
+ +Example of non-compliant code
- int len = array.length; - boolean[] copy = new boolean[array.length]; - for (int i = 0; i < len; i++) { - copy[i] = array[i]; // Noncompliant - } - return copy; +int len = array.length; +boolean[] copy = new boolean[array.length]; +for (int i = 0; i < len; i++) { + copy[i] = array[i]; // Noncompliant +} +return copy;+Compliant Solution
- int[] copy = new int[array.length]; - System.arraycopy(array, 0, copy, 0, array.length); - return copy; +int[] copy = new int[array.length]; +System.arraycopy(array, 0, copy, 0, array.length); +return copy;diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC28.html b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC28.html index d37bcef2..8aa31575 100644 --- a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC28.html +++ b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC28.html @@ -1,29 +1,28 @@Optimize read file exception
-Noncompliant Code Example
-- public void readPreferences(String filename) { - //... - InputStream in = null; - try { - in = new FileInputStream(filename); - } catch (FileNotFoundException e) { - logger.log(e); - } - in.read(...); - //... - } +Example of non-compliant code
++public void readPreferences(String filename) { + //... + InputStream in = null; + try { + in = new FileInputStream(filename); + } catch (FileNotFoundException e) { + logger.log(e); + } + in.read(...); + //... +}+Compliant Solution
- public void readPreferences(String filename) - throws IllegalArgumentException, - FileNotFoundException, IOException { - if (filename == null) { - throw new IllegalArgumentException ("filename is null"); - } //if - //... - InputStream in = new FileInputStream(filename); - //... - } +public void readPreferences(String filename) throws IllegalArgumentException, FileNotFoundException, IOException { + if (filename == null) { + throw new IllegalArgumentException ("filename is null"); + } + //... + InputStream in = new FileInputStream(filename); + //... +}diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC3.html b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC3.html index b347d714..e78e5cc3 100644 --- a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC3.html +++ b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC3.html @@ -1,19 +1,20 @@When iterating over any collection, fetch the size of the collection in advance to avoid fetching it on each iteration, this saves CPU cycles, and therefore consumes less power. The example provided below illustrates what should be avoided.
-Noncompliant Code Example
-- List<String> objList = getData(); - for (int i = 0; i < objList.size(); i++) { // Noncompliant - // execute code - } +Example of non-compliant code
++List<String> objList = getData(); +for (int i = 0; i < objList.size(); i++) { // Noncompliant + // execute code +}+Compliant Solution
- List<String> objList = getData(); +List<String> objList = getData(); - int size = objList.size(); - for (int i = 0; i < size; i++) { - // execute code - } +int size = objList.size(); +for (int i = 0; i < size; i++) { + // execute code +}diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC32.html b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC32.html index 3fb0f434..b3a66ed6 100644 --- a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC32.html +++ b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC32.html @@ -1,18 +1,21 @@- If you know in advance how many characters would be appended, initialize builder/buffer with the appropriate size. - They will thus never have to be resized, It saves CPU cycles so unless energy consumption. + If you know in advance how many characters would be appended, initialize builder/buffer with the appropriate size. + They will thus never have to be resized. + This saves CPU cycles and therefore consumes less energy.
-Noncompliant Code Example
+ +Example of non-compliant code
- StringBuilder sb = new StringBuilder(); // Noncompliant - for (int i = 0; i < 100; i++) { - sb.append(...); - } +StringBuilder sb = new StringBuilder(); // Noncompliant +for (int i = 0; i < 100; i++) { + sb.append(...); +}+Compliant Solution
- StringBuilder sb = new StringBuilder(100); - for (int i = 0; i < 100; i++) { - sb.append(...); - } +StringBuilder sb = new StringBuilder(100); +for (int i = 0; i < 100; i++) { + sb.append(...); +}diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC4.html b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC4.html index a3ee9fe6..8867bde7 100644 --- a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC4.html +++ b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC4.html @@ -1,26 +1,38 @@ +Prefer local variables as parameters
+ +When calling a global variable, the interpretation engine must check that it exists in all scopes, that it has a value, etc. Passing global variables as arguments gives them the status of local variables inside the function, which saves computation time (CPU cycles).
+ +CASE 1 (Avoid as much as possible)
- Prefer local variables as parameters -
-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 statut of local variables inside the function, thus saving computing time (CPU cycles). + You are back on the service code. + You see that
+ +func1()
usesglobalVariabl1
. + Alright, but what's its value now? How does it change? + Who mutates theglobalVariabl1
before it gets to this function? + What was the sequence of all these mutations? + You would have no idea. + It will be quite difficult to understand all this.CASE 2 (Recommended)
-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):
-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. -
-example:
+ You are back to your code and see that
- var aGlobal = new String('Hello');
- function globalLength(){
- length = aGlobal.length;
- console.log(length);
- }
- globalLength();
-
- var aGlobal = new String('Hello');
- function someVarLength(str){
- length = str.length;
- console.log(length);
- }
- somVarLength(aGlobal);func0()
is getting something and then passing it tofunc1(param1)
as a parameter. + You clearly know what the data is, and how it gets here. + + +Example:
+ ++var aGlobal = new String('Hello'); +function globalLength() { + length = aGlobal.length; + console.log(length); +} +globalLength(); + +var aGlobal = new String('Hello'); +function someVarLength(str) { + length = str.length; + console.log(length); +} +somVarLength(aGlobal); +diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC5.html b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC5.html index 1e5c63ad..5ac54225 100644 --- a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC5.html +++ b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC5.html @@ -1,19 +1,20 @@ -Use PreparedStatement instead of Statement, that's because SQL will validate the query only once, whereas if you just use a statement it will validate the query each time. - This induced unnecessary calculation by the CPU so unless energy consumption.
-Noncompliant Code Example
+Use
+ +PreparedStatement
instead ofStatement
, because SQL will only commit the query once, whereas if you used only one statement, it would commit the query every time and thus induce unnecessary calculations by the CPU and therefore superfluous energy consumption.Example of non-compliant code
- public void select() { - Statement statement = connection.createStatement(); - statement.executeUpdate("INSERT INTO persons(id, name) VALUES(2, 'Toto')"); // Noncompliant - } +public void select() { + Statement statement = connection.createStatement(); + statement.executeUpdate("INSERT INTO persons(id, name) VALUES(2, 'Toto')"); // Noncompliant +}+Compliant Solution
- public void select() { - PreparedStatement statement = connection.prepareStatement(INSERT INTO persons(id, name) VALUES(?, ?)); +public void select() { + PreparedStatement statement = connection.prepareStatement("INSERT INTO persons(id, name) VALUES(?, ?)"); - statement.setInt(1, 2); - statement.setString(2, "Toto"); - statement.executeQuery(); - } + statement.setInt(1, 2); + statement.setString(2, "Toto"); + statement.executeQuery(); +}diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC53.html b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC53.html index b1d548c9..64d920ad 100644 --- a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC53.html +++ b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC53.html @@ -1,18 +1,19 @@ -Using List instead of Arrays with Foreach save CPU cycles calculations and RAM consumption.
-Noncompliant Code Example
-- private final Integer[] intArray = new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; +Using List instead of Arrays with Foreach will save CPU cycles calculations and RAM consumption.
- for (Integer i : intArray) { - ... - } +Example of non-compliant code
++private final Integer[] intArray = new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; +for (Integer i : intArray) { + // ... +}+Compliant Solution
- private final List<Integer> intList = Arrays.asList(new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); +private final List<Integer> intList = Arrays.asList(new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); - for (Integer i : intList) { - ... - } +for (Integer i : intList) { + // ... +}diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC63.html b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC63.html index 4de73773..67a92ddb 100644 --- a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC63.html +++ b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC63.html @@ -1,12 +1,15 @@ -Do not unnecessarily assign values to variables. It increase unless RAM memory usage.
-Noncompliant Code Example
+Do not unnecessarily assign values to variables. It increases the use of RAM memory.
+ +Example of non-compliant code
String var1 = getValue(); return var1; String var2 = "hello" var2 = "world" // Noncompliant because it is never assigned -+Compliant Solution
-return getValue();++return getValue(); +diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC67.html b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC67.html index baa5e6e4..542d34b8 100644 --- a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC67.html +++ b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC67.html @@ -1,9 +1,14 @@ -The form $i++ creates a temporary variable whereas ++$i does not. It save CPU cycles.
-Noncompliant Code Example
++ The form
+ +i++
creates a temporary variable whereas++i
does not. + It save CPU cycles. +Example of non-compliant code
- i++ // Noncompliant +i++ // Noncompliant+Compliant Solution
- ++i +++idiff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC69.html b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC69.html index 5d8c46aa..39932764 100644 --- a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC69.html +++ b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC69.html @@ -1,23 +1,25 @@ -Do not call a function when declaring a for-type loop in order to avoid function calls each iterations. It saves CPU cycles.
-Noncompliant Code Example
-- public void foo() { - for (int i = 0; i < getMyValue(); i++) { // Noncompliant - System.out.println(i); - boolean b = getMyValue() > 6; - } - } ++ Do not call a function when declaring a for-type loop in order to avoid function calls each iteration. + It saves CPU cycles. +
+Example of non-compliant code
++public void foo() { + for (int i = 0; i < getMyValue(); i++) { // Noncompliant + System.out.println(i); + boolean b = getMyValue() > 6; + } +}+Compliant Solution
- - public void foo() { - int myValue = getMyValue(); - for (int i = 0; i < myValue; i++) { - System.out.println(i); - boolean b = getMyValue() > 6; - } - } - +public void foo() { + int myValue = getMyValue(); + for (int i = 0; i < myValue; i++) { + System.out.println(i); + boolean b = getMyValue() > 6; + } +}diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC72.html b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC72.html index bd52cdfe..0cd842ed 100644 --- a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC72.html +++ b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC72.html @@ -1,49 +1,47 @@Executing SQL queries in loop induced unnecessary calculation by the CPU, RAM usage and network transfert.
-Noncompliant Code Example
-- public void foo() { - ... - String baseQuery = "SELECT name FROM users where id = "; - - for (int i = 0; i < 20; i++) { - - String query = baseQuery.concat("" + i); - Statement st = conn.createStatement(); - ResultSet rs = st.executeQuery(query); // Noncompliant - - // iterate through the java resultset - while (rs.next()) { - String name = rs.getString("name"); - System.out.println(name); - } - st.close(); - } - ... - } +Example of non-compliant code
++public void foo() { + // ... + String baseQuery = "SELECT name FROM users where id = "; + + for (int i = 0; i < 20; i++) { + String query = baseQuery.concat("" + i); + Statement st = conn.createStatement(); + ResultSet rs = st.executeQuery(query); // Noncompliant + + // iterate through the java resultset + while (rs.next()) { + String name = rs.getString("name"); + System.out.println(name); + } + st.close(); + } + // ... +}+Compliant Solution
- - public void foo() { - ... - String query = "SELECT name FROM users where id in (0 "; - for (int i = 1; i < 20; i++) { - - query = baseQuery.concat("," + i); - } - - query = baseQuery.concat(")"); - Statement st = conn.createStatement(); - ResultSet rs = st.executeQuery(query); // compliant - - // iterate through the java resultset - while (rs.next()) { - String name = rs.getString("name"); - System.out.println(name); - } - st.close(); - ... - } - +public void foo() { + // ... + String query = "SELECT name FROM users where id in (0 "; + + for (int i = 1; i < 20; i++) { + query = baseQuery.concat("," + i); + } + + query = baseQuery.concat(")"); + Statement st = conn.createStatement(); + ResultSet rs = st.executeQuery(query); // compliant + + // iterate through the java resultset + while (rs.next()) { + String name = rs.getString("name"); + System.out.println(name); + } + st.close(); + // ... +}diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC74.html b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC74.html index 8d977de2..5192b804 100644 --- a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC74.html +++ b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC74.html @@ -1,21 +1,19 @@ -Databases servers have to solves fileds regarding to schema. Knowing and using the schema save CPU cycles and network transfer.
-Noncompliant Code Example
-- public void foo() { - ... - String baseQuery = "SELECT * FROM users"; // Noncompliant - - ... - } +Databases servers have to solve fields regarding the schema. Knowing and using the schema save CPU cycles and network transfer.
+Example of non-compliant code
++public void foo() { + // ... + String baseQuery = "SELECT * FROM users"; // Noncompliant + // ... +}+Compliant Solution
- - public void foo() { - ... - String query = "SELECT id,name, adress FROM users "; - ... - } - +public void foo() { + // ... + String query = "SELECT id,name,address FROM users "; + // ... +}diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC75.html b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC75.html index 67c1c1cc..fded4656 100644 --- a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC75.html +++ b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC75.html @@ -1,41 +1,38 @@- Don't concatenate Strings in loop. User StringBuilder instead.
-
- Strings are immutable so each time you concatenate a String, a new String is created. This is a waste of memory and CPU. + Don't concatenate Strings in loop. User StringBuilder instead.
+ Strings are immutable so each time you concatenate a String, a new String is created. + This is a waste of memory and CPU.Noncompliant Code Example
+Example of non-compliant code
- - public String concatenateStrings(String[] strings) { - String result = ""; - - for (String string : strings) { - result += string; // Noncompliant - } - return result; - } - - public String concatenateStrings2() { - String result = ""; - - for (int i = 0; i < 1000; ++i) { - result += "another"; // Noncompliant - } - return result; - } - +public String concatenateStrings(String[] strings) { + String result = ""; + + for (String string : strings) { + result += string; // Noncompliant + } + return result; +} + +public String concatenateStrings2() { + String result = ""; + + for (int i = 0; i < 1000; ++i) { + result += "another"; // Noncompliant + } + return result; +}Compliant Solution
- - public String concatenateStrings(String[] strings) { - StringBuilder result = new StringBuilder(); - - for (String string : strings) { - result.append(string); - } - return result.toString(); - } - + public String concatenateStrings(String[] strings) { + StringBuilder result = new StringBuilder(); + + for (String string : strings) { + result.append(string); + } + return result.toString(); + }diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC76.html b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC76.html index 1f4bf7b3..6345694e 100644 --- a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC76.html +++ b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC76.html @@ -1,28 +1,28 @@- Avoid usage of static collections.
-
- If you want to use static collections make them final and create for example a singleton if needed containing the collections.
- The static fields are more complicated for the Garbage Collector to manage and can lead to memory leaks. + Avoid usage of static collections.
+ If you want to use static collections make them final and create for example a singleton if needed containing the collections.
+ The static fields are more complicated for the Garbage Collector to manage and can lead to memory leaks.Noncompliant Code Example
+Example of non-compliant code
- public class AvoidUsageOfStaticCollections { - public static final List<> LIST = new ArrayList<>(); // Noncompliant - public static final Set<> SET = new HashSet<>(); // Noncompliant - public static final Map<> MAP = new HashMap<>(); // Noncompliant - } +public class AvoidUsageOfStaticCollections { + public static final List<> LIST = new ArrayList<>(); // Noncompliant + public static final Set<> SET = new HashSet<>(); // Noncompliant + public static final Map<> MAP = new HashMap<>(); // Noncompliant +}Compliant Solution
- public class GoodUsageOfStaticCollections { - public static volatile GoodUsageOfStaticCollections INSTANCE = new GoodUsageOfStaticCollections(); +public class GoodUsageOfStaticCollections { + public static volatile GoodUsageOfStaticCollections INSTANCE = new GoodUsageOfStaticCollections(); - public final List<> LIST = new ArrayList<>(); - public final Set<> SET = new HashSet<>(); - public final Map<> MAP = new HashMap<>(); + public final List<> LIST = new ArrayList<>(); + public final Set<> SET = new HashSet<>(); + public final Map<> MAP = new HashMap<>(); - private GoodUsageOfStaticCollections() { - } - } + private GoodUsageOfStaticCollections() { + } +}diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC77.html b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC77.html index 8de04cef..5fecac08 100644 --- a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC77.html +++ b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC77.html @@ -1,67 +1,55 @@- Avoid using Pattern.compile() in a non-static context. - This operation requires a non negligible amount of computational power, Using a single match saves CPU cycles and RAM consumption. + Avoid using
-Pattern.compile()
in a non-static context. + This operation requires a non-negligible amount of computational power. + Using a single match saves CPU cycles and RAM consumption.Noncompliant Code Example
+Example of non-compliant code
+public class AvoidRegexPatternNotStatic { - public class AvoidRegexPatternNotStatic { - - public boolean foo() { - final Pattern pattern = Pattern.compile("foo"); // Noncompliant - return pattern.matcher("foo").find(); - } - - } - + public boolean foo() { + final Pattern pattern = Pattern.compile("foo"); // Noncompliant + return pattern.matcher("foo").find(); + } +}Compliant Solution N°1
+public class ValidRegexPattern { - public class ValidRegexPattern { - - private static final Pattern pattern = Pattern.compile("foo"); // Compliant - - public boolean foo() { - return pattern.matcher("foo").find(); - } - - } + private static final Pattern pattern = Pattern.compile("foo"); // Compliant + public boolean foo() { + return pattern.matcher("foo").find(); + } +}Compliant Solution N°2
+public class ValidRegexPattern2 { - public class ValidRegexPattern2 { - - private final Pattern pattern = Pattern.compile("foo"); // Compliant - - public boolean foo() { - return pattern.matcher("foo").find(); - } - - } + private final Pattern pattern = Pattern.compile("foo"); // Compliant + public boolean foo() { + return pattern.matcher("foo").find(); + } +}Compliant Solution N°3
+public class ValidRegexPattern3 { + private final Pattern pattern; - public class ValidRegexPattern3 { - - private final Pattern pattern; - - public ValidRegexPattern3() { - pattern = Pattern.compile("foo"); // Compliant - } - - public boolean foo() { - return pattern.matcher("foo").find(); - } - - } + public ValidRegexPattern3() { + pattern = Pattern.compile("foo"); // Compliant + } + public boolean foo() { + return pattern.matcher("foo").find(); + } +}diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC78.html b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC78.html index 873c74a0..3d861aad 100644 --- a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC78.html +++ b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC78.html @@ -1,5 +1,9 @@ -Don't set const parameter in batch update => Put its in query. Creating this parameter and destroying it consumes CPU cycles and RAM unnecessarily.
-Noncompliant Code Example
++ Don't set a constant parameter in a loop: put it in the query. + Creating this parameter and destroying it consumes CPU cycles and RAM unnecessarily. +
+ +Example of non-compliant code
public void foo() { // ... @@ -14,17 +18,18 @@+Noncompliant Code Example
// ... }Compliant Solution
- public void foo() { - ... - String query = "insert into mytable values(123,?,?)"; - ... - for(DummyClass o : list) { - stmt.setString(1, o.getName()); - stmt.setDouble(2, o.getPrice()); - stmt.addBatch(); - } - ... - } +public void foo() { + // ... + String query = "insert into mytable values(123,?,?)"; + // ... + for(DummyClass o : list) { + stmt.setString(1, o.getName()); + stmt.setDouble(2, o.getPrice()); + stmt.addBatch(); + } + // ... +}diff --git a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC79.html b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC79.html index f8801ef2..75b5d87e 100644 --- a/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC79.html +++ b/java-plugin/src/main/resources/fr/greencodeinitiative/l10n/java/rules/java/EC79.html @@ -1,24 +1,26 @@ -try-with-resources Statement needs to be implemented for any object that implements the AutoCloseable interface, it save computer resources.
-Noncompliant Code Example
+try-with-resources statement needs to be implemented for any object that implements the
+ +AutoCloseable
interface, it save computer resources.Example of non-compliant code
- private static void printFileJava7() throws IOException { - FileInputStream input = new FileInputStream("file.txt"); - int data = input.read(); - while(data != -1){ - System.out.print((char) data); - data = input.read(); - } - } +private static void printFileJava7() throws IOException { + FileInputStream input = new FileInputStream("file.txt"); + int data = input.read(); + while(data != -1){ + System.out.print((char) data); + data = input.read(); + } +}+Compliant Solution
- private static void printFileJava7() throws IOException { - try(FileInputStream input = new FileInputStream("file.txt")) { - int data = input.read(); - while(data != -1){ - System.out.print((char) data); - data = input.read(); - } - } - } +private static void printFileJava7() throws IOException { + try(FileInputStream input = new FileInputStream("file.txt")) { + int data = input.read(); + while(data != -1) { + System.out.print((char) data); + data = input.read(); + } + } +}diff --git a/php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/EC22.html b/php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/EC22.html index 85bc76eb..7d0dd7b8 100644 --- a/php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/EC22.html +++ b/php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/EC22.html @@ -1,9 +1,10 @@Use of methods for basic operations
-Noncompliant Code Example
+ +Example of non-compliant code
- $min = min($a, $b); // Noncompliant + $min = min($a, $b); // NoncompliantCompliant Solution
- $min = ($a < $b) ? $a : $b; + $min = ($a < $b) ? $a : $b;diff --git a/php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/EC34.html b/php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/EC34.html index 6a6b0661..999eee44 100644 --- a/php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/EC34.html +++ b/php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/EC34.html @@ -1,112 +1,75 @@Inside complex code parts (for exemple multiple loops, complex data constructions...), avoid using try...catch...finally.
When an exception is thrown, a variable (the exception itself) is created in a catch block and it's destruction consumes unnecessary CPU cycles and RAM. Prefer using logical tests in this cases.
-Noncompliant Code Example
+ +Example of non-compliant code
-try -{ - $picture = PDF_open_image_file($PDF, "jpeg", $imgFile, "", 0); // This is the original statement, this works on PHP4 +try { + $picture = PDF_open_image_file($PDF, "jpeg", $imgFile, "", 0); // This is the original statement, this works on PHP4 } -catch(Exception $ex) -{ - $msg = "Error opening $imgFile for Product $row['Identifier']"; - throw new Exception($msg); +catch(Exception $ex) { + $msg = "Error opening $imgFile for Product $row['Identifier']"; + throw new Exception($msg); }+Compliant Solution
//try if (file_exists($imgFile)) { - $picture = PDF_open_image_file($PDF, "jpeg", $imgFile, "", 0); + $picture = PDF_open_image_file($PDF, "jpeg", $imgFile, "", 0); } //catch if (!$picture) { - $msg = "Error opening $imgFile for Product $row['Identifier']"; - print $msg; + $msg = "Error opening $imgFile for Product $row['Identifier']"; + print $msg; }-The three sources of impacts of a code identified are:
-- Energy (measured in joules) -
- Transfer (measured in Bytes) -
- Storage (measured in Bytes)
-
The control of these 3 impacts allows to lengthen the life of the terminals as well as reduce their energy consumption. -
The ETSdiff tool allows measuring a differential on these three values and in a given context (database and fixed measurement environment). -
The results generated by ETSdiff must help define the interest of the rule reported by Sonarqube in the context of the code analyzed. -
-Case for a 1GB database:
+ +Measurement protocol
+The three sources of impacts of a code identified are:
+
The control of these 3 impacts allows to lengthen the life of the terminals as well as reduce their energy consumption.
+The ETSdiff tool allows measuring a differential on these three values and in a given context (database and fixed measurement environment).
+The results generated by ETSdiff must help define the interest of the rule reported by Sonarqube in the context of the code analyzed.
+ +- |
-
-
- Compliant
- |
-
-
-
- Non-compliant
- |
-
-
-
- Energy
- |
-
-
-
- 515.855638
- |
-
-
-
- 516.9188409999999
- |
-
-
-
- Transfer
- |
-
-
-
- 1579453
- |
-
-
-
- 1579457
- |
-
-
-
- Storage
- |
-
-
-
- 637549804
- |
-
-
-
- 637549804
- |
-
+ | Compliant | +Non-compliant | +
---|---|---|
Energy | +515.855638 | +516.9188409999999 | +
Transfer | +1579453 | +1579457 | +
Storage | +637549804 | +637549804 | +
- Prefer local variables as parameters -
-A l'appel d'une variable globale, le moteur d'interprétation doit vérifier son existence dans tous les scopes, qu'elle dispose d'une valeur, etc. Passer les variables globales en argument de routines les rend locales dans la fonction, permettant ainsi d'économiser du temps de calcul (cycles CPU). -
-exemple:
- var aGlobal = new String('Hello');
- function globalLength(){
- length = aGlobal.length;
- console.log(length);
- }
- globalLength();
-
- var aGlobal = new String('Hello');
- function someVarLength(str){
- length = str.length;
- console.log(length);
- }
- somVarLength(aGlobal);
Prefer local variables as parameters
+ +When calling a global variable, the interpretation engine must check that it exists in all scopes, that it has a value, etc. Passing global variables as arguments gives them the status of local variables inside the function, which saves computation time (CPU cycles).
+ +Example:
+ ++var aGlobal = new String('Hello'); +function globalLength() { + length = aGlobal.length; + console.log(length); +} +globalLength(); + +var aGlobal = new String('Hello'); +function someVarLength(str) { + length = str.length; + console.log(length); +} +somVarLength(aGlobal); +diff --git a/php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/EC66.html b/php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/EC66.html index 1cf595d5..8bb6ffdd 100644 --- a/php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/EC66.html +++ b/php-plugin/src/main/resources/fr/greencodeinitiative/l10n/php/rules/custom/EC66.html @@ -1,97 +1,67 @@ -
PHP allows declaring a string with simple or double quotes. Using double quotes allows developers to insert variables which will be substituted during execution. When the string has no variables, using simple quotes avoid PHP to search inexisting variables. It will save CPU cycles consumption and RAM usage.
-+ PHP allows you to declare a string with single or double quotes. + Using double quotes allows developers to insert variables that will be substituted at runtime. + When the string has no variables, using single quotes saves PHP from searching for variables that don't exist. + This will save CPU cycle consumption and RAM usage. +
+ +myFunction("name", "age", "IsStudent"); - $lastName = "Hugo"; - $concatenatedString = "$lastName is a student"; +$lastName = "Hugo"; +$concatenatedString = "$lastName is a student";+
myFunction('name', 'age', 'IsStudent'); - $lastName = 'Hugo'; - $concatenatedString = $lastName . 'is a student'; +$lastName = 'Hugo'; +$concatenatedString = $lastName . 'is a student';-
The three sources of impacts of a code identified are:
+The control of these 3 impacts allows to lengthen the life of the terminals as well as reduce their energy consumption.
+The ETSdiff tool allows measuring a differential on these three values and in a given context (database and fixed measurement environment).
+The results generated by ETSdiff must help define the interest of the rule reported by Sonarqube in the context of the code analyzed.
+ +- |
-
-
- Compliant
- |
-
-
-
- Non-compliant
- |
-
-
-
- Energy
- |
-
-
-
- 3.041966
- |
-
-
-
- 1.2651545000000002
- |
-
-
-
- Transfer
- |
-
-
-
- 68520884
- |
-
-
-
- 68588123
- |
-
-
-
- Storage
- |
-
-
-
- 637548795
- |
-
-
-
- 637548795
- |
-
+ | Compliant | +Non-compliant | +
---|---|---|
Energy | +3.041966 | +1.2651545000000002 | +
Transfer | +68520884 | +68588123 | +
Storage | +637548795 | +637548795 | +
The form $i++ creates a temporary variable whereas ++$i does not. It save CPU cycles.
-
+ The form $i++
creates a temporary variable whereas ++$i
does not.
+ It save CPU cycles.
+
$i++-
++$i-
+++$i ++ +
The three sources of impacts of a code identified are:
+The control of these 3 impacts allows to lengthen the life of the terminals as well as reduce their energy consumption.
+The ETSdiff tool allows measuring a differential on these three values and in a given context (database and fixed measurement environment).
+The results generated by ETSdiff must help define the interest of the rule reported by Sonarqube in the context of the code analyzed.
+ +- |
-
-
- Compliant
- |
-
-
-
- Non-compliant
- |
-
-
-
- Energy
- |
-
-
-
- 1.8163645000000002
- |
-
-
-
- 0.2613885000000001
- |
-
-
-
- Transfer
- |
-
-
-
- 11265758
- |
-
-
-
- 11290494
- |
-
-
-
- Storage
- |
-
-
-
- 637548673
- |
-
-
-
- 637548673
- |
-
+ | Compliant | +Non-compliant | +
---|---|---|
Energy | +1.8163645000000002 | +0.2613885000000001 | +
Transfer | +11265758 | +11290494 | +
Storage | +637548673 | +637548673 | +
Do not call a function when declaring a for-type loop in order to avoid function calls each iterations. It saves CPU cycles.
-+ Do not call a function when declaring a for-type loop in order to avoid function calls each iteration. + It saves CPU cycles. +
+ +-for ($i = 0; $i <= foo(); $i++) { // NOK - ...... +for ($i = 0; $i <= foo(); $i++) { // Noncompliant + // ...... }+
$maxI = foo(); for ($i = 0; $i <= $maxI; $i++) { - ..... + // ..... } - OR +// OR for ($i = 0, $maxI = foo(); $i <= $maxI; $i++) { - ..... + // ..... } -}+ -
The three sources of impacts of a code identified are:
+The control of these 3 impacts allows to lengthen the life of the terminals as well as reduce their energy consumption.
+The ETSdiff tool allows measuring a differential on these three values and in a given context (database and fixed measurement environment).
+The results generated by ETSdiff must help define the interest of the rule reported by Sonarqube in the context of the code analyzed.
-- |
-
-
- Compliant
- |
-
-
-
- Non-compliant
- |
-
-
-
- Energy
- |
-
-
-
- 144.635057
- |
-
-
-
- 144.58341249999998
- |
-
-
-
- Transfer
- |
-
-
-
- 50000
- |
-
-
-
- 50004
- |
-
-
-
- Storage
- |
-
-
-
- 637549590
- |
-
-
-
- 637549590
- |
-
+ | Compliant | +Non-compliant | +
---|---|---|
Energy | +144.635057 | +144.58341249999998 | +
Transfer | +50000 | +50004 | +
Storage | +637549590 | +637549590 | +
Executing SQL queries in loop induced unnecessary network transfert, calculation by the CPU and RAM usage.
-- public function foo() { - ... - $baseQuery = "SELECT name FROM users where id = "; - for ($i = 0; $i < 20; ++$i) { +Example of non-compliant code
++public function foo() { + // ... + $baseQuery = "SELECT name FROM users where id = "; - $query = $baseQuery . $i; - $connection = mysql_connect($dbhost, $dbuser, $dbpass) or die("Unable to Connect to '$dbhost'"); - mysql_select_db($dbname) or die("Could not open the db '$dbname'"); - $result = mysql_query($this->Query);// Noncompliant + for ($i = 0; $i < 20; ++$i) { - // iterate through the result - ... - mysql_close($connection); - } - ... - } + $query = $baseQuery . $i; + $connection = mysql_connect($dbhost, $dbuser, $dbpass) or die("Unable to Connect to '$dbhost'"); + mysql_select_db($dbname) or die("Could not open the db '$dbname'"); + $result = mysql_query($this->Query);// Noncompliant + // iterate through the result + // ... + mysql_close($connection); + } + // ... +}+Compliant Solution
+public function foo() { + // ... + $query = "SELECT name FROM users where id in ("; - public function foo() { - ... - $query = "SELECT name FROM users where id in ("; - - for ($i = 0; $i < 20; ++$i) { - $query .= ',' . $i; - } - $query .= ')'; - - $connection = mysql_connect($dbhost, $dbuser, $dbpass) or die("Unable to Connect to '$dbhost'"); - mysql_select_db($dbname) or die("Could not open the db '$dbname'"); - $result = mysql_query($this->Query); // compliant + for ($i = 0; $i < 20; ++$i) { + $query .= ',' . $i; + } + $query .= ')'; - // iterate through the result - ... - mysql_close($connection); - } + $connection = mysql_connect($dbhost, $dbuser, $dbpass) or die("Unable to Connect to '$dbhost'"); + mysql_select_db($dbname) or die("Could not open the db '$dbname'"); + $result = mysql_query($this->Query); // compliant + // iterate through the result + // ... + mysql_close($connection); +}-The three sources of impacts of a code identified are:
- - Energy (measured in joules) -
- Transfer (measured in Bytes) -
- Storage (measured in Bytes)
-
The control of these 3 impacts allows to lengthen the life of the terminals as well as reduce their energy consumption. -
The ETSdiff tool allows measuring a differential on these three values and in a given context (database and fixed measurement environment). -
The results generated by ETSdiff must help define the interest of the rule reported by Sonarqube in the context of the code analyzed. -
+Measurement protocol
+The three sources of impacts of a code identified are:
+
The control of these 3 impacts allows to lengthen the life of the terminals as well as reduce their energy consumption.
+The ETSdiff tool allows measuring a differential on these three values and in a given context (database and fixed measurement environment).
+The results generated by ETSdiff must help define the interest of the rule reported by Sonarqube in the context of the code analyzed.
-- |
-
-
- Compliant
- |
-
-
-
- Non-compliant
- |
-
-
-
- Energy
- |
-
-
-
- 73.907586
- |
-
-
-
- 82.15627099999998
- |
-
-
-
- Transfer
- |
-
-
-
- 49526
- |
-
-
-
- 221836
- |
-
-
-
- Storage
- |
-
-
-
- 637549572
- |
-
-
-
- 637549572
- |
-
+ | Compliant | +Non-compliant | +
---|---|---|
Energy | +73.907586 | +82.15627099999998 | +
Transfer | +49526 | +221836 | +
Storage | +637549572 | +637549572 | +
- |
-
-
- Compliant
- |
-
-
-
- Non-compliant
- |
-
-
-
- Energy
- |
-
-
-
- 159.4871645
- |
-
-
-
- 169.746055
- |
-
-
-
- Transfer
- |
-
-
-
- 50385
- |
-
-
-
- 228225
- |
-
-
-
- Storage
- |
-
-
-
- 1178614788
- |
-
-
-
- 1178614788
- |
-
+ | Compliant | +Non-compliant | +
---|---|---|
Energy | +159.4871645 | +169.746055 | +
Transfer | +50385 | +228225 | +
Storage | +50385 | +228225 | +
- |
-
-
- Compliant
- |
-
-
-
- Non-compliant
- |
-
-
-
- Energy
- |
-
-
-
- 395.7629349999999
- |
-
-
-
- 404.37447649999996
- |
-
-
-
- Transfer
- |
-
-
-
- 51597
- |
-
-
-
- 238884
- |
-
-
-
- Storage
- |
-
-
-
- 2357214212
- |
-
-
-
- 2357214212
- |
-
+ | Compliant | +Non-compliant | +
---|---|---|
Energy | +395.7629349999999 | +404.37447649999996 | +
Transfer | +51597 | +238884 | +
Storage | +2357214212 | +2357214212 | +
- |
-
-
- Compliant
- |
-
-
-
- Non-compliant
- |
-
-
-
- Energy
- |
-
-
-
- 992.128585
- |
-
-
-
- 1005.4625534999999
- |
-
-
-
- Transfer
- |
-
-
-
- 52189
- |
-
-
-
- 249499
- |
-
-
-
- Storage
- |
-
-
-
- 4685052932
- |
-
-
-
- 4685052932
- |
-
+ | Compliant | +Non-compliant | +
---|---|---|
Energy | +992.128585 | +1005.4625534999999 | +
Transfer | +52189 | +249499 | +
Storage | +4685052932 | +4685052932 | +
Databases servers have to solves fileds regarding to schema. Knowing and using the schema save CPU cycles and network transfer.
-- public function foo() { - ... - $baseQuery = "SELECT * FROM users"; // Noncompliant +Databases servers have to solve fields regarding the schema. Knowing and using the schema save CPU cycles and network transfer.
- ... - } +Example of non-compliant code
++public function foo() { + // ... + $baseQuery = "SELECT * FROM users"; // Noncompliant + // ... +}+Compliant Solution
- public function foo() { - ... - $baseQuery = "SELECT id,name, adress FROM users "; - ... - } +public function foo() { + // ... + $baseQuery = "SELECT id,name, address FROM users "; + // ... +}-The three sources of impacts of a code identified are:
-- Energy (measured in joules) -
- Transfer (measured in Bytes) -
- Storage (measured in Bytes)
-
The control of these 3 impacts allows to lengthen the life of the terminals as well as reduce their energy consumption. -
The ETSdiff tool allows measuring a differential on these three values and in a given context (database and fixed measurement environment). -
The results generated by ETSdiff must help define the interest of the rule reported by Sonarqube in the context of the code analyzed. -
-Case for a 1GB database:
+Measurement protocol
+The three sources of impacts of a code identified are:
+
The control of these 3 impacts allows to lengthen the life of the terminals as well as reduce their energy consumption.
+The ETSdiff tool allows measuring a differential on these three values and in a given context (database and fixed measurement environment).
+The results generated by ETSdiff must help define the interest of the rule reported by Sonarqube in the context of the code analyzed.
+ +- |
-
-
- Compliant
- |
-
-
-
- Non-compliant
- |
-
-
-
- Energy
- |
-
-
-
- 0.040610499999999994
- |
-
-
-
- 0.065223
- |
-
-
-
- Transfer
- |
-
-
-
- 779232
- |
-
-
-
- 2697937
- |
-
-
-
- Storage
- |
-
-
-
- 637548827
- |
-
-
-
- 637548827
- |
-
+ | Compliant | +Non-compliant | +
---|---|---|
Energy | +0.040610499999999994 | +0.065223 | +
Transfer | +779232 | +2697937 | +
Storage | +637548827 | +637548827 | +
Inside complex code parts (for exemple multiple loops, complex data constructions...), avoid using try...catch...finally. -
When an exception is thrown, a variable (the exception itself) is created in a catch block and it's destruction consumes unnecessary CPU cycles and RAM. Prefer using logical tests in this cases.
+Inside complex code parts (for exemple multiple loops, complex data constructions...), avoid using try...catch...finally.
+When an exception is thrown, a variable (the exception itself) is created in a catch block, and it's destruction consumes unnecessary CPU cycles and RAM. Prefer using logical tests in this cases.
-try: - f = open(path) - print(fh.read()) + f = open(path) + print(fh.read()) except: - print('No such file '+path + print('No such file '+path finally: - f.close() + f.close()+
if os.path.isfile(path): - fh = open(path, 'r') - print(fh.read()) - fh.close + fh = open(path, 'r') + print(fh.read()) + fh.closediff --git a/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/ECPY4.html b/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/ECPY4.html index c92e7e5b..0f1f2ced 100644 --- a/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/ECPY4.html +++ b/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/ECPY4.html @@ -1,20 +1,22 @@
When function calls global variables, a lot a CPU cycles is consumed.
-global_var = 'foo' def print_global_var_details(): - print(len(global_var)) # Noncompliant - print('Global var : ', global_var) # Noncompliant - print('Global var : ' + global_var) # Noncompliant + print(len(global_var)) # Noncompliant + print('Global var : ', global_var) # Noncompliant + print('Global var : ' + global_var) # Noncompliant print_global_var_details()+
global_var = 'foo'; def print_var_details(local_var) { - print(len(local_var)); - print('Var : ', local_var) - print('Var : ' + local_var) + print(len(local_var)); + print('Var : ', local_var) + print('Var : ' + local_var) } print_length(global_var);diff --git a/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/ECPY69.html b/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/ECPY69.html index 4b90e50d..fd1314b9 100644 --- a/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/ECPY69.html +++ b/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/ECPY69.html @@ -1,13 +1,14 @@ -
Do not call a function when declaring a for-type loop in order to avoid function calls each iterations. It saves CPU cycles.
--for i in my_function(): - ...... +Do not call a function when declaring a for-type loop in order to avoid function calls each iteration. It saves CPU cycles.
+Example of non-compliant code
++for i in my_function(): # Noncompliant + # ......+Compliant Solution
limit = my_function() for i in limit: - ...... + # ......diff --git a/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/ECPY7.html b/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/ECPY7.html index 181906af..c947d74b 100644 --- a/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/ECPY7.html +++ b/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/ECPY7.html @@ -1,36 +1,37 @@ -Avoid Getters and Setters in class, It increase unless RAM memory usage.
-Noncompliant Code Example
+Avoid Getters and Setters in class. It increases unless RAM memory usage.
+ +Example of non-compliant code
class Client(): - def __init__(self, age): - self.age = age + def __init__(self, age): + self.age = age - def get_age(self): - return self.age + def get_age(self): + return self.age - def set_age(self, age): - self.age = age + def set_age(self, age): + self.age = age client = Client(25) -client.get_age() # Getter inutile -client.set_age(25) # Setter inutile - +client.get_age() # Noncompliant: Useless getter +client.set_age(25) # Noncompliant: Useless setter+Compliant Solution
class Client(): - def __init__(self, age): - self.age = age + def __init__(self, age): + self.age = age - def get_age(self): - return self.age + def get_age(self): + return self.age - def set_age(self, age): - self.age = age + def set_age(self, age): + self.age = age client = Client(25) -client.age # Récupérer l'attribut age -client.age = 26 # Modifier l'attribut age +client.age # Get age value +client.age = 26 # Set age valuediff --git a/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/ECPY72.html b/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/ECPY72.html index 88b6109c..14090c15 100644 --- a/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/ECPY72.html +++ b/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/ECPY72.html @@ -1,21 +1,21 @@Executing SQL queries in loop induced unnecessary calculation by the CPU, RAM usage and network transfert.
-Noncompliant Code Example
+ +Example of non-compliant code
- def foo(): - ... - results = [] - for id in range(20): - results.append(cursor.execute("SELECT name FROM users where id = ?", (id)).fetchone()) # Noncompliant {{Avoid performing SQL queries within a loop}} - ... +def foo(): + # ... + results = [] + for id in range(20): + results.append(cursor.execute("SELECT name FROM users where id = ?", (id)).fetchone()) # Noncompliant {{Avoid performing SQL queries within a loop}} + # ...+Compliant Solution
- - def foo(): - ... - ids = range(20) - results = cursor.execute("SELECT name FROM users where id IN ({0})".format(', '.join("?" * len(ids))), ids).fetchmany() # Compliant - ... - } - +def foo(): + # ... + ids = range(20) + results = cursor.execute("SELECT name FROM users where id IN ({0})".format(', '.join("?" * len(ids))), ids).fetchmany() # Compliant + # ... +}diff --git a/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/ECPY74.html b/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/ECPY74.html index 8d977de2..efbb7909 100644 --- a/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/ECPY74.html +++ b/python-plugin/src/main/resources/fr/greencodeinitiative/l10n/python/rules/python/ECPY74.html @@ -1,21 +1,19 @@ -Databases servers have to solves fileds regarding to schema. Knowing and using the schema save CPU cycles and network transfer.
-Noncompliant Code Example
-- public void foo() { - ... - String baseQuery = "SELECT * FROM users"; // Noncompliant - - ... - } +Databases servers have to solve fields regarding the schema. Knowing and using the schema save CPU cycles and network transfer.
+Example of non-compliant code
++public void foo() { + ... + String baseQuery = "SELECT * FROM users"; // Noncompliant + ... +}+Compliant Solution
- - public void foo() { - ... - String query = "SELECT id,name, adress FROM users "; - ... - } - +public void foo() { + ... + String query = "SELECT id,name,address FROM users "; + ... +}