Skip to content

GCI82 : make variable constant and update documentation#437

Open
lambrecn wants to merge 4 commits into
green-code-initiative:mainfrom
snail-unamur:GCI82-NL
Open

GCI82 : make variable constant and update documentation#437
lambrecn wants to merge 4 commits into
green-code-initiative:mainfrom
snail-unamur:GCI82-NL

Conversation

@lambrecn

Copy link
Copy Markdown

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

@dedece35

Copy link
Copy Markdown
Member

Hi @lambrecn,
sorry but I don't see python implementation and I can't accept this PR.
If you want to make the new implementation in Python plugin, no problem, but some modifications are wrong in the current PR. For example, the icon "done" is'nt ok, and you should put "in progress" icon instead.

@lambrecn

lambrecn commented Apr 2, 2026

Copy link
Copy Markdown
Author

Hello @dedece35 , the main modification has been done !

Comment thread CHANGELOG.md

### Changed

Implement GCI82 rule for Python to detect variables that are never reassigned and can be converted to constants.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread CHANGELOG.md

### Added

- Add rule GCI24 (Python): Avoid retrieving unlimited SQL rows

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you add this new "added" section for GCI24 ? your PR is dealing with GCI82 only.

Comment thread CHANGELOG.md

### 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).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you add this new "changed" section for GCI74 ? your PR is dealing with GCI82 only.

@dedece35

dedece35 commented Jun 16, 2026

Copy link
Copy Markdown
Member

Hi @lambrecn,
after a quick check with Copilot ... for me, there is a very low enironnemental impact.

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:
public final String CONFIG_PATH = "/config";

Compile-time constant folding: The Java compiler replaces all references to CONFIG_PATH with the literal value "/config" directly in the bytecode
No runtime lookup: The JVM doesn't need to look up the variable during execution
Constant optimization: The compiler can perform dead code elimination and other optimizations
Result: Measurable reduction in CPU cycles during execution -> Lower energy consumption
PYTHON - NEGLIGIBLE ENVIRONMENTAL IMPACT

Code example:
CONFIG_PATH = "/config" # Runtime variable lookup required

No compilation step: Python (CPython, the standard implementation) interprets code line-by-line at runtime
Runtime variable resolution: Even with typing.Final, Python still performs a namespace lookup every time the variable is accessed
No compiler optimization: There is no compilation phase where constant folding or dead code elimination can occur
Result: Identical CPU cost compared to a regular variable
TECHNICAL COMPARISON

Aspect: Execution Model
Java: Compiled to bytecode, JIT optimized
Python (CPython): Line-by-line interpretation

Aspect: Constants Optimization
Java: Inlined at compile time
Python (CPython): Looked up in namespace at runtime

Aspect: CPU Cycles Saved
Java: Measurable (constant folding)
Python (CPython): None (identical lookup cost)

Aspect: Environmental Gain
Java: Real and quantifiable
Python (CPython): Negligible/None

Aspect: typing.Final Support
Java: N/A (uses final keyword)
Python (CPython): Type-checker hint only (CPython ignores it)

WHY TYPING.FINAL DOESN'T HELP ENVIRONMENTALLY

Code example:
from typing import Final

MAX_SIZE: Final[int] = 1000

CPython still performs a namespace lookup:
if len(items) > MAX_SIZE: # <- Lookup occurs here
pass

Benefits:

Helps developers: Type-checkers (mypy, Pylance) warn about reassignment attempts
Improves code intent: Signals to other developers that the variable is immutable
Drawbacks:

No runtime optimization: CPython completely ignores the Final annotation
Same environmental footprint: Identical variable lookup cost as a regular variable
ALTERNATIVE PYTHON IMPLEMENTATIONS

While CPython is the standard (99% of deployments), other implementations exist:

PyPy (JIT-compiled): Could potentially optimize constants better, but not guaranteed
Jython (runs on JVM): Could theoretically benefit from Java optimizations
IronPython (runs on .NET): Similar considerations
However, CPython dominance means the rule recommendation should focus on the most common case.

RECOMMENDATIONS

CURRENT STATUS

Java: Keep as "implemented" (real environmental benefit)
Python: Should be changed to "Rule to analyze for applicability"
RATIONALE

No environmental benefit: GCI82's core premise (compile-time optimization) doesn't apply to Python
Only maintainability benefit: The rule provides code clarity and intent signaling, not eco-performance
Consistency: Other interpreted languages (JavaScript) should also be reconsidered
SUGGESTED ACTION

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
PEP 8 - Python Style Guide (Constants Convention)
PEP 591 - Adding a Final Qualifier to Typing
CPython Interpreter Architecture

@dedece35 dedece35 self-assigned this Jun 16, 2026
@dedece35 dedece35 moved this from Backlog to In Progress in Hackathon Kanban - RULES-SPECIFICATIONS Jun 16, 2026
@dedece35 dedece35 moved this from In Progress to Blocked - Need Support in Hackathon Kanban - RULES-SPECIFICATIONS Jun 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Blocked - Need Support

Development

Successfully merging this pull request may close these issues.

2 participants