Why do we feel so uncomfortable with the idea of small functions that have names? What is it about that that frightens us? I’ll tell you what it is.
It’s the fact that long functions are familiar to us in a very deep way.
Take a long function and turn it on its side, and it looks like a landscape. We, humans, evolved to recognize landscapes. We know where we are by recognizing landmarks. That makes us feel comfortable in a very primal kind of way. We’re very good, we humans, at navigating complex terrains by recognizing landmarks.
How do we know our way around inside of a large function?
We recognize the landscape.
We know, for example, that the axes are scaled in the third major indent after the second comment block. We know that the timing factor is adjusted in that little divot of code that follows the big long if statement, and it’s just before that nested try-catch block. We’ve memorized the landscape, and we recognize the landmarks, and that’s a very comfortable feeling to our reptilian hind-brain.
But if you drop someone new into the project, they’re going to look around this landscape of code and recognize nothing. They’ll be completely bewildered. They won’t know where to go or even how to begin. Eventually, they’ll just have to start wandering around aimlessly in the code, in hopes that they can find some safe place to camp before they’re eaten by a sabretooth.
On the other hand, if you drop someone into code that has lots of signposts, well-named functions inside of well-named classes, inside of well-named namespaces, then you’ll find that they’ll be able to navigate their way around without getting eaten by the lurking terror birds. They will establish themselves easily, and then become productive in short order.
(That was a quote from Uncle Bob)