Skip to main content
Object Calisthenics 70 XP · 7 min

Use First-Class Collections

Any class containing a collection should have no other member variables — wrapping collections gives filtering, sorting, and rules a semantic and cohesive home.

Showing
Ad (728×90)

Why this matters

When a plain list is passed around, every caller invents its own way to filter, sort, or query it. The same for user in users if user.is_admin loop ends up in five different files. Wrapping the collection in a class with named methods means that logic exists exactly once — and the name makes the intent obvious.

Code Challenge

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

Key takeaway

Any time you pass a raw list or array, ask: does this collection have behaviour that's repeated in more than one place? If yes, it deserves its own class with a meaningful name and focused methods.

Done with this lesson?

Mark it complete to earn XP and track your progress.