← Back to context

Comment by senderista

2 days ago

“Premature abstraction” forced by unit tests can make systems harder to maintain.

It can but more often it’s the opposite.

Code that’s hard to write tests for tends to be code that’s too tightly coupled and lacking proper interface boundaries.

the problem is people make units too small. A unit is not an isolated class or function. (It can be but usually isn't) a unit is one of those boxes you see on those architecture diagrams.

Inability to unit test is usually either a symptom of poor system structure (e.g. components are inappropriately coupled) or an attempt to shoehorn testing into the wrong spot.

If you find yourself trying to test a piece of code and it’s an unreasonable effort, try moving up a level. The “unit” you’re testing might be the wrong granularity. If you can’t test a level up, then it’s probably that your code is bad and you don’t have units. You have a blob.