GCI82 : make variable constant and update documentation#437
Conversation
|
Hi @lambrecn, |
|
Hello @dedece35 , the main modification has been done ! |
|
|
||
| ### Changed | ||
|
|
||
| Implement GCI82 rule for Python to detect variables that are never reassigned and can be converted to constants. |
There was a problem hiding this comment.
I'm ok with "added" part but not with this "changed" part. Why do you add some data in this "changed" part ?
For me, you don't need anythong in this "changed" part.
|
|
||
| ### Added | ||
|
|
||
| - Add rule GCI24 (Python): Avoid retrieving unlimited SQL rows |
There was a problem hiding this comment.
why do you add this new "added" section for GCI24 ? your PR is dealing with GCI82 only.
|
|
||
| ### Changed | ||
|
|
||
| Upgrade GCI74 rule to use an abstract implementation, shared with the GCI24 rule, to avoid code duplication and to make it easier to maintain in the future. This abstract implementation can be used for other rules that have the same logic (pattern verification). |
There was a problem hiding this comment.
why do you add this new "changed" section for GCI74 ? your PR is dealing with GCI82 only.
|
Hi @lambrecn, Here is a extract : GCI82: LIMITED ENVIRONMENTAL IMPACT IN PYTHON CONTEXT GCI82 ("Make variable constant") has a significant environmental benefit in Java but a negligible environmental benefit in Python. This analysis explains the fundamental differences and recommends adjusting the rule status for Python. THE CORE DIFFERENCE: COMPILED VS INTERPRETED LANGUAGES JAVA - STRONG ENVIRONMENTAL IMPACT Code example: Compile-time constant folding: The Java compiler replaces all references to CONFIG_PATH with the literal value "/config" directly in the bytecode Code example: No compilation step: Python (CPython, the standard implementation) interprets code line-by-line at runtime Aspect: Execution Model Aspect: Constants Optimization Aspect: CPU Cycles Saved Aspect: Environmental Gain Aspect: typing.Final Support WHY TYPING.FINAL DOESN'T HELP ENVIRONMENTALLY Code example: MAX_SIZE: Final[int] = 1000 CPython still performs a namespace lookup: Benefits: Helps developers: Type-checkers (mypy, Pylance) warn about reassignment attempts No runtime optimization: CPython completely ignores the Final annotation While CPython is the standard (99% of deployments), other implementations exist: PyPy (JIT-compiled): Could potentially optimize constants better, but not guaranteed RECOMMENDATIONS CURRENT STATUS Java: Keep as "implemented" (real environmental benefit) No environmental benefit: GCI82's core premise (compile-time optimization) doesn't apply to Python If the rule is kept for Python, add a language-specific note clarifying: "Note: Unlike compiled languages (Java, C#), Python does not provide environmental benefits from using constants. This rule for Python should focus on code maintainability and developer intent rather than performance optimization." REFERENCES Java Language Specification - Constant Folding |
The implementation of the GCI82 rule for python is implemented and the pull request will follow.
Modification of constant_severity and effort due to issues