Skip to main content
Clean Code 50 XP · 5 min

Conventions & Magic Numbers

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

Showing
Ad (728×90)

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.

Done with this lesson?

Mark it complete to earn XP and track your progress.