-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: convert rule description from HTML to ASCIIDOC
- Loading branch information
Showing
48 changed files
with
1,075 additions
and
1,557 deletions.
There are no files selected for viewing
39 changes: 21 additions & 18 deletions
39
ecocode-rules-specifications/src/main/rules/EC1/java/EC1.asciidoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<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<>(); | ||
|
||
for (Integer id: ids) { | ||
Optional<Employee> employee = employeeRepository.findById(id); // Noncompliant | ||
if (employee.isPresent()) { | ||
employees.add(employee.get()); | ||
} | ||
} | ||
## Noncompliant Code Example | ||
|
||
</pre> | ||
<h2>Compliant Solution</h2> | ||
<pre> | ||
private final List<Integer> ids = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); | ||
List<Employee> 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); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 27 additions & 26 deletions
53
ecocode-rules-specifications/src/main/rules/EC2/java/EC2.asciidoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
``` |
151 changes: 78 additions & 73 deletions
151
ecocode-rules-specifications/src/main/rules/EC203/python/EC203.asciidoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,78 @@ | ||
<p>If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.</p> | ||
<p>Because SVGs are generally smaller than other image format, they’re less taxing on your server despite needing to render on load.</p> | ||
<p>When to use SVG : | ||
<ul> | ||
<li>Your image is used for decorative website graphics, logos, icons, graphs and diagrams, and other simple images.</li> | ||
<li>You image require animation.</li> | ||
<li>You image need to be responsive and scale without lack of quality.</li> | ||
</ul> | ||
</p> | ||
<p>Some advantages of using SVG: | ||
<ul> | ||
<li>SVGs are scalable and will render pixel-perfect at any resolution whereas JPEGs, PNGs and GIFs will not.</li> | ||
<li>SVGs are vector images and therefore are usually much smaller in file-size than bitmap-based images.</li> | ||
<li>SVGs can be embedded into the HTML which means they can be cached, edited directly using CSS and indexed for greater accessibility.</li> | ||
<li>SVGs can be animated directly or by using CSS or JavaScript making it easy for web designers to add interactivity to a site.</li> | ||
</ul> | ||
</p> | ||
|
||
|
||
<h2>Noncompliant Code Example</h2> | ||
<pre> | ||
... | ||
img_jpg = "image.jpg" | ||
... | ||
</pre> | ||
<h2>Compliant Solution</h2> | ||
<pre> | ||
... | ||
img_svg = "image.svg" | ||
... | ||
</pre> | ||
|
||
<h2>Noncompliant Code Example</h2> | ||
<pre> | ||
public void foo() { | ||
... | ||
image_format = testImage("image.jpg") | ||
... | ||
} | ||
</pre> | ||
<h2>Compliant Solution</h2> | ||
<pre> | ||
public void foo() { | ||
... | ||
image_format = testImage("image.svg") | ||
... | ||
} | ||
</pre> | ||
|
||
<h2>Noncompliant Code Example</h2> | ||
<pre> | ||
public void foo() { | ||
... | ||
return '<html><img src="xx/xx/image.bmp"></html>' | ||
... | ||
} | ||
</pre> | ||
<h2>Compliant Solution</h2> | ||
<pre> | ||
public void foo() { | ||
... | ||
return '<html><img src="xx/xx/image.svg"></html>' | ||
... | ||
} | ||
|
||
public void foo2() { | ||
... | ||
return ('<html><svg width="100" height="100">' + | ||
'<circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />' + | ||
'</svg></html>') | ||
... | ||
} | ||
</pre> | ||
If possible, the utilisation of svg image format (or `<svg/>` html tag) is recommended over other image format. | ||
|
||
Because SVGs are generally smaller than other image format, they’re less taxing on your server despite needing to render on load. | ||
|
||
When to use SVG : | ||
|
||
- Your image is used for decorative website graphics, logos, icons, graphs and diagrams, and other simple images. | ||
- You image require animation. | ||
- You image need to be responsive and scale without lack of quality. | ||
Some advantages of using SVG: | ||
|
||
- SVGs are scalable and will render pixel-perfect at any resolution whereas JPEGs, PNGs and GIFs will not. | ||
- SVGs are vector images and therefore are usually much smaller in file-size than bitmap-based images. | ||
- SVGs can be embedded into the HTML which means they can be cached, edited directly using CSS and indexed for greater accessibility. | ||
- SVGs can be animated directly or by using CSS or JavaScript making it easy for web designers to add interactivity to a site. | ||
## Noncompliant Code Example | ||
|
||
``` | ||
img_jpg = "image.jpg" | ||
``` | ||
|
||
## Compliant Solution | ||
|
||
``` | ||
img_svg = "image.svg" | ||
``` | ||
|
||
## Noncompliant Code Example | ||
|
||
``` | ||
public void foo() { | ||
// ... | ||
image_format = testImage("image.jpg") | ||
// ... | ||
} | ||
``` | ||
|
||
## Compliant Solution | ||
|
||
``` | ||
public void foo() { | ||
// ... | ||
image_format = testImage("image.svg") | ||
// ... | ||
} | ||
``` | ||
|
||
## Noncompliant Code Example | ||
|
||
``` | ||
public void foo() { | ||
// ... | ||
return '<html><img src="xx/xx/image.bmp"></html>' | ||
// ... | ||
} | ||
``` | ||
|
||
## Compliant Solution | ||
|
||
``` | ||
public void foo() { | ||
// ... | ||
return '<html><img src="xx/xx/image.svg"></html>' | ||
// ... | ||
} | ||
``` | ||
|
||
Or | ||
|
||
``` | ||
public void foo() { | ||
// ... | ||
return ('<html><svg width="100" height="100"><circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow"/></svg></html>') | ||
// ... | ||
} | ||
``` |
22 changes: 13 additions & 9 deletions
22
ecocode-rules-specifications/src/main/rules/EC22/php/EC22.asciidoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
<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
52
ecocode-rules-specifications/src/main/rules/EC27/java/EC27.asciidoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
``` |
Oops, something went wrong.