AI Briefing
KO

Code Quality Improvement - Session 65: Collection Isn't Just List

·2026.02.06 11:00

Key point

Considering the characteristics of Collections and appropriately using Set and Map instead of List can improve code clarity and stability.

Details

When implementing a bulkQuery() function that retrieves large amounts of user data in bulk, code quality varies significantly depending on how the argument and return value types are set. The existing approach of taking List<UserCategory> as an argument and returning List<Set<User>> has an implicit relationship, requiring results and categories to be matched via index.

This structure is hard to understand because it requires matching results using zip() and similar methods, making it a fragile structure where relationships can easily break. To improve this, the following changes are recommended.

  • Change argument type: Change List<UserCategory> to Set<UserCategory> to prevent passing duplicate categories and to explicitly indicate that duplicates don't matter.
  • Change return type: Change List<Set<User>> to Map<UserCategory, List<User>>. This explicitly expresses the relationship between categories and user data, improving the code's readability and reusability.

Additionally, when the order of data matters, it's better to use List instead of Set. In particular, performing sorting at the database layer has performance benefits, and adding a comment about the sort order can further improve code readability.

This summary was generated automatically by AI. Check the original for the author's claims and context. Copyright belongs to the original author.

Our guide explains how the AI works. Report summary errors, attribution issues, or removal requests via Contact.