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.

Object Calisthenics60 XP6 min

Don't Use the Else Keyword

Replace if/else with guard clauses and early returns — else is usually a sign of unclear flow that can be simplified into a straightforward sequence.

Why this matters

Every else branch creates a parallel path through your code. The reader must track both the "normal" and "alternative" flows simultaneously. Guard clauses and early returns collapse these parallel paths: handle the edge case first, then write the happy path straight down the page — no branching required.

Code Challenge

Study the messy code, try to refactor it, then reveal the clean version.

💡Key takeaway

Return or throw early for every special case, then write the normal path without any branches. Your readers will thank you — they only have to follow one path at a time.

🔧 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: Every else adds a branch to hold in your head simultaneously. Guard clauses let you forget each case once it's handled.

✗ Your version