Skip to main content

Sign in to CleanKata

Track your progress, earn XP, and unlock every lesson.

By signing in you agree to our Terms of Use and Privacy Policy.

Clean Code50 XP5 min

Conventions & Magic Numbers

Follow team coding standards religiously and replace every magic literal with a named constant.

Coding Conventions

G24: Teams that follow coding standards consistently produce codebases where any developer can read any file with minimal friction. Standards cover naming (camelCase, SCREAMING_SNAKE), spacing, brace placement, and file organization. The specific rules matter less than applying them without exception. Inconsistency forces every reader to context-switch as they navigate the file.

Eliminating Magic Numbers

G25: A magic number is a literal value in code whose meaning is not self-evident. 86400 is a magic number. SECONDS_PER_DAY = 86_400 is not. Named constants serve three purposes: they document intent, they make global search-and-replace safe, and they centralize the value so it's changed in one place. This applies to strings too — "application/json" should be CONTENT_TYPE_JSON.

Code Challenge

Replace all magic numbers with named constants.

💡Key takeaway

Magic numbers are as bad as magic spells — nobody knows what they do or where they came from. Name them.

🔧 Some exercises may still have errors. If something seems wrong, use the Feedback button (bottom-right of the page) to report it — it helps us fix it fast.

Hint: G24: Pick a convention and apply it without exception. G25: Name every number — 86400 is SECONDS_PER_DAY.

✗ Your version

Conventions & Magic Numbers — CleanKata — CleanKata