Skip to main content
Object Calisthenics 60 XP · 6 min

One Level of Indentation per Method

Each method should do one thing at one level of abstraction — nested control structures are a sign to extract a method.

Showing
Ad (728×90)

Why this matters

Every extra level of nesting is a cognitive tax. When a method has a loop inside an if inside another loop, you have to hold three contexts in your head at once just to understand the innermost line. Flatten the nesting and you turn one complex function into several simple, independently testable pieces.

Code Challenge

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

Key takeaway

If you need to indent a second time inside a method, stop and extract a helper. The helper name becomes free documentation, and each piece is testable on its own.

Done with this lesson?

Mark it complete to earn XP and track your progress.