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 Calisthenics70 XP7 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.

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.

πŸ”§ 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: If you're filtering or sorting a list in more than one place, the list needs to become a class with those behaviors built in.

βœ— Your version

Use First-Class Collections β€” CleanKata β€” CleanKata