4 ms·
Linear code can increase the state that you need to hold in your head while reading through it. You typically can’t just start reading in the middle and underst
by layer8 3y ago
Linear code can increase the state that you need to hold in your head while reading through it. You typically can’t just start reading in the middle and understand what is going on, because you have to trace the evolution of the state up to that point.
Breaking the code up into smaller functions can reduce what you need to keep in your head, if the functions can be understood standalone just by their parameters, and if any side effects they may have on their parameters (in case of mutable objects) are straightforward enough to understand from their naming and/or comments (rather than from their implementation).
One purpose of functions is to separate interface from implementation. If for some part of the code an interface is easier to understand than the implementation, then that’s a clear case for making it a separate function.
The points of separation should therefore be the points where the least context is needed to understand the seperated-out operation.