Inside complex code parts (for exemple multiple loops, complex data constructions…​), avoid using try…​catch.

When an exception is thrown, a variable (the exception itself) is created in a catch block and it’s destruction consumes unnecessary CPU cycles and RAM. Prefer using logical tests in this cases.

Non compliant Code Example

try
{
  $picture = PDF_open_image_file($PDF, "jpeg", $imgFile, "", 0); // This is the original statement, this works on PHP4
}
catch(Exception $ex)
{
  $msg = "Error opening $imgFile for Product $row['Identifier']";
  throw new Exception($msg);
}

Compliant Solution

//try
if (file_exists($imgFile)) {
    $picture = PDF_open_image_file($PDF, "jpeg", $imgFile, "", 0);
}

//catch
if (!$picture) {
   $msg = "Error opening $imgFile for Product $row['Identifier']";
   print $msg;
}

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)

515.855638

516.9188409999999

Transfer (B)

1579453

1579457

Storage (B)

637549804

637549804