Skip to main content
Clean Code 60 XP · 6 min

Placement & Statics

Put every function in the class most interested in it, and keep static methods truly stateless utilities.

Showing
Ad (728×90)

Misplaced Responsibility

G17: Every piece of code should live where a reader would naturally look for it. When you write a method on Order that only reads Inventory data, you've misplaced it. The reader looking at Inventory won't find a method that belongs there. The method has a natural home — put it there. The rule of thumb: a method belongs in the class whose data it uses most.

Inappropriate Static Methods

G18: Static methods are fine for pure utilities that have no need for polymorphism — Math.round, String.format. They become a problem when the method should be polymorphic. If you have Order.calculateFee(order) as a static and tomorrow you need RushOrder to calculate fees differently, you're stuck. Instance methods support overriding; static methods do not. Reserve statics for operations that will never need specialization.

Code Challenge

Move the reorder_needed method to the Inventory class where it belongs.

Key takeaway

Put code where a reader would naturally look for it — in the class most interested in the data it uses.

Done with this lesson?

Mark it complete to earn XP and track your progress.