Consistency in code reduces cognitive overhead. When we read code, we process it through some sort of “expectation” mental model — you have expectations about what you’ll see and what it means. If these expectations match reality, the cognitive overhead of reading code is low. However if the expectations are frequently betrayed, you expend energy on updating your mental model, or worse, give up on the model because all code is so ad hoc and different that it all needs to be read with full attention, no expectations can be trusted. This can be exhausting.
Consistency applies to many aspects of programming: naming, formatting, design patterns, architecture, idioms.
A common way to break consistency is to find a new and better way of doing something. Even if you find one, consider using the old boring way to reduce the mental overhead of the reader — the gain in having tried a new pattern is unlikely to outweigh the mental overhead caused by introducing the inconsistency. Alternatively, consider changes all instances of the old to the new and better way, having of course, considered the retest overhead (see be aware of context). Mixing multiple patterns that achieve the same thing reduces consistency and decreases readability.
Comments
Post a Comment