[WEB][DRAFT][25 meters] Avoid high level of nested loops
pymilone opened this issue · 2 comments
Efficient code: Avoid high level of nested loops
Platform
OS | OS version | Langage |
---|---|---|
- | - | Javascript |
Main caracteristics
ID | Title | Category | Sub-category |
---|---|---|---|
{id} | {title} | {Category} | {SubCategory} |
Severity / Remediation Cost
Severity | Remediation Cost |
---|---|
Minor | Medium |
Rule short description
Usings lots of levels of nested loops may result in useless processing.
Rule complete description
Text
Nested loops are frequently (but not always) bad practice, because they're frequently (but not always) overkill for what you're trying to do.
In many cases, there's a much faster and less wasteful way to accomplish the goal you're trying to achieve.
It is usually better to break this code into smaller functions.
HTML
for(var i = 1; i <= 100; i++)
{
for(var j = 1; j <= 100; j++)
{
for(var j = 1; j <= 100; j++)
{
statement2; // It will execute 1000000 times.
}
}
}
Implementation principle
Detect nested loops when loop depth exceeds X
X being a configurable value. Default to 4?
For me this rule is invalid. Nested loop is a problem of clean code, not green code.
Hello!
I completely agree with @jhertout's opinion: this rule is designed to improve the quality and readability of the code. Generally speaking, and without talking about eco-design, it's a bad practice in most cases. There's also a risk of a lot of false positives, and I'm convinced that many users wouldn't activate the rule because it's too restrictive and a bit off-topic. Also, it wouldn't be able to handle loops inside functions that are themselves in loops, so it would be pretty limited.
I therefore propose to close the ticket.