Skip to content

Conversation

sujalsalekar
Copy link

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:

    • Introduced descriptive named constants for configuration defaults, timeouts, protocol parameters, and other previously hardcoded values.
    • Constants are now defined at the top of relevant files or in configuration sections, making them easy to locate and update.
  • Enums:

    • Where appropriate (e.g., status codes), replaced repeated numeric values with Python Enum or IntEnum classes for better type safety and readability.
  • Code Updates:

    • Replaced all direct usages of magic numbers in logic, class attributes, and function calls with the new named constants or enums.
    • Updated docstrings and inline comments to reference the new constants where relevant.

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

  • No functional changes:
    This is a non-breaking, internal refactor. All existing functionality remains unchanged.
  • Testing:
    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.

Comment on lines +49 to +51
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
Copy link
Member

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.

Comment on lines 238 to 239
if peer_id not in peerstore.peer_ids():
return None
Copy link
Member

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?

Comment on lines 296 to 298
available_protocols = [
p for p in mux.get_protocols() if p is not None
]
Copy link
Member

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
Copy link
Member

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.

@pacrob
Copy link
Member

pacrob commented Jul 21, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants