Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove non-reachable code #371

Open
skaslay1910 opened this issue Feb 2, 2025 · 3 comments
Open

Remove non-reachable code #371

skaslay1910 opened this issue Feb 2, 2025 · 3 comments

Comments

@skaslay1910
Copy link

skaslay1910 commented Feb 2, 2025

Remove non-reachable code

Platform

OS OS version Langage
All OS/Especially Javascript, PHP and languages other than JAVA and C#

Rule short description

SonarQube as a code profiler has checks to list out unused variable, unused arguments etc but there are no rules to check for unused code. Intelligent editors of Java and .NET tackles this issue, but there is no way to detect this issue in Javascript and other scripting languages.

Rule complete description

Check for non-reachable code. Removing non-reachable code can lead to compiler optimization resulting in less CPU work, less memory usage and thus saving energy consumption
Also, leaving such unused code in the software results in maintainability issues for the support team. This results in increased efforts leading to increased usage of resources like device power consumption.

Optimized API: List Shallow Copy - Module copy

Platform

OS OS version Langage
- - Python

Main caracteristics

ID Title Category Sub-category
EOPT001 List Shallow Copy - Module copy Environment Optimized API

Severity / Remediation Cost

Severity Remediation Cost
Minor Minor

Rule short description

Using copy.copy(x) of module copy to perform a shallow copy of a list is not energy efficient.

Rule complete description

Text

Using copy.copy(x) of module copy to perform a shallow copy of a list is not energy efficient.
Prefer the usage of list.copy() which is more energy friendly.

HTML

<p>Using <code>copy.copy(x)</code> of <code>module copy</code> to perform a shallow
copy of a list is not energy efficient.</p>
<p>Prefer the usage of <code>list.copy()</code> which is more energy friendly.</p>
<h2>Noncompliant Code Example</h2>
<pre>
import copy
my_list = [1, 2, [3, 4], 5]
list_copy = copy.copy(my_list)
</pre>
<h2>Compliant Solution</h2>
<pre>
my_list = [1, 2, [3, 4], 5]
list_copy = my_list.copy()
</pre>

Implementation principle

  • Inspect the import node to find copy import
  • Inspect the ARG_LIST node
  • If the direct parent is CALL_EXPR tree and the function is copy.copy()
  • If the function is present, check the first argument
  • If it is a list, report the line
@dedece35
Copy link
Member

dedece35 commented Feb 14, 2025

Hi @skaslay1910,

thank you for your ideas !
I refactored your issue to delete templated and unused description parts.
In this issue you have two different ideas ... could you create a new issue for the second idea please ("Optimized API: List Shallow Copy - Module copy") ?
Another question about the second idea : is this idea only for python language ? can you give us a technical documentation which deal with this aspect ? or maybe a performance tests done by your team ? in fact, we need to prove your idea to create a new rule.

what do you think about them @vdebellabre for C#, @utarwyn for JS ?

@utarwyn
Copy link
Member

utarwyn commented Feb 16, 2025

Hello @dedece35!
This rule already exists in SonarJS: https://sonarsource.github.io/rspec/#/rspec/S1763/javascript
It also exists in ESLint. So for me, there's nothing to do in creedengo-javascript. Maybe tag it as eco-design.

@sunithakaslay
Copy link

Hello everyone,
This is Sunitha Kaslay from Capgemini. I did my research on testing the unreachable code in Javascript and found that SonarQube does look out for this issue. Thank you @utarwyn for pointing it out.
As for the second idea, about list.copy() being more efficient than copy.copy() is based on the pure understanding of the implementation of these 2 functionalities. I have not found any technical paper that supports it. But the shallow copy provided by list is built in and native to the list class and hence will have no additional overheads, whereas copy.copy() is a general purpose method that supports different kinds of objects and hence will have the additional overhead of determining which type of object is being shallow copied. This observation is based on the general best practice of software engineering that a complex method will make use of additional CPU cycles than a less complex or rather native method of a class.
I have no performance test results to support the same, so you can ignore the idea!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants