Improving Code Quality - Session 38: Where Is the Master Key?
Key point
This explains how to improve code stability by consolidating related data scattered across multiple Maps or Lists into a single data class.
Details
When multiple Maps (messageTextMap, backgroundColorIntMap, etc.) are used to manage different properties corresponding to the same Key (MessageId), an implicit relationship arises between the data. This happens because the type system cannot guarantee the connection between the data, creating a high risk of bugs when specifications change.
To solve this problem, related properties should be grouped into a single data class (UiAttributes) and managed as a single Map (Map<MessageId, UiAttributes>). This makes the relationship between the data explicit, improving the robustness of the code.
This principle can be applied not only to properties but also to other areas.
- Relationships between Lists: Instead of two lists linked by index, manage them as a single list containing Pair objects.
- Function arguments and return values: Instead of
List<Id>andList<Element>mapped by index, useMap<Id, Element>to clarify the relationship.
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.