-
Notifications
You must be signed in to change notification settings - Fork 183
Refactor: Replace magic numbers with named constants and enums for clarity and maintainability #783
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Refactor: Replace magic numbers with named constants and enums for clarity and maintainability #783
Conversation
…arity and maintainability
reservation_ttl: int = DEFAULT_RESERVATION_TTL # seconds | ||
max_circuit_duration: int = DEFAULT_MAX_CIRCUIT_DURATION # seconds | ||
max_circuit_bytes: int = DEFAULT_MAX_CIRCUIT_BYTES # 1GB |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since the explanatory comments are next to where the constants are defined, the can be removed here.
if peer_id not in peerstore.peer_ids(): | ||
return None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a functional change that doesn't appear related to constants. How did it come up?
available_protocols = [ | ||
p for p in mux.get_protocols() if p is not None | ||
] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, functional change, however small. They're fine if they improve code, but should be noted in the PR.
# - Hash everything for a fixed size output | ||
random_bytes = os.urandom(16) # 128 bits of randomness | ||
timestamp = str(int(self.created_at * 1000000)).encode() | ||
random_bytes = os.urandom(RANDOM_BYTES_LENGTH) # 128 bits of randomness |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
don't need inline comment here and where defined.
Needs linting and newsfragments. The PR mentions using Enum and IntEnum, but I only see named constants. Or is there more coming? Apologies if this still WIP. |
Refactor: Replace Magic Numbers with Named Constants and Enums
Overview
This pull request refactors multiple modules in the codebase to eliminate the use of magic numbers and hardcoded values. Instead, all such values are now defined as named constants or enums. This change is aimed at improving code clarity, maintainability, and adherence to best practices.
Details
What Was Changed
Named Constants:
Enums:
Enum
orIntEnum
classes for better type safety and readability.Code Updates:
Rationale
Clarity:
Named constants and enums make the code self-documenting, so the purpose of each value is immediately clear to future maintainers and reviewers.
Maintainability:
Centralizing values in constants or enums makes it easier to update configuration or protocol parameters in one place, reducing the risk of inconsistencies or missed updates.
Best Practices:
This refactor aligns the codebase with Python and general software engineering best practices, reducing technical debt and improving code quality.
Safety:
Using enums for status codes and similar repeated values helps prevent bugs caused by incorrect or inconsistent usage of numeric literals.
Impact
This is a non-breaking, internal refactor. All existing functionality remains unchanged.
All existing tests should continue to pass. No new tests are required, but this refactor lays the groundwork for easier future testing and configuration changes.