AI Briefing
KO

Code Quality Improvement - Session 46: Functions Aren't What They Seem

·2025.09.12 11:00

Key point

This proposes a design approach that keeps function names and behavior consistent, in order to prevent bugs caused by mismatches between a function's name and its actual behavior.

Details

In the FooItemRepository class, getItem returns only the cached value, while getItemAsync looks up data via the fooItemStore if the data isn't in the cache. The names of these two functions appear to differ only in sync/async, but there is actually a significant difference in how data is retrieved.

This mismatch between name and behavior can cause a bug where, when a developer swaps in the function for asynchronous handling, data that isn't in the cache fails to be fetched.

To solve this problem, one of the following two approaches should be chosen:

  • Align behavior with the name: Modify getItem to look up data from the store as well as the cache, unifying its behavior with the async version.
  • Make the name clearly reflect the behavior: Use a name like getCachedItem or getOrFetchItemAsync that intuitively conveys the function's actual behavior.

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.