Do not call a function when declaring a for-type loop in order to avoid function calls each iteration. It saves CPU cycles.

Non compliant Code Example

for ($i = 0; $i <= foo(); $i++) {  // Noncompliant
        // ......
}

Compliant Solution

$maxI = foo();
for ($i = 0; $i <= $maxI; $i++) {
  .....
}

  OR

for ($i = 0, $maxI = foo(); $i <= $maxI; $i++) {
  .....
}
}

The three sources of impacts of a code identified are:

  • Energy: measured in joules (J)

  • Transfer: measured in Bytes (B)

  • Storage: measured in Bytes (B)

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:

ETSdiff percent comparison
Source of impacts Compliant Non-compliant

Energy (J)

144.635057

144.58341249999998

Transfer (B)

50000

50004

Storage (B)

637549590

637549590