Give Business Limits a Clear Constant Name
Scenario
The library decides to raise its borrowing limit from 3 books to 4 books.
Try it yourself
Compare raw magic numbers against a named constant when a rule changes.
| Case | Result |
|---|---|
| Code repeats raw "3" in checker, message, and tests | Select a choice above to see what happens. |
| LIMIT = 3 (no unit or meaning) | Select a choice above to see what happens. |
| MAX_BORROWED_BOOKS = 3 -> borrowedCount < MAX_BORROWED_BOOKS | Select a choice above to see what happens. |
Reveal the explanation
- Code repeats raw "3" in checker, message, and tests
Fragile. Updating one "3" to "4" while forgetting another creates contradictory behavior.
- LIMIT = 3 (no unit or meaning)
Still unclear whether 3 means books, days, or rooms.
- MAX_BORROWED_BOOKS = 3 -> borrowedCount < MAX_BORROWED_BOOKS
Self-documenting and centralized: one update keeps rule and checks aligned.
A clear constant name states both what is being limited and its unit (for example PASSING_SCORE = 60 or MAX_ROOM_CAPACITY = 12).
Your turn
Replace the magic numbers in "score >= 60 and score <= 100" with two well-named constants.
Common mistake Naming constants NUM_1 and NUM_2 instead of describing the rule.
Check your work
PASSING_SCORE = 60 and MAX_QUIZ_SCORE = 100; score >= PASSING_SCORE and score <= MAX_QUIZ_SCORE.
Quick check
This browser could not save your progress. Changes last until you leave the page.