Improving Code Quality - Session 40: Is It Better to Split a Function into Two?
Key point
To resolve the 'split knowledge' problem caused by a function depending on a specific data format, this proposes a refactoring approach that moves domain knowledge to the call site.
Details
When implementing a utility object that handles multiple versions of Regex for string parsing, a problem arises when a helper function embeds knowledge of a specific data format. For example, if an internal function is designed to expect the index or data type of a specific Regex group, knowledge of the data format becomes split across the function and its call sites.
To resolve this Split Knowledge problem, the helper function should be refactored so that it no longer depends on domain knowledge. The key is for the helper function to simply return the group values resulting from the Regex match, while moving the actual logic for parsing the data to the call site.
Since a function refactored this way is no longer tied to a specific data format, it can be reused in logic that handles other data types (e.g., BarEntry), improving the code's versatility and maintainability.
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.