diff --git a/CHANGELOG.md b/CHANGELOG.md index 366aab86..3ae9b5c8 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 +- [#400](https://github.com/green-code-initiative/creedengo-rules-specifications/pull/400) Add rule GCI535 - Prefer usage of Intl.NumberFormat + ### Changed - Correction of various typos in rules documentations diff --git a/RULES.md b/RULES.md index ff95eea9..19e391c9 100644 --- a/RULES.md +++ b/RULES.md @@ -1,10 +1,10 @@ # Rules - [Rules](#rules) - - [Rules support matrix by techno](#rules-support-matrix-by-techno) - - [Rules to be reworked / measured / clarified](#rules-to-be-reworked--measured--clarified) - - [Deprecated rules](#deprecated-rules) - - [Refused / Deleted rules](#refused--deleted-rules) + - [Rules support matrix by techno](#rules-support-matrix-by-techno) + - [Rules to be reworked / measured / clarified](#rules-to-be-reworked--measured--clarified) + - [Deprecated rules](#deprecated-rules) + - [Refused / Deleted rules](#refused--deleted-rules) ## Rules support matrix by techno @@ -83,6 +83,7 @@ Some are applicable for different technologies. | | 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) | 🚫 | 🚫 | πŸš€ | 🚫 | 🚫 | 🚫 | 🚫 | +| GCI535 | Use native Intl.NumberFormat to format numbers | There's no need to use a library to display formatted numbers in a recent browser. Use Intl.NumberFormat for that use case. | | 🚫 | 🚫 | 🚧 | 🚫 | 🚫 | 🚫 | 🚫 | ## Rules to be reworked / measured / clarified @@ -109,7 +110,6 @@ will be completely deleted in next releases and moved to bottom deleted rules ar |----------|------------|-----------------------------------|-----------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | EC34 | Java / PHP | Using try...catch...finally calls | Implementation is too simple (only detection of presence of "try" statement) AND replaced by `GCI35` rule | Github discussion with measures : [general/java](https://github.com/green-code-initiative/creedengo-rules-specifications/pull/128) / [php](https://github.com/green-code-initiative/creedengo-php/pull/10) | - ## Refused / Deleted rules This table lists rules proposed by the community but refused or/and deleted in creedengo plugins with the justification. diff --git a/src/main/rules/GCI535/GCI535.json b/src/main/rules/GCI535/GCI535.json new file mode 100644 index 00000000..0f40db32 --- /dev/null +++ b/src/main/rules/GCI535/GCI535.json @@ -0,0 +1,25 @@ +{ + "title": "Using external libraries for number format should be avoided in favor or Intl.NumberFormat", + "type": "CODE_SMELL", + "code": { + "impacts": { + "RELIABILITY": "MEDIUM" + }, + "attribute": "EFFICIENT" + }, + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "15min" + }, + "tags": [ + "creedengo", + "eco-design", + "performance" + ], + "defaultSeverity": "Major", + "compatibleLanguages": [ + "JAVASCRIPT", + "TYPESCRIPT" + ] +} diff --git a/src/main/rules/GCI535/javascript/GCI535.asciidoc b/src/main/rules/GCI535/javascript/GCI535.asciidoc new file mode 100644 index 00000000..a3496e08 --- /dev/null +++ b/src/main/rules/GCI535/javascript/GCI535.asciidoc @@ -0,0 +1,56 @@ +:!sectids: + +== Why is this an issue? + +Importing an external library for lightweight operations increases overall size of the program. +Using native methods instead reduces the amount of memory and storage to run and store the application. +This is especially critical in environments with limited resources, such as on mobile devices or in web applications +where bandwidth and download times matter. + +Smaller programs generally have better runtime performance. +Reducing the number of unnecessary modules minimizes the amount of code that needs to be interpreted or compiled, +leading to faster execution and improved overall performance. + +== Examples + +*Example with the https://numbrojs.com/[numbro] library, when you use `format` method.* + +[source,js] +---- +// Example with numbro (not compliant) +import numbro from "numbro"; + +numbro.setLanguage('en-GB'); +var string = numbro(1000).format({ + thousandSeparated: true, +}); // '1,000' + +// Example with numerable (not compliant) +import { format } from "numerable"; +format(1000, '0,0'); + +// Example with Intl (compliant) +new Intl.NumberFormat("en-GB").format(1000); // '1,000' +---- + +== Limitations + +As for now, only two libraries are handled by this rule : + +- https://numbrojs.com/[numbro] +- https://numerablejs.com/lander[numerable] + + +Some candidates for the future developments are : + +- https://github.com/Mottie/javascript-number-formatter[javascript-number-formatter] +- https://www.npmjs.com/package/numerable[numeraljs] +- https://formatjs.github.io/[formatjs] + +It’s more likely this rule won’t ever be exhaustive. + +== Resources + +=== Documentation + +- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat[Mozilla Web Technology for Developers]