Skip to content

Commit

Permalink
refactor: convert rule description from HTML to ASCIIDOC
Browse files Browse the repository at this point in the history
  • Loading branch information
jycr committed Apr 12, 2023
1 parent e99a5d4 commit f013f06
Show file tree
Hide file tree
Showing 43 changed files with 832 additions and 1,324 deletions.
39 changes: 21 additions & 18 deletions ecocode-rule-specification/src/main/rules/EC1/java/EC1.asciidoc
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
<p>The use of Spring repository in a loop induces unnecessary calculations by the CPU and therefore superfluous energy consumption.</p>
<h2>Noncompliant Code Example</h2>
<pre>
private final List&lt;Integer&gt; 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&lt;Employee&gt; employees = new ArrayList&lt;&gt;();

for (Integer id: ids) {
Optional&lt;Employee&gt; employee = employeeRepository.findById(id); // Noncompliant
if (employee.isPresent()) {
employees.add(employee.get());
}
}
## Noncompliant Code Example

</pre>
<h2>Compliant Solution</h2>
<pre>
private final List&lt;Integer&gt; ids = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
List&lt;Employee&gt; employees = employeeRepository.findAllById(ids);
</pre>
```java
private final List<Integer> ids = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

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

```java
private final List<Integer> ids = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
List<Employee> employees = employeeRepository.findAllById(ids);
```
53 changes: 27 additions & 26 deletions ecocode-rule-specification/src/main/rules/EC2/java/EC2.asciidoc
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
<p>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.</p>
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`.

<h2>Non-compliant Code Example</h2>
<pre>
int index = 1;
int nb = 2;
## Non-compliant Code Example

if (nb > index) {
nb = nb + index;
} else {
nb = nb - 1;
}
if (nb != index + 1) {
nb = nb + index;
} else {
nb = nb - 1;
}
```java
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;
}
```

</pre>
<h2>Compliant Code Example</h2>
<pre>
int index = 1;
int nb = 2;
## Compliant Code Example

if (nb > index) {
nb = nb + index;
} else {
nb = nb - 1;
}
</pre>
```java
int index = 1;
int nb = 2;

if (nb > index) {
nb = nb + index;
} else {
nb = nb - 1;
}
```
23 changes: 14 additions & 9 deletions ecocode-rule-specification/src/main/rules/EC22/php/EC22.asciidoc
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
<p>Use of methods for basic operations</p>
<h2>Noncompliant Code Example</h2>
<pre>
$min = min($a, $b); // Noncompliant
</pre>
<h2>Compliant Solution</h2>
<pre>
$min = ($a < $b) ? $a : $b;
</pre>
Use of methods for basic operations

## Noncompliant Code Example

```php
$min = min($a, $b); // Noncompliant
```

## Compliant Solution

```php
$min = ($a < $b) ? $a : $b;
```

52 changes: 28 additions & 24 deletions ecocode-rule-specification/src/main/rules/EC27/java/EC27.asciidoc
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
<p>Using System.arraycopy to copy arrays</p>
<p>
Programs 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.<br />
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.<br />
Loops must be optimized to reduce processing time and make full use of hardware and processor mechanisms and optimizations.<br />
In the case of table copying (table), the native System.arraycopy.<br />
We can also use copyOf or clone that are slightly less efficient.<br />
The looping method will be outlawed.
</p>
<h2>Noncompliant Code Example</h2>
<pre>
int len = array.length;
boolean[] copy = new boolean[array.length];
for (int i = 0; i < len; i++) {
copy[i] = array[i]; // Noncompliant
}
return copy;
</pre>
<h2>Compliant Solution</h2>
<pre>
int[] copy = new int[array.length];
System.arraycopy(array, 0, copy, 0, array.length);
return copy;
</pre>
Using `System.arraycopy` to copy arrays

Programs 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.
Loops must be optimized to reduce processing time and make full use of hardware and processor mechanisms and optimizations.
In the case of table copying (table), use the native `System.arraycopy`.
We can also use `copyOf` or `clone` that are slightly less efficient.
The looping method will be outlawed.

## Noncompliant Code Example

```java
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

```java
int[] copy = new int[array.length];
System.arraycopy(array, 0, copy, 0, array.length);
return copy;
```
57 changes: 29 additions & 28 deletions ecocode-rule-specification/src/main/rules/EC28/java/EC28.asciidoc
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
<p>Optimize read file exception</p>
<h2>Noncompliant Code Example</h2>
<pre>
public void readPreferences(String filename) {
//...
InputStream in = null;
try {
in = new FileInputStream(filename);
} catch (FileNotFoundException e) {
logger.log(e);
}
in.read(...);
//...
}
Optimize read file exception

</pre>
<h2>Compliant Solution</h2>
<pre>
public void readPreferences(String filename)
throws IllegalArgumentException,
FileNotFoundException, IOException {
if (filename == null) {
throw new IllegalArgumentException ("filename is null");
} //if
//...
InputStream in = new FileInputStream(filename);
//...
}
</pre>
## Noncompliant Code Example

```java
public void readPreferences(String filename) {
//...
InputStream in = null;
try {
in = new FileInputStream(filename);
} catch (FileNotFoundException e) {
logger.log(e);
}
in.read(...);
//...
}
```

## Compliant Solution

```java
public void readPreferences(String filename) throws IllegalArgumentException, FileNotFoundException, IOException {
if (filename == null) {
throw new IllegalArgumentException ("filename is null");
}
//...
InputStream in = new FileInputStream(filename);
//...
}
```
35 changes: 19 additions & 16 deletions ecocode-rule-specification/src/main/rules/EC3/java/EC3.asciidoc
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
<p>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.</p>
<h2>Noncompliant Code Example</h2>
<pre>
List&lt;String&gt; objList = getData();
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.

for (int i = 0; i < objList.size(); i++) { // Noncompliant
// execute code
}
## Noncompliant Code Example

</pre>
<h2>Compliant Solution</h2>
<pre>
List&lt;String&gt; objList = getData();
```java
List<String> objList = getData();

int size = objList.size();
for (int i = 0; i < size; i++) {
// execute code
}
</pre>
for (int i = 0; i < objList.size(); i++) { // Noncompliant
// execute code
}
```

## Compliant Solution

```java
List<String> objList = getData();

int size = objList.size();
for (int i = 0; i < size; i++) {
// execute code
}
```
40 changes: 21 additions & 19 deletions ecocode-rule-specification/src/main/rules/EC32/java/EC32.asciidoc
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
<p>
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.
</p>
<h2>Noncompliant Code Example</h2>
<pre>
StringBuilder sb = new StringBuilder(); // Noncompliant
for (int i = 0; i < 100; i++) {
sb.append(...);
}
</pre>
<h2>Compliant Solution</h2>
<pre>
StringBuilder sb = new StringBuilder(100);
for (int i = 0; i < 100; i++) {
sb.append(...);
}
</pre>
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

```java
StringBuilder sb = new StringBuilder(); // Noncompliant
for (int i = 0; i < 100; i++) {
sb.append(...);
}
```

## Compliant Solution

```java
StringBuilder sb = new StringBuilder(100);
for (int i = 0; i < 100; i++) {
sb.append(...);
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Energy (J),515.855638,516.9188409999999
Transfer (B),1579453,1579457
Storage (B),637549804,637549804
Loading

0 comments on commit f013f06

Please sign in to comment.