AI Briefing
KO

Improving Code Quality - Session 50: Single Source of Truth

·2025.10.24 11:00

Key point

It presents a design method to prevent invalid state creation caused by value duplication when applying aggregation or composition after separating data models.

Details

If attributes with different lifecycles or different scopes of use are mixed within a data model, separating them can increase the robustness of the code. For example, this involves splitting frequently changing onlineStatus and rarely changing accountType into separate models to clarify the direction of dependency.

However, there is a point to be careful about when applying Aggregation or Composition. If the upper model UserState includes the lower model AccountModel, and both models have the same userId, there is a risk that objects with different IDs could be combined, creating an Invalid state.

To solve this problem, Value duplication must be avoided. Data consistency should be ensured by either deleting userId from the upper model entirely, or designing it to reference the userId of the lower model.

Also, a Factory function can be used to fundamentally block inconsistencies between attributes. Using a method that verifies whether the IDs of the two models match before creating an instance can prevent the creation of invalid combinations of values.

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.