Improving Code Quality - Session 60: Don't Hammer Down Every Protruding Nail
Key point
Be wary of over-applying the SLAP principle, as it can harm the readability of the main logic.
Details
To improve code readability, functions are often split by applying the SLAP (Single Level of Abstraction Principle). However, when the main logic is extracted into a separate function, it can create a situation where you have to hunt through other functions just to grasp what the function essentially does.
For example, if file compression logic is split into a function like compressFileInternalOrThrow, the caller cannot immediately tell from the function signature alone what steps are actually involved (creating directories, copying files, etc.). This raises the code's level of abstraction too much, resulting in reduced readability.
Therefore, the following 'sub-logic' should be separated into individual functions, while the main logic should keep concrete state.
- Error handling
- Edge cases
- Setup and teardown
- Resource management
- Type conversion of return values
Maintaining consistency in code is a means to improve quality, not an end in itself. Rather than accumulating technical debt through unnecessary abstraction, you should intentionally keep the main logic's level of abstraction low so that the overall flow can be easily understood.
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.