From dce92dd98b61d6bafb6c0d57aabf9797920a44d1 Mon Sep 17 00:00:00 2001 From: utarwyn Date: Sat, 4 May 2024 13:28:00 +0200 Subject: [PATCH 1/2] Add HTML assembly configuration --- ecocode-rules-specifications/pom.xml | 12 ++++++++++++ .../src/main/assembly/html.xml | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 ecocode-rules-specifications/src/main/assembly/html.xml diff --git a/ecocode-rules-specifications/pom.xml b/ecocode-rules-specifications/pom.xml index 1c01ccfc..f2e0bfdd 100644 --- a/ecocode-rules-specifications/pom.xml +++ b/ecocode-rules-specifications/pom.xml @@ -198,6 +198,18 @@ + + assembly-html + prepare-package + + single + + + + ${project.basedir}/src/main/assembly/html.xml + + + true diff --git a/ecocode-rules-specifications/src/main/assembly/html.xml b/ecocode-rules-specifications/src/main/assembly/html.xml new file mode 100644 index 00000000..14bd14f8 --- /dev/null +++ b/ecocode-rules-specifications/src/main/assembly/html.xml @@ -0,0 +1,18 @@ + + html + + jar + + false + + + ${project.build.outputDirectory} + + io/ecocode/rules/html/*.* + + + + + From 1ca8e843ab0841ba4d743257667dfd625cd97535 Mon Sep 17 00:00:00 2001 From: utarwyn Date: Sat, 4 May 2024 13:29:31 +0200 Subject: [PATCH 2/2] Add HTML rule EC36 --- CHANGELOG.md | 2 + RULES.md | 107 +++++++++--------- .../src/main/rules/EC36/EC36.json | 22 ++++ .../src/main/rules/EC36/html/EC36.asciidoc | 47 ++++++++ 4 files changed, 125 insertions(+), 53 deletions(-) create mode 100644 ecocode-rules-specifications/src/main/rules/EC36/EC36.json create mode 100644 ecocode-rules-specifications/src/main/rules/EC36/html/EC36.asciidoc diff --git a/CHANGELOG.md b/CHANGELOG.md index 8649805b..24405ac0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- [#298](https://github.com/green-code-initiative/ecoCode/pull/298) Add HTML rule EC36 (Avoid autoplay for videos and audio content) + ### Changed ### Deleted diff --git a/RULES.md b/RULES.md index 414b71a7..99d235ba 100644 --- a/RULES.md +++ b/RULES.md @@ -18,59 +18,60 @@ Some are applicable for different technologies. - ❓ Rule to analyze for applicability - 🚫 Non applicable rule -| Rule key | Name | Description | Reference/Validation | Java | Php | JS | Python | Rust | C# | -|----------|---------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------|-----|----|--------|------|----| -| CRJVM205 | Force usage of FetchType LAZY for collections on JPA entities | | | 🚧 | ❓ | ❓ | ❓ | ❓ | ❓ | -| CRJVM206 | Avoid N+1 selects problem | | | 🚧 | ❓ | ❓ | ❓ | ❓ | ❓ | -| CRJVM207 | Customer data must have end-of-life information | | | 🚧 | ❓ | ❓ | ❓ | ❓ | ❓ | -| CRJVM??? | Persistence Java : Avoid Joints on non indexed columns | | | 🚧 | ❓ | ❓ | ❓ | ❓ | ❓ | -| EC1 | Calling a Spring repository inside a loop or a stream | The use of Spring repository in a loop or a stream induces unnecessary calculations by the CPU and therefore superfluous energy consumption. | | ✅ | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | -| EC2 | Multiple if-else statements | Using too many conditional if-else statements will impact performance since JVM will have to compare the conditions. Prefer using a switch statement instead of multiple if-else if possible, or refactor your code to reduce conditonnal statements on the same variable. Switch statement has a performance advantage over if – else. | | ✅ | ✅ | 🚀 | ✅ | 🚀 | 🚀 | -| EC3 | Getting the size of the collection in the loop | When iterating over any collection, fetch the size of the collection in advance to avoid fetching it on each iteration, this saves CPU cycles, and therefore consumes less power. | | ✅ | ✅ | 🚀 | 🚫 | 🚀 | 🚫 | -| EC4 | Use global variables | When using a global variable, the interpretation engine must check: 1) that it exists in the current scope, in the one above, etc. ; 2) the variable has a value; 3) ... To avoid all these checks, it is often possible to pass the useful variables as arguments of routines, making them local. This process saves computational time (CPU cycles). | [cnumr best practices (3rd edition) BP_050 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | 🚫 | ✅ | 🚀 | ✅ | 🚀 | 🚫 | -| EC5 | Usage of preparedStatement instead of Statement | SQL will only commit the query once, whereas if you used only one statement, it would commit the query every time and thus induce unnecessary calculations by the CPU and therefore superfluous energy consumption. | | ✅ | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | -| EC7 | Rewrite native getter/setters | Overloading them lengthens the compilation and execution times of these methods, which are usually much better optimized by the language than by the developer. | [cnumr best practices (3rd edition) BP_062 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | 🚫 | 🚫 | 🚀 | ✅ | 🚀 | 🚫 | -| EC8 | Avoid using high accuracy geolocation | ESLint key : @ecocode/avoid-high-accuracy-geolocation /// type : Front-end | | ❓ | ❓ | ✅ | ❓ | ❓ | ❓ | -| EC9 | No import all from library | ESLint key : @ecocode/no-import-all-from-library /// type : Common | | ❓ | ❓ | ✅ | ❓ | ❓ | ❓ | -| EC10 | Use unoptimized vector images | Less heavy SVG images using less bandwidth | [cnumr best practices (3rd edition) BP_036](https://github.com/cnumr/best-practices/blob/main/chapters/BP_036_fr.md) | 🚧 | 🚀 | 🚀 | ✅ | 🚀 | 🚫 | -| EC11 | Call a DOM element multiple times without caching | ESLint key : @ecocode/no-multiple-access-dom-element /// type : Front-end | | 🚫 | ❓ | ✅ | 🚫 | 🚫 | ❓ | -| EC12 | Modify several CSS properties all at once | ESLint key : @ecocode/no-multiple-style-changes /// type : Front-end | | 🚫 | ❓ | ✅ | 🚫 | 🚫 | ❓ | -| EC13 | Prefer API collections with pagination | ESLint key : @ecocode/prefer-collections-with-pagination /// type : Back-end | | ❓ | ❓ | ✅ | ❓ | ❓ | ❓ | -| EC22 | The use of methods for basic operations | Using methods for basic operations consumes additional system resources. The interpreter must in effect and solve the objects and then the methods, just to carry out these simple operations of the language. | [cnumr best practices (3rd edition) BP_048 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | 🚫 | 🚫 | 🚀 | 🚀 | 🚀 | 🚫 | -| EC24 | Limit the number of returns for a SQL query | ESLint key : @ecocode/limit-db-query-results /// type : Back-end | | ✅ | 🚀 | ✅ | 🚀 | 🚀 | 🚀 | -| EC25 | Do not use an image with empty source attribute | ESLint key : @ecocode/no-empty-image-src-attribute /// type : Front-end | | ❓ | ❓ | ✅ | ❓ | ❓ | ❓ | -| EC26 | Prefer shorthand CSS notations | ESLint key : @ecocode/prefer-shorthand-css-notations /// type : Front-end | | ❓ | ❓ | ✅ | ❓ | ❓ | ❓ | -| EC27 | Usage of system.arraycopy to copy arrays | Programs spend most of the time in loops. These can be resource consuming, especially when they integrate heavy processing (IO access). Moreover, the size of the data and processing inside the loops will not allow full use of hardware mechanisms such as the cache or compiler optimization mechanisms. | | ✅ | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | -| EC28 | Optimize read file exceptions | | | ✅ | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | -| EC29 | Avoid usage of CSS animations | ESLint key : @ecocode/avoid-css-animations /// type : Front-end | | ❓ | ❓ | ✅ | ❓ | ❓ | ❓ | -| EC30 | Provide a print stylesheet | ESLint key : @ecocode/provide-print-css /// type : Front-end | | ❓ | ❓ | ✅ | ❓ | ❓ | ❓ | -| EC31 | Prefer lighter formats for image files | ESLint key : ... /// type : ... | | ❓ | ❓ | 🚀 | ❓ | ❓ | ❓ | -| EC32 | Initialize builder/buffer with the appropriate size | If you know in advance how many characters would be appended, initialize builder/buffer with the appropriate size. They will thus never have to be resized. This saves CPU cycles and therefore consumes less energy. | | ✅ | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | -| EC35 | Using try...catch calls (on File Not Found error) | When an exception is thrown, a variable (the exception itself) is created in the catch block and destroyed at the end of the block. Creating this variable and destroying it consumes CPU cycles and RAM unnecessarily. That is why it is important not to use this construction and to prefer, as much as possible, a logical test. This new rule replace old EC34 only for a particular use case (FileNotFoundException) | [cnumr best practices (3rd edition) BP_047 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | 🚫 | ✅ | 🚀 | ✅ | 🚀 | 🚫 | -| EC66 | Use single quote (') instead of quotation mark (") | The shape using the quotation marks allows the developer to insert variables that will be substituted at run time. But if the string does not have a variable, use quotes instead. Thus, language will not look for variables to subtituture, which will reduce the consumption of CPU cycles. | [cnumr best practices (3rd edition) BP_066 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | 🚀 | ✅ | 🚀 | 🚫 | 🚀 | 🚫 | -| EC67 | Use the $i++ variable during an iteration | The $i++ form has the disadvantage of generating a tem-porary variable during incrementation, which is not the case with the ++$i form. | [cnumr best practices (3rd edition) BP_067 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | ✅ | ✅ | 🚀 | 🚫 | 🚫 | 🚫 | -| EC69 | Calling a loop invariant function in a loop condition | Avoid calling loop invariant functions in loop conditions. | [cnumr best practices (3rd edition) BP_069 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | ✅ | ✅ | 🚀 | ✅ | 🚀 | ✅ | -| EC72 | Perform an SQL query inside a loop | Servers are optimized to process multiple selections, insertions, or changes in a single query or transaction. consume CPU cycles, RAM, and bandwidth unnecessarily. | [cnumr best practices (3rd edition) BP_072](https://github.com/cnumr/best-practices/blob/main/chapters/BP_072_fr.md) | ✅ | ✅ | 🚀 | ✅ | 🚀 | ✅ | -| EC74 | Write SELECT * FROM | The database server must resolve the fields based on the schema. If you are familiar with the diagram, it is strongly recommended to name the fields. | [cnumr best practices (3rd edition) BP_074 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | ✅ | ✅ | 🚀 | ✅ | 🚀 | 🚀 | -| EC75 | Don't concatenate strings in loops | Avoid repeated string allocations and consider using a StringBuilder instead. | | 🚫 | ❓ | ❓ | ❓ | ❓ | ✅ | -| EC76 | Usage of static collections | Avoid usage of static collections. If you want to use static collections make them final and create for example a singleton if needed containing the collections. The static fields are more complicated for the Garbage Collector to manage and can lead to memory leaks. | | ✅ | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | -| EC77 | Usage Pattern.compile() in a non-static context | Avoid using Pattern.compile() in a non-static context. This operation requires a non negligible amount of computational power, Using a single match saves CPU cycles and RAM consumption. | | ✅ | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | -| EC78 | Const parameter in batch update | Don't set const parameter in batch update => Put its in query. Creating this parameter and destroying it consumes CPU cycles and RAM unnecessarily. | | ✅ | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | -| EC79 | Free resources | try-with-resources Statement needs to be implemented for any object that implements the AutoCloseable interface, it save computer resources. | | ✅ | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | -| EC81 | Specify struct layouts | When possible, specify struct layouts to optimize their memory footprint | | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | ✅ | -| EC82 | Make variable constant | A variable is never reassigned and can be made constant | | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | ✅ | -| EC83 | Replace Enum ToString() with nameof | When no string format is applied, use nameof instead of ToString() for performance | | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | ✅ | -| EC84 | Avoid async void methods | Use async Task methods instead, for performance, stability and testability | | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | ✅ | -| EC85 | Make type sealed | Seal types that don't need inheritance for performance reasons | | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | ✅ | -| EC203 | Detect unoptimized file formats | When it is possible, to use svg format image over other image format | | 🚀 | 🚀 | 🚀 | ✅ | 🚀 | 🚀 | -| EC404 | Avoid list comprehension in iterations | Use generator comprehension instead of list comprehension in for loop declaration | | 🚫 | 🚫 | 🚫 | ✅ | 🚫 | 🚫 | -| | Use official social media sharing buttons | These JavaScript plugins are very resource-intensive: to work, they require a large number of requests and download heavy files. It is better to prefer direct links. | [cnumr best practices (3rd edition) BP_019](https://github.com/cnumr/best-practices/blob/main/chapters/BP_019_fr.md) | 🚫 | 🚫 | 🚀 | 🚫 | 🚫 | 🚫 | -| | Non-grouped similar CSS declarations | When multiple Document Object Model (DOM) elements have common CSS properties, declare them together in the same style sheet. This method reduces the weight of CSS. | [cnumr best practices (3rd edition) BP_025](https://github.com/cnumr/best-practices/blob/main/chapters/BP_025_fr.md) | 🚫 | 🚫 | 🚧 | 🚫 | 🚫 | 🚫 | -| | Non-standard fonts used | Prefer standard fonts, as they are already present on the user's computer, so they do not need to download them. This saves bandwidth, while speeding up the display of the site. | [cnumr best practices (3rd edition) BP_029](https://github.com/cnumr/best-practices/blob/main/chapters/BP_029_fr.md) | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | -| | Non-outsourced CSS and Javascript | If you include CSS or JavaScript code in the body of the HTML file, while the HTML file is used by several pages (or even the entire site), this code must be transferred for each page requested by the user, which increases the volume of data transmitted. | [cnumr best practices (3rd edition) BP_032](https://github.com/cnumr/best-practices/blob/main/chapters/BP_032_fr.md) | 🚫 | 🚫 | 🚀 | 🚫 | 🚫 | 🚫 | -| | Resize images browser-side | Do not resize images using the HEIGHT and WIDTH attributes of the HTML code. This approach requires transferring these images to their original size, wasting bandwidth and CPU cycles. | [cnumr best practices (3rd edition) BP_034](https://github.com/cnumr/best-practices/blob/main/chapters/BP_034_fr.md) | 🚫 | 🚫 | 🚧 | 🚫 | 🚫 | 🚫 | -| | Modify the DOM when traversing it | Modifying the DOM (Document Object Model) as you traverse it can lead to situations where the loop becomes very resource-intensive, especially CPU cycles. | [cnumr best practices (3rd edition) BP_041](https://github.com/cnumr/best-practices/blob/main/chapters/BP_041_fr.md) | 🚫 | 🚫 | 🚧 | 🚫 | 🚫 | 🚫 | -| | Edit DOM elements to make it invisible | When an element of the Document Object Model (DOM) needs to be modified by several properties, each change in style or content will generate a repaint or reflow. | [cnumr best practices (3rd edition) BP_042](https://github.com/cnumr/best-practices/blob/main/chapters/BP_042_fr.md) | 🚫 | 🚫 | 🚀 | 🚫 | 🚫 | 🚫 | +| Rule key | Name | Description | Reference/Validation | Java | Php | JS | Python | Rust | C# | HTML | +|----------|---------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------|-----|----|--------|------|----|------| +| CRJVM205 | Force usage of FetchType LAZY for collections on JPA entities | | | 🚧 | ❓ | ❓ | ❓ | ❓ | ❓ | 🚫 | +| CRJVM206 | Avoid N+1 selects problem | | | 🚧 | ❓ | ❓ | ❓ | ❓ | ❓ | 🚫 | +| CRJVM207 | Customer data must have end-of-life information | | | 🚧 | ❓ | ❓ | ❓ | ❓ | ❓ | ❓ | +| CRJVM??? | Persistence Java : Avoid Joints on non indexed columns | | | 🚧 | ❓ | ❓ | ❓ | ❓ | ❓ | 🚫 | +| EC1 | Calling a Spring repository inside a loop or a stream | The use of Spring repository in a loop or a stream induces unnecessary calculations by the CPU and therefore superfluous energy consumption. | | ✅ | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | +| EC2 | Multiple if-else statements | Using too many conditional if-else statements will impact performance since JVM will have to compare the conditions. Prefer using a switch statement instead of multiple if-else if possible, or refactor your code to reduce conditonnal statements on the same variable. Switch statement has a performance advantage over if – else. | | ✅ | ✅ | 🚀 | ✅ | 🚀 | 🚀 | 🚫 | +| EC3 | Getting the size of the collection in the loop | When iterating over any collection, fetch the size of the collection in advance to avoid fetching it on each iteration, this saves CPU cycles, and therefore consumes less power. | | ✅ | ✅ | 🚀 | 🚫 | 🚀 | 🚫 | 🚫 | +| EC4 | Use global variables | When using a global variable, the interpretation engine must check: 1) that it exists in the current scope, in the one above, etc. ; 2) the variable has a value; 3) ... To avoid all these checks, it is often possible to pass the useful variables as arguments of routines, making them local. This process saves computational time (CPU cycles). | [cnumr best practices (3rd edition) BP_050 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | 🚫 | ✅ | 🚀 | ✅ | 🚀 | 🚫 | 🚫 | +| EC5 | Usage of preparedStatement instead of Statement | SQL will only commit the query once, whereas if you used only one statement, it would commit the query every time and thus induce unnecessary calculations by the CPU and therefore superfluous energy consumption. | | ✅ | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | +| EC7 | Rewrite native getter/setters | Overloading them lengthens the compilation and execution times of these methods, which are usually much better optimized by the language than by the developer. | [cnumr best practices (3rd edition) BP_062 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | 🚫 | 🚫 | 🚀 | ✅ | 🚀 | 🚫 | 🚫 | +| EC8 | Avoid using high accuracy geolocation | ESLint key : @ecocode/avoid-high-accuracy-geolocation /// type : Front-end | | ❓ | ❓ | ✅ | ❓ | ❓ | ❓ | 🚫 | +| EC9 | No import all from library | ESLint key : @ecocode/no-import-all-from-library /// type : Common | | ❓ | ❓ | ✅ | ❓ | ❓ | ❓ | 🚫 | +| EC10 | Use unoptimized vector images | Less heavy SVG images using less bandwidth | [cnumr best practices (3rd edition) BP_036](https://github.com/cnumr/best-practices/blob/main/chapters/BP_036_fr.md) | 🚧 | 🚀 | 🚀 | ✅ | 🚀 | 🚫 | ❓ | +| EC11 | Call a DOM element multiple times without caching | ESLint key : @ecocode/no-multiple-access-dom-element /// type : Front-end | | 🚫 | ❓ | ✅ | 🚫 | 🚫 | ❓ | 🚫 | +| EC12 | Modify several CSS properties all at once | ESLint key : @ecocode/no-multiple-style-changes /// type : Front-end | | 🚫 | ❓ | ✅ | 🚫 | 🚫 | ❓ | 🚫 | +| EC13 | Prefer API collections with pagination | ESLint key : @ecocode/prefer-collections-with-pagination /// type : Back-end | | ❓ | ❓ | ✅ | ❓ | ❓ | ❓ | 🚫 | +| EC22 | The use of methods for basic operations | Using methods for basic operations consumes additional system resources. The interpreter must in effect and solve the objects and then the methods, just to carry out these simple operations of the language. | [cnumr best practices (3rd edition) BP_048 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | 🚫 | 🚫 | 🚀 | 🚀 | 🚀 | 🚫 | 🚫 | +| EC24 | Limit the number of returns for a SQL query | ESLint key : @ecocode/limit-db-query-results /// type : Back-end | | ✅ | 🚀 | ✅ | 🚀 | 🚀 | 🚀 | 🚫 | +| EC25 | Do not use an image with empty source attribute | ESLint key : @ecocode/no-empty-image-src-attribute /// type : Front-end | | ❓ | ❓ | ✅ | ❓ | ❓ | ❓ | 🚀 | +| EC26 | Prefer shorthand CSS notations | ESLint key : @ecocode/prefer-shorthand-css-notations /// type : Front-end | | ❓ | ❓ | ✅ | ❓ | ❓ | ❓ | 🚫 | +| EC27 | Usage of system.arraycopy to copy arrays | Programs spend most of the time in loops. These can be resource consuming, especially when they integrate heavy processing (IO access). Moreover, the size of the data and processing inside the loops will not allow full use of hardware mechanisms such as the cache or compiler optimization mechanisms. | | ✅ | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | +| EC28 | Optimize read file exceptions | | | ✅ | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | +| EC29 | Avoid usage of CSS animations | ESLint key : @ecocode/avoid-css-animations /// type : Front-end | | ❓ | ❓ | ✅ | ❓ | ❓ | ❓ | 🚫 | +| EC30 | Provide a print stylesheet | ESLint key : @ecocode/provide-print-css /// type : Front-end | | ❓ | ❓ | ✅ | ❓ | ❓ | ❓ | 🚫 | +| EC31 | Prefer lighter formats for image files | ESLint key : ... /// type : ... | | ❓ | ❓ | 🚀 | ❓ | ❓ | ❓ | 🚀 | +| EC32 | Initialize builder/buffer with the appropriate size | If you know in advance how many characters would be appended, initialize builder/buffer with the appropriate size. They will thus never have to be resized. This saves CPU cycles and therefore consumes less energy. | | ✅ | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | +| EC35 | Using try...catch calls (on File Not Found error) | When an exception is thrown, a variable (the exception itself) is created in the catch block and destroyed at the end of the block. Creating this variable and destroying it consumes CPU cycles and RAM unnecessarily. That is why it is important not to use this construction and to prefer, as much as possible, a logical test. This new rule replace old EC34 only for a particular use case (FileNotFoundException) | [cnumr best practices (3rd edition) BP_047 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | 🚫 | ✅ | 🚀 | ✅ | 🚀 | 🚫 | 🚫 | +| EC36 | Avoid autoplay for videos and audio content | Autoplaying media consumes unnecessary energy, especially when users might not be actively engaging with the content. | [cnumr best practices BP_4003](https://github.com/cnumr/best-practices/blob/main/chapters/BP_4003_en.md) | 🚫 | 🚫 | 🚀 | 🚫 | 🚫 | 🚫 | 🚧 | +| EC66 | Use single quote (') instead of quotation mark (") | The shape using the quotation marks allows the developer to insert variables that will be substituted at run time. But if the string does not have a variable, use quotes instead. Thus, language will not look for variables to subtituture, which will reduce the consumption of CPU cycles. | [cnumr best practices (3rd edition) BP_066 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | 🚀 | ✅ | 🚀 | 🚫 | 🚀 | 🚫 | 🚫 | +| EC67 | Use the $i++ variable during an iteration | The $i++ form has the disadvantage of generating a tem-porary variable during incrementation, which is not the case with the ++$i form. | [cnumr best practices (3rd edition) BP_067 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | ✅ | ✅ | 🚀 | 🚫 | 🚫 | 🚫 | 🚫 | +| EC69 | Calling a loop invariant function in a loop condition | Avoid calling loop invariant functions in loop conditions. | [cnumr best practices (3rd edition) BP_069 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | ✅ | ✅ | 🚀 | ✅ | 🚀 | ✅ | 🚫 | +| EC72 | Perform an SQL query inside a loop | Servers are optimized to process multiple selections, insertions, or changes in a single query or transaction. consume CPU cycles, RAM, and bandwidth unnecessarily. | [cnumr best practices (3rd edition) BP_072](https://github.com/cnumr/best-practices/blob/main/chapters/BP_072_fr.md) | ✅ | ✅ | 🚀 | ✅ | 🚀 | ✅ | 🚫 | +| EC74 | Write SELECT * FROM | The database server must resolve the fields based on the schema. If you are familiar with the diagram, it is strongly recommended to name the fields. | [cnumr best practices (3rd edition) BP_074 (no longer exists in edition 4)](https://www.greenit.fr/2019/05/07/ecoconception-web-les-115-bonnes-pratiques-3eme-edition/) | ✅ | ✅ | 🚀 | ✅ | 🚀 | 🚀 | 🚫 | +| EC75 | Don't concatenate strings in loops | Avoid repeated string allocations and consider using a StringBuilder instead. | | 🚫 | ❓ | ❓ | ❓ | ❓ | ✅ | 🚫 | +| EC76 | Usage of static collections | Avoid usage of static collections. If you want to use static collections make them final and create for example a singleton if needed containing the collections. The static fields are more complicated for the Garbage Collector to manage and can lead to memory leaks. | | ✅ | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | +| EC77 | Usage Pattern.compile() in a non-static context | Avoid using Pattern.compile() in a non-static context. This operation requires a non negligible amount of computational power, Using a single match saves CPU cycles and RAM consumption. | | ✅ | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | +| EC78 | Const parameter in batch update | Don't set const parameter in batch update => Put its in query. Creating this parameter and destroying it consumes CPU cycles and RAM unnecessarily. | | ✅ | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | +| EC79 | Free resources | try-with-resources Statement needs to be implemented for any object that implements the AutoCloseable interface, it save computer resources. | | ✅ | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | +| EC81 | Specify struct layouts | When possible, specify struct layouts to optimize their memory footprint | | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | ✅ | 🚫 | +| EC82 | Make variable constant | A variable is never reassigned and can be made constant | | 🚀 | 🚀 | 🚀 | 🚀 | 🚀 | ✅ | 🚫 | +| EC83 | Replace Enum ToString() with nameof | When no string format is applied, use nameof instead of ToString() for performance | | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | ✅ | 🚫 | +| EC84 | Avoid async void methods | Use async Task methods instead, for performance, stability and testability | | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | ✅ | 🚫 | +| EC85 | Make type sealed | Seal types that don't need inheritance for performance reasons | | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | ✅ | 🚫 | +| EC203 | Detect unoptimized file formats | When it is possible, to use svg format image over other image format | | 🚀 | 🚀 | 🚀 | ✅ | 🚀 | 🚀 | 🚫 | +| EC404 | Avoid list comprehension in iterations | Use generator comprehension instead of list comprehension in for loop declaration | | 🚫 | 🚫 | 🚫 | ✅ | 🚫 | 🚫 | 🚫 | +| | Use official social media sharing buttons | These JavaScript plugins are very resource-intensive: to work, they require a large number of requests and download heavy files. It is better to prefer direct links. | [cnumr best practices (3rd edition) BP_019](https://github.com/cnumr/best-practices/blob/main/chapters/BP_019_fr.md) | 🚫 | 🚫 | 🚀 | 🚫 | 🚫 | 🚫 | 🚀 | +| | Non-grouped similar CSS declarations | When multiple Document Object Model (DOM) elements have common CSS properties, declare them together in the same style sheet. This method reduces the weight of CSS. | [cnumr best practices (3rd edition) BP_025](https://github.com/cnumr/best-practices/blob/main/chapters/BP_025_fr.md) | 🚫 | 🚫 | 🚧 | 🚫 | 🚫 | 🚫 | 🚫 | +| | Non-standard fonts used | Prefer standard fonts, as they are already present on the user's computer, so they do not need to download them. This saves bandwidth, while speeding up the display of the site. | [cnumr best practices (3rd edition) BP_029](https://github.com/cnumr/best-practices/blob/main/chapters/BP_029_fr.md) | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | 🚫 | +| | Non-outsourced CSS and Javascript | If you include CSS or JavaScript code in the body of the HTML file, while the HTML file is used by several pages (or even the entire site), this code must be transferred for each page requested by the user, which increases the volume of data transmitted. | [cnumr best practices (3rd edition) BP_032](https://github.com/cnumr/best-practices/blob/main/chapters/BP_032_fr.md) | 🚫 | 🚫 | 🚀 | 🚫 | 🚫 | 🚫 | 🚀 | +| | Resize images browser-side | Do not resize images using the HEIGHT and WIDTH attributes of the HTML code. This approach requires transferring these images to their original size, wasting bandwidth and CPU cycles. | [cnumr best practices (3rd edition) BP_034](https://github.com/cnumr/best-practices/blob/main/chapters/BP_034_fr.md) | 🚫 | 🚫 | 🚧 | 🚫 | 🚫 | 🚫 | 🚀 | +| | Modify the DOM when traversing it | Modifying the DOM (Document Object Model) as you traverse it can lead to situations where the loop becomes very resource-intensive, especially CPU cycles. | [cnumr best practices (3rd edition) BP_041](https://github.com/cnumr/best-practices/blob/main/chapters/BP_041_fr.md) | 🚫 | 🚫 | 🚧 | 🚫 | 🚫 | 🚫 | 🚫 | +| | Edit DOM elements to make it invisible | When an element of the Document Object Model (DOM) needs to be modified by several properties, each change in style or content will generate a repaint or reflow. | [cnumr best practices (3rd edition) BP_042](https://github.com/cnumr/best-practices/blob/main/chapters/BP_042_fr.md) | 🚫 | 🚫 | 🚀 | 🚫 | 🚫 | 🚫 | 🚫 | ## Rules to be reworked / measured / clarified diff --git a/ecocode-rules-specifications/src/main/rules/EC36/EC36.json b/ecocode-rules-specifications/src/main/rules/EC36/EC36.json new file mode 100644 index 00000000..1ea23699 --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC36/EC36.json @@ -0,0 +1,22 @@ +{ + "title": "Avoid autoplay for videos and audio content", + "type": "CODE_SMELL", + "code": { + "impacts": { + "MAINTAINABILITY": "MEDIUM" + }, + "attribute": "EFFICIENT" + }, + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + "ecocode", + "eco-design", + "video", + "audio" + ], + "defaultSeverity": "Major" +} diff --git a/ecocode-rules-specifications/src/main/rules/EC36/html/EC36.asciidoc b/ecocode-rules-specifications/src/main/rules/EC36/html/EC36.asciidoc new file mode 100644 index 00000000..41059f3d --- /dev/null +++ b/ecocode-rules-specifications/src/main/rules/EC36/html/EC36.asciidoc @@ -0,0 +1,47 @@ +:!sectids: + +== Why is this an issue? + +Autoplaying media consumes unnecessary energy, especially when users might not be actively engaging with the content. +This can drain battery life on devices, particularly on mobile devices, leading to increased energy consumption and potentially contributing to environmental impact. +It can also consume data, particularly for users on limited data plans or in areas with poor internet connectivity. +This can lead to unnecessary data usage and potentially increased costs for users. + +However, even without autoplay, segments of video or audio files might still download. +This leads to unnecessary data usage, especially if users don't commence playback. +To mitigate this, it's crucial to prevent browsers from preloading any content by configuring the appropriate settings. + +Video: + +[source,html,data-diff-id="1",data-diff-type="noncompliant"] +---- + // Non-compliant +---- + +[source,html,data-diff-id="1",data-diff-type="compliant"] +---- + // Compliant +---- + +Audio: + +[source,html,data-diff-id="1",data-diff-type="noncompliant"] +---- + // Non-compliant +---- + +[source,html,data-diff-id="1",data-diff-type="compliant"] +---- + // Compliant +---- + +== Resources + +=== Documentation + +- https://github.com/cnumr/best-practices/blob/main/chapters/BP_4003_en.md[CNUMR best practices] - Avoid autoplay for videos and audio content + +=== Articles & blog posts + +- https://eco-conception.designersethiques.org/guide/en/content/5-2-video.html[eco-conception.designersethiques.org - 5.2. Video and sound] +- https://www.ecoindex.fr/en/ecodesign/[www.ecoindex.fr - Some good practices]