Skip to content

Commit

Permalink
Merge pull request #236 from green-code-initiative/EC34-replace
Browse files Browse the repository at this point in the history
[PR 128-ecocode] replace EC34 by EC35 : update CHANGELOG, update rule…
  • Loading branch information
dedece35 authored Oct 27, 2023
2 parents 16d1c90 + e97e9fc commit 6535e31
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 45 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- [#225](https://github.com/green-code-initiative/ecoCode/pull/225) Upgrade licence system and licence headers of Java files
- [#140](https://github.com/green-code-initiative/ecoCode/issues/140) Upgrade rule EC3 for Python : no implementation possible for python
- [#136](https://github.com/green-code-initiative/ecoCode/issues/136) Upgrade rule EC53 for Python : no implementation possible for python
- [#128](https://github.com/green-code-initiative/ecoCode/pull/128) Adding EC35 rule for Python and PHP : EC35 rule replaces EC34 with a specific use case ("file not found" sepcific)

### Deleted

Expand Down
87 changes: 44 additions & 43 deletions RULES.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Inside complex code parts (for exemple multiple loops, complex data constructions...), avoid using try...catch...finally.
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.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Inside complex code parts (for example multiple loops, complex data constructions...), avoid using try...catch...finally.
Inside complex code parts (for example 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.

Expand Down
15 changes: 15 additions & 0 deletions ecocode-rules-specifications/src/main/rules/EC35/EC35.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"title": "Avoid using try-catch statement with file open operation in try block",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"eco-design",
"performance",
"ecocode"
],
"defaultSeverity": "Minor"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Energy (J),515.855638,516.9188409999999
Transfer (B),1579453,1579457
Storage (B),637549804,637549804
46 changes: 46 additions & 0 deletions ecocode-rules-specifications/src/main/rules/EC35/php/EC35.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
Inside complex code parts (for exemple multiple loops, complex data constructions...), avoid using try...catch.
For the moment, this rule only deals with "file open" use case in try...catch blocks.

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.

## Noncompliant Code Example

```php
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

```php
//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;
}
```

include::../../etsdiff-methodology.asciidoc[]

## Case for a 1GB database:

image::https://live.staticflickr.com/65535/52622382871_f19da08db4_o.png[ETSdiff percent comparison]

[format=csv,cols="1h,1,1"]
|===
Source of impacts,Compliant,Non-compliant

include::1GB.etsdiff.csv[]
|===
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Inside complex code parts (for example multiple loops, complex data constructions...), avoid using try...catch.
For the moment, this rule only deals with "file open" use case in try...catch blocks.

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.

## Noncompliant Code Example

```python
try:
f = open(path)
print(fh.read())
except:
print('No such file '+path
finally:
f.close()
```
## Compliant Solution
```python
if os.path.isfile(path):
fh = open(path, 'r')
print(fh.read())
fh.close
```

0 comments on commit 6535e31

Please sign in to comment.