Skip to content

Conversation

Copy link

Copilot AI commented Sep 15, 2025

This PR addresses code maintainability by replacing magic numbers with appropriately named constants across all language implementations in the refactoring kata.

Changes Made

Magic Numbers Replaced

Formatting Constants:

  • 2PRICE_DECIMAL_PLACES for currency formatting precision
  • 0WHOLE_QUANTITY_DECIMAL_PLACES for integer quantity display
  • 3FRACTIONAL_QUANTITY_DECIMAL_PLACES for fractional quantity display

Business Logic Constants:

  • 2TWO_FOR_OFFER_QUANTITY for "two for amount" special offers
  • 3THREE_FOR_TWO_QUANTITY for "three for two" special offers
  • 5FIVE_FOR_OFFER_QUANTITY for "five for amount" special offers
  • 100.0PERCENTAGE_DIVISOR for percentage discount calculations

Layout Constants:

  • 40DEFAULT_COLUMN_WIDTH for receipt formatting
  • 1DEFAULT_ITEM_QUANTITY for standard item quantities
  • 3DISCOUNT_SPACING_OFFSET for receipt layout spacing

Implementation Details

Each language follows its own conventions for constants:

C++: Used constexpr constants in the private section for compile-time evaluation
C: Used #define macros in the header file following C conventions
TypeScript: Used static readonly properties for type-safe class constants
Ruby: Used class constants with ALL_CAPS naming convention
Elixir: Used module attributes (@constant) for immutable compile-time constants

Benefits

  • Improved Readability: Code intent is now self-documenting through meaningful constant names
  • Easier Maintenance: Changes to formatting or business rules only require updating one location
  • Reduced Errors: Eliminates the risk of using incorrect magic numbers in calculations
  • Better Documentation: Constants serve as inline documentation for business rules

Testing

All existing tests continue to pass, confirming that the refactoring maintains backward compatibility:

  • C++ (GTest): ✅
  • C (Catch2): ✅
  • TypeScript (Mocha): ✅
  • Ruby: ✅
  • Elixir: ✅ (syntax validated)

Fixes #3.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Replace magic numbers with named constants Replace magic numbers with named constants across all language implementations Sep 15, 2025
Copilot AI requested a review from nstubbe September 15, 2025 12:20
@nstubbe nstubbe marked this pull request as ready for review October 1, 2025 12:55
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.

Replace magic numbers with named constants

2 participants