-
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.
Improve documentation and taxonomy of all Javascript rules
- Loading branch information
Showing
18 changed files
with
304 additions
and
129 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
41 changes: 28 additions & 13 deletions
41
ecocode-rules-specifications/src/main/rules/EC11/javascript/EC11.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 |
---|---|---|
@@ -1,21 +1,36 @@ | ||
This rule aims to reduce DOM access assigning its object to variable when access multiple time. | ||
It saves CPU cycles. | ||
:!sectids: | ||
|
||
== Examples | ||
== Why is this an issue? | ||
|
||
Examples of **incorrect** code for this rule: | ||
Accessing the Document Object Model (DOM) is a relatively expensive operation in terms of performance. | ||
Each time you access the DOM, the browser needs to traverse the document tree to find the requested element. | ||
By assigning the DOM object to a variable when accessed multiple times, you avoid redundant traversals, leading to improved performance. | ||
|
||
[source,js] | ||
Assigning the DOM object to a variable not only improves performance but also enhances code readability. | ||
It makes the code more concise and self-explanatory. | ||
Developers reading the code can understand that the variable holds a reference to a specific DOM element, and its subsequent use is likely for multiple operations. | ||
|
||
Here's an example in JavaScript to illustrate this rule: | ||
|
||
[source,js,data-diff-id="2",data-diff-type="noncompliant"] | ||
---- | ||
var el1 = document.getElementById("block1").test1; | ||
var el2 = document.getElementById("block1").test2; | ||
const width = document.getElementById('block').clientWidth; | ||
const height = document.getElementById('block').clientHeight; // Non-compliant | ||
---- | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
[source,js] | ||
[source,js,data-diff-id="1",data-diff-type="noncompliant"] | ||
---- | ||
var blockElement = document.getElementById("block1"); | ||
var el1 = blockElement.test1; | ||
var el2 = blockElement.test2; | ||
const blockElement = document.getElementById('block'); // Compliant | ||
const width = blockElement.clientWidth; | ||
const height = blockElement.clientHeight; | ||
---- | ||
|
||
In the first example, getElementById is called twice, potentially resulting in two separate traversals of the DOM tree. | ||
In the second example, the DOM element reference is cached in the `blockElement` variable, and subsequent property accesses use this cached reference. | ||
|
||
== Resources | ||
|
||
=== Documentation | ||
|
||
- https://github.com/cnumr/best-practices/blob/main/chapters/BP_054_en.md[CNUMR best practices] - Reduce DOM access via JavaScript | ||
- https://developer.mozilla.org/en-US/docs/Learn/Performance/JavaScript#tips_for_writing_more_efficient_code[Mozilla Web Technology for Developers] - Tips for writing more efficient code |
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
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
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
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
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
33 changes: 25 additions & 8 deletions
33
ecocode-rules-specifications/src/main/rules/EC24/javascript/EC24.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 |
---|---|---|
@@ -1,17 +1,34 @@ | ||
This rule aims at reducing CPU consumption by limiting the number of returns for a single SQL query. | ||
:!sectids: | ||
|
||
== Examples | ||
== Why is this an issue? | ||
|
||
Examples of **non compliant** code for this rule: | ||
SQL queries often involve processing large amounts of data, and fetching a large number of rows can consume significant CPU resources. | ||
By limiting the number of rows returned, you reduce the amount of processing that needs to be done by the database engine, which in turn lowers CPU consumption. | ||
|
||
[source,js] | ||
Transmitting a large number of rows over a network can be resource-intensive. | ||
By restricting the result set size, you reduce the amount of data that needs to be transferred between the database and the application, improving network efficiency. | ||
|
||
If you store data about customers, you certainly don’t need to retrieve information of all at once, because the larger the table will be, the more elements the query will return. | ||
|
||
[source,js,data-diff-id="1",data-diff-type="noncompliant"] | ||
---- | ||
const query = "SELECT * FROM clients"; | ||
const query = "SELECT * FROM customers"; // Non-compliant | ||
---- | ||
|
||
Examples of **compliant** code for this rule: | ||
It may therefore be a good idea to limit the results and use pagination, for example. | ||
|
||
[source,js] | ||
[source,js,data-diff-id="1",data-diff-type="compliant"] | ||
---- | ||
const query = "SELECT columns FROM table_name FETCH FIRST number ROWS ONLY"; | ||
const query = "SELECT id,name,email FROM customers FETCH FIRST 10 ROWS ONLY"; // Compliant | ||
---- | ||
|
||
== Resources | ||
|
||
=== Documentation | ||
|
||
- https://dev.mysql.com/doc/refman/8.0/en/limit-optimization.html[MySQL Reference Manual] - LIMIT Query Optimization | ||
- https://www.postgresql.org/docs/current/queries-limit.html[PostgreSQL: Documentation] - LIMIT and OFFSET | ||
|
||
=== Articles & blog posts | ||
|
||
- https://www.oreilly.com/library/view/high-performance-mysql/9780596101718/ch04.html[Query Performance Optimization] |
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
47 changes: 36 additions & 11 deletions
47
ecocode-rules-specifications/src/main/rules/EC25/javascript/EC25.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 |
---|---|---|
@@ -1,20 +1,45 @@ | ||
This rule aims to prohibit the usage of <img/> without specifying its source attribute because it causes extra and unnecessary http requests. This rule is build for the https://react.dev/[react library] and JSX. | ||
:!sectids: | ||
|
||
== Examples | ||
== Why is this an issue? | ||
|
||
Examples of **non compliant** code for this rule: | ||
When the src attribute is missing, some browsers may still attempt to make a request to the current URL (the base URL of the document) in an attempt to fetch the image. | ||
This can lead to unnecessary server requests, impacting performance and potentially causing errors. | ||
|
||
[source,html] | ||
Proper use of the src attribute is essential for web accessibility. | ||
Screen readers and other assistive technologies rely on valid image sources to provide meaningful information to users with disabilities. | ||
A missing src attribute can result in confusion and hinder accessibility. | ||
|
||
[source,typescriptjsx,data-diff-id="3",data-diff-type="noncompliant"] | ||
---- | ||
<img src='' /> | ||
<img/> | ||
return ( | ||
<> | ||
<img src="" /> // Non-compliant | ||
<img /> // Non-compliant | ||
</> | ||
) | ||
---- | ||
|
||
Examples of **compliant** code for this rule: | ||
The HTML specification requires the src attribute for the <img> element, and not including it may lead to non-compliance with standards. | ||
|
||
[source,js] | ||
[source,typescriptjsx,data-diff-id="4",data-diff-type="compliant"] | ||
---- | ||
import imgSvg from './img.svg' | ||
<img src='./img.svg' /> | ||
<img src={imgSvg} /> | ||
import myLogo from "./logo.svg" | ||
return ( | ||
<> | ||
<img src="./logo.svg" /> // Compliant | ||
<img src={myLogo} /> // Compliant | ||
</> | ||
) | ||
---- | ||
|
||
This rule is build for https://react.dev/[React] and JSX. | ||
|
||
== Resources | ||
|
||
=== Documentation | ||
|
||
- https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Images_in_HTML[Mozilla Web Technology for Developers] - Images in HTML | ||
|
||
=== Articles & blog posts | ||
|
||
- https://humanwhocodes.com/blog/2009/11/30/empty-image-src-can-destroy-your-site/[Empty image src can destroy your site] |
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
Oops, something went wrong.