Skip to content

✨ Clean Code Principles ​

Writing code that humans can read, maintain, and extend.

Core principles ​

  • Readability first β€” Code is read 10x more than it's written
  • Single Responsibility β€” Each function/class does one thing well
  • DRY β€” Don't Repeat Yourself, but don't abstract too early
  • KISS β€” Keep It Simple, Stupid
  • YAGNI β€” You Aren't Gonna Need It β€” don't build for hypothetical futures

Naming conventions ​

  • Variables: describe what it holds (userEmail, not data)
  • Functions: describe what it does (calculateTotal, not process)
  • Booleans: use prefixes (isActive, hasPermission, canEdit)
  • Constants: SCREAMING_SNAKE_CASE for true constants
  • Avoid abbreviations unless universally understood (id, url, api)

Function guidelines ​

  • Keep functions short β€” ideally under 20 lines
  • One level of abstraction per function
  • Max 3 parameters β€” use an object/struct beyond that
  • No side effects unless clearly named (saveUser, sendEmail)
  • Return early to avoid deep nesting

Code smells ​

SmellFix
Long functionExtract smaller functions
Deep nestingReturn early, extract logic
Magic numbersUse named constants
Commented-out codeDelete it (git has history)
God classSplit into focused classes
Duplicate codeExtract shared function

Comments ​

  • Don't comment what β€” the code should say that
  • Comment why β€” explain non-obvious decisions
  • TODO comments need a ticket reference
  • Delete outdated comments immediately

Pergame Knowledge Base