3 ms·
> Documenting code via single-caller functions is usually a mistake, because to everyone else who looks, the set of functions is an API. That's less the case i
by dragonwriter 7y ago
> Documenting code via single-caller functions is usually a mistake, because to everyone else who looks, the set of functions is an API.
That's less the case if the functions are local (either not exported from the module they are included in or actually function-local, in languages supporting nested functions, to their caller.
> Both internal and external APIs must be kept coherent.
If it's not external, it's not an application programming interface. And, even ignoring that, there is no definition of coherent for which that approximates truth that is inconsistent with single use functions for code clarity and organization.
Even languages that don't have mechanisms to make methods/functions truly local/private tend to have conventions for indicating functions/methods not part of the intended-as-public API of a class or module.
> Also when readers are trying to understand exactly how some function changes the system state, having to refer to numerous other functions it calls is tedious.
Conversely, I find the code having functions with descriptive names helps me not have to read through a bunch of irrelevant code when I'm doing that, which helps me get to the part I need to understand whatever I'm trying to understand faster and with less distraction. Yes, if it's done badly it's problematic, but that's true of literally everything.
EDIT:
> Also when readers are trying to understand exactly how some function changes the system state
Yes, if you are doing relatively unconstrained imperative code that's willy-nilly modifying state, breaking that up into functions passing mutable state around is likely to make it even more incomprehensible. Decomposing into units that are externally-pure functions (that is, while they may do local mutations, they don't modify any state received from the caller), though, is not problematic. In general, except for subsystems dedicated to managing mutable state (and where this function is kept as constrained as possible), I prefer coding in externally-pure functions in general. When you start with that and use it as a constraint on decomposition, decomposition can no longer serve to obscure state modifications. Indeed, it clarifies and more narrowly isolates them.
- networkimprov 7y ago> If it's not external, it's not an application programming interface Splitting hairs, I think. Even if not intended, someone else may assume your "documentation" function was meant to be generally accessible (i.e. an API), and call it that way.
- dragonwriter 7y ago> Even if not intended, someone else may assume your "documentation" function was meant to be generally accessible (i.e. an API), and call it that way. And then, suddenly, instead of a single use function that serves mainly for documentation and code organization, it js a locally resuable and locally reused piece of functionality. Which is a problem, ...why, exactly? The only difference between a well-designed single-user function and a well-designed locally reused function is that the single, clear, and unit tested purpose of the single-use function isn't needed in more than one place when it is written. If that changes, why shouldn't it be reused?
- networkimprov 7y agoThat's what I'd call accidental API design. It might work, or it might lead to perdition :-) Refactor with a new internal and coherent API once it's clear that's needed.