Skip to content

Commit

Permalink
green-code-initiative#63 - doc: Adds "Noncompliant" comment
Browse files Browse the repository at this point in the history
  • Loading branch information
jycr committed Mar 21, 2023
1 parent b90fa7c commit 664fe07
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ <h2>Noncompliant Code Example</h2>
private final List&lt;Integer&gt; ids = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

List&lt;Employee&gt; employees = new ArrayList&lt;&gt;();

for (Integer id: ids) {
Optional&lt;Employee&gt; employee = employeeRepository.findById(id); // Noncompliant
Optional&lt;Employee&gt; employee = employeeRepository.findById(id); // Noncompliant
if (employee.isPresent()) {
employees.add(employee.get());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ <h2>Noncompliant Code Example</h2>
int len = array.length;
boolean[] copy = new boolean[array.length];
for (int i = 0; i < len; i++) {
copy[i] = array[i];
copy[i] = array[i]; // Noncompliant
}
return copy;
</pre>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ <h2>Noncompliant Code Example</h2>
<pre>
List&lt;String&gt; objList = getData();

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ <h2>Noncompliant Code Example</h2>
return var1;

String var2 = "hello"
var2 = "world" //Non compliant cause never assigned
var2 = "world" // Noncompliant because it is never assigned

</pre>
<h2>Compliant Solution</h2>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<p>The form $i++ creates a temporary variable whereas ++$i does not. It save CPU cycles.</p>
<h2>Noncompliant Code Example</h2>
<pre>
i++
i++ // Noncompliant
</pre>
<h2>Compliant Solution</h2>
<pre>++i</pre>
<pre>
++i
</pre>
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,15 @@

<h2>Noncompliant Code Example</h2>
<pre>

/**
* Not compliant
*/
public class AvoidUsageOfStaticCollections {
public static final List<> LIST = new ArrayList<>();
public static final Set<> SET = new HashSet<>();
public static final Map<> MAP = new HashMap<>();
public static final List<> LIST = new ArrayList<>(); // Noncompliant
public static final Set<> SET = new HashSet<>(); // Noncompliant
public static final Map<> MAP = new HashMap<>(); // Noncompliant
}

</pre>

<h2>Compliant Solution</h2>
<pre>

/**
* Compliant
*/
public class GoodUsageOfStaticCollections {
public static volatile GoodUsageOfStaticCollections INSTANCE = new GoodUsageOfStaticCollections();

Expand All @@ -34,5 +25,4 @@ <h2>Compliant Solution</h2>
private GoodUsageOfStaticCollections() {
}
}

</pre>
</pre>
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<p>Don't set const parameter in batch update => Put its in query. Creating this parameter and destroying it consumes CPU cycles and RAM unnecessarily.</p>
<h2>Noncompliant Code Example</h2>
<pre>
public void foo() {
...
String query = "insert into mytable values(?,?,?)";
...
for(DummyClass o : list) {
stmt.setInt(1, 123);
stmt.setString(2, o.getName());
stmt.setDouble(3, o.getPrice());
stmt.addBatch();
}
...
}
public void foo() {
// ...
String query = "insert into mytable values(?,?,?)";
// ...
for(DummyClass o : list) {
stmt.setInt(1, 123); // Noncompliant
stmt.setString(2, o.getName());
stmt.setDouble(3, o.getPrice());
stmt.addBatch();
}
// ...
}
</pre>
<h2>Compliant Solution</h2>
<pre>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<p>Use of methods for basic operations</p>
<h2>Noncompliant Code Example</h2>
<pre>
$min = min($a, $b); // NOK
$min = min($a, $b); // Noncompliant
</pre>
<h2>Compliant Solution</h2>
<pre>
$min = ($a < $b) ? $a : $b; // NOK
$min = ($a < $b) ? $a : $b;
</pre>

0 comments on commit 664fe07

Please sign in to comment.