1 min read

Too few or too many functions make dreadful code

A script with too many functions can be just as dreadful as one with too few.

Few functions should be five hundred lines long. Usually it will be clearer if split up.

Split up code too much and it becomes hard to change or reason about. That's especially true if using a lot of custom data bundles. At a certain point there are too many different pieces to keep in mind. These pieces demand to be grouped or connected somehow to make them understandable. I'm thinking mainly of report generation code here.

I have some code that needs to weave together a bunch of structured data into a table form so it can be dumped out to Excel to be looked at. I've got a function for doing this. It was nearly 400 lines long before I started refactoring. I couldn't understand it.

Now that I've split up that long function, it's not clear to me that the splitting made the important bits easier to follow. The original function is more readable, which is great. But the computation logic is now split up over multiple places. It's also more redundant. The worst part though are the names. The names are readable enough, but there are a few ugly constructions in there. I need to compute averages and standard deviations of some averages. How to name the functions and data structures around that? I found names that are technically accurate but they are horribly unwieldy to think about. They make the signatures sloppy and slippery. Dreadful, even.