You Knew What To Do

So just a blurb of a thought today really regarding crazy methods I find in day to day stuff. It’s a smell of single responsibility, but a few more nuanced smells that create an intoxicating bouquet. It’s that of amnesia driven development.

It looks like this

Callsite: small. You just chuck stuff in this sweet function and it works. It looks like code dedupe.

Method Signature: boolean traps, variadics, and generally greater than 3 arguments, loosely typed. It could be *anything* you’re getting.

Method Body: high cyclomatic complexity, tons of mutation of arguments based on their combinations, over 100 lines, a lot of guess work. I mean mostly, if not all ask don’t tell. So much guess work.

Problems: low testability. Are you really going to figure out all the combinations of input correctly? Are you even going to test it? Wide bounds. You can’t refactor this baby so easily. It’s bound to x amount of variable behavior that you can’t abstract any further. There’s no abstraction here even though it seemed like it. It’s just as wide of a berth and coupled to everything in your app as anything else but now you’ve got a whole trail of dependents that need it in different ways with no possible single substitution.

Refactoring: start removing chunks out of the body into smaller functions in the same class but write that horrible test first. Lastly replace the method at the call sites with smaller methods using replace parameter with explicit method.

The caller might get a bit more code hoisted up into it, but you can accommodate your one offs there because you know what they are. The whole point of this blurb is that your caller actually knows what they want to do, but it’s been mediated that with this catch all thing that doesn’t know what they want to do and has to figure it out all over again. That’s a sneaky form of duplication right there. You forgot everything, had to figure it out all over again, and saved a line or two of code. I’m just as guilty of this as the next dev, so hopefully this thought will save you some pain.