Remove magic numbers and strings from receipt printer implementations #24
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
This PR addresses the issue of magic numbers and magic strings throughout the codebase by extracting them into well-named constants. This improves code maintainability and makes it easier to modify formatting behavior in the future.
Changes
Refactored receipt printer implementations across 11 programming languages to eliminate magic values:
Magic Numbers Replaced
40→DEFAULT_COLUMNS- Default receipt column width1→QUANTITY_THRESHOLD- Threshold for displaying unit price breakdown2→PRICE_DECIMAL_PLACES- Decimal places for price formatting3→WEIGHT_DECIMAL_PLACES- Decimal places for weight quantities3→DISCOUNT_LINE_OFFSET- Column offset in discount line formatting2,3,5) in C implementation100.0→PERCENT_DIVISORin C discount calculationsMagic Strings Replaced
"%.2f","N2", etc.) →PRICE_FORMAT"%.3f","N3", etc.) →WEIGHT_FORMAT"Total: "→TOTAL_LABEL" "," ","\n") → Named constants"(",")","-"," * ") → Named constantsBug Fix
Fixed a bug in Ruby, PHP, Kotlin, and Swift implementations where integer quantities were being formatted with
%x(hexadecimal) instead of%d(decimal). For example, a quantity of 10 would display asainstead of10.Languages Updated
Testing
All existing tests continue to pass:
pytesttests passingmochatests passinggo testpassingImpact
Example
Before:
After:
Original prompt
Fixes #23
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.