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 Jun 29, 2023
1 parent 5c0d987 commit e988c1a
Show file tree
Hide file tree
Showing 48 changed files with 1,075 additions and 1,557 deletions.
39 changes: 21 additions & 18 deletions ecocode-rules-specifications/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);
```
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
<p>SVG images generated by common drawing softwares contains unnecessary data: calc layer, metadata, namespaces and comments.</p>
<h2>Noncompliant Code Example</h2>
<pre>
&lt;!-- Created with Inkscape (http://www.inkscape.org/) --&gt;
&lt;svg
SVG images generated by common drawing softwares contains unnecessary data: calc layer, metadata, namespaces and comments.

## Noncompliant Code Example

```xml
<!-- Created with Inkscape (http://www.inkscape.org/) -->
<svg
width="210mm"
height="297mm"
viewBox="0 0 210 297"
Expand All @@ -13,35 +15,25 @@
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns="http://www.w3.org/2000/svg"
xmlns:svg="http://www.w3.org/2000/svg"&gt;
&lt;g
xmlns:svg="http://www.w3.org/2000/svg">
<g
inkscape:label="Calque 1"
inkscape:groupmode="layer"
id="layer1"&gt;
&lt;circle
id="layer1">
<circle
style="fill:#ff00ff;stroke-width:0.264583"
id="path111"
cx="104.02724"
cy="152.19028"
r="73.177132" /&gt;
&lt;/g&gt;
&lt;/svg&gt;
</pre>
<h2>Compliant Solution</h2>
<pre>
&lt;svg
width="210mm"
height="297mm"
viewBox="0 0 210 297"
xmlns="http://www.w3.org/2000/svg"
xmlns:svg="http://www.w3.org/2000/svg"&gt;
&lt;g&gt;
&lt;circle
style="fill:#ff00ff;stroke-width:0.264583"
id="path111"
cx="104.02724"
cy="152.19028"
r="73.177132" /&gt;
&lt;/g&gt;
&lt;/svg&gt;
</pre>
r="73.177132" />
</g>
</svg>
```

## Compliant Solution

```xml
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 210 297" width="210mm" height="297mm">
<circle cx="104.02724" cy="152.19028" r="73.177132" style="fill:#ff00ff;stroke-width:0.264583"/>
</svg>
```
53 changes: 27 additions & 26 deletions ecocode-rules-specifications/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;
}
```
Original file line number Diff line number Diff line change
@@ -1,73 +1,78 @@
<p>If possible, the utilisation of svg image format (or &lt;svg/&gt; 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 '&lt;html&gt;&lt;img src="xx/xx/image.bmp"&gt;&lt;/html&gt;'
...
}
</pre>
<h2>Compliant Solution</h2>
<pre>
public void foo() {
...
return '&lt;html&gt;&lt;img src="xx/xx/image.svg"&gt;&lt;/html&gt;'
...
}

public void foo2() {
...
return ('&lt;html&gt;&lt;svg width="100" height="100"&gt;' +
'&lt;circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" /&gt;' +
'&lt;/svg&gt;&lt;/html&gt;')
...
}
</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 ecocode-rules-specifications/src/main/rules/EC22/php/EC22.asciidoc
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 ecocode-rules-specifications/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;
```
Loading

0 comments on commit e988c1a

Please sign in to comment.