-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #236 from green-code-initiative/EC34-replace
[PR 128-ecocode] replace EC34 by EC35 : update CHANGELOG, update rule…
- Loading branch information
Showing
8 changed files
with
137 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
ecocode-rules-specifications/src/main/rules/EC34/php/EC34.asciidoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
ecocode-rules-specifications/src/main/rules/EC34/python/EC34.asciidoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
ecocode-rules-specifications/src/main/rules/EC35/EC35.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
3 changes: 3 additions & 0 deletions
3
ecocode-rules-specifications/src/main/rules/EC35/php/1GB.etsdiff.csv
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
46
ecocode-rules-specifications/src/main/rules/EC35/php/EC35.asciidoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[] | ||
|=== |
26 changes: 26 additions & 0 deletions
26
ecocode-rules-specifications/src/main/rules/EC35/python/EC35.asciidoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` | ||
|