8 ms·
What makes unit tested code maintainable has nothing to do with how application code is structured and is entirely about the ability to know in an automated fas
by curun1r 9y ago
What makes unit tested code maintainable has nothing to do with how application code is structured and is entirely about the ability to know in an automated fashion whether the previous expectations for how the code should function still hold after a refactoring. Business realities change over time and there are lessons learned that get woven into the code. If we're not careful, refactoring code can lead to losing those lessons. Encoding those lessons as executable tests allows us to better preserve that knowledge as a piece of code evolves.
Where unit tests have an advantage and where you might have been criticized is their ability to test permutations. If you write three tests for one function and three tests for a dependency of that function, you've essentially covered nine cases with six tests. In a complex application with a large dependency graph, that ability to multiply the value of each test can be difficult to overcome when testing at a higher level. Integration tests rarely cover edge cases. Additionally, unit tests are often quite simple to read and have limited setup which means that a programmer coming to the code base without prior knowledge of the code (I always pictured that being me six months in the future so that I'd be as kind as possible :-) can easily see exactly what is expected of the code.
But we shouldn't be dogmatic about the kind of testing we do and lose sight of the goal of that testing in the first place. Any test that furthers a future programmer's ability to rip the application code apart, put it back together and quickly know whether all the previous expectations of the code are still met is a good test. There's no hard and fast rule and judgment and experience are still necessary to know what to do in a specific situation. Too many people focus on the details of automated testing and lose sight of the goal.
- pqh 9y agoI agree with the latter parts of your comment, but not that "What makes unit tested code maintainable has nothing to do with how application code is structured". My original point was that code that's hard to test is often hard to refactor.
- curun1r 9y ago> My original point was that code that's hard to test is often hard to refactor I agree with that. The poster I was replying to said somewhat the opposite. He said: > Making that code unit testable would have complicated it, thus lowering its maintainability My basic point was that less complicated doesn't mean maintainable. There's no perfect design in the face of future changes in requirements. So the most important quality of the code we write today is the ability to refactor it and know that it still satisfies all the initial requirements that haven't changed. Unit tests give you that property, lack of complexity doesn't. Simple code can be better than complex, but it's a secondary concern to a comprehensive test suite. I'll take a full test suite over code quality any day because it allows me to go in and add the quality later without fear of breaking things. I guess I misspoke when I said "how the application is structured" since it you've interpreted it differently than I meant it. It was a reference to the line I quoted above...that complicating code lowers it's maintainability. I just meant that complexity and tested are separate concerns and that tests more so than simplicity make code maintainable. So I think we agree more than we disagree.
- loup-vaillant 9y agoWhat I said hinged on a very narrow definition of unit testability, one that I believe is quite useless. My code was highly testable, and thoroughly tested. Those may not have been unit tests™ (I hard coded my A->B->C dependency chain instead of injecting it), but I don't care: it works, it's simple, and it's easy to read. > There's no perfect design in the face of future changes in requirements. So the most important quality of the code we write today is the ability to refactor it and[…] I take issue with the idea of such protean requirements. I'm more the YAGNI type: those changes you're trying to anticipate? They likely won't happen. I'd rather keep the code simple, and add the flexibility only when needed. Simple code is easier to modify anyway. (Of course, I'd rather not ossify an architecture if I can avoid it, but an ossified architecture is often the sign of tight couplings, which to me are a form of complexity. If we keep the interfaces between modules small and simple, we are more likely to end up with something flexible.)
- curun1r 9y ago> I hard coded my A->B->C dependency chain instead of injecting it Read my above point about the cartesian product of tests...if you're doing this, you're almost certainly not getting particularly good coverage of edge cases even if your test coverage percentage is high. If I have test A1, A2, A3 that use a mocked B and B1, B2, B3 that use a mocked C and C1, C2, C3, I've essentially tested 27 different cases that you'd have to write in your hard-coded dependency style while writing only 9 tests. When an application reaches even a moderate amount of complexity and that dependency graph grows beyond 3, that ability for unit tests to cover a geometrically-increasing number of ways that the software behaves is going to be hard to overcome with integration tests like you seem to have been writing. But, again, maybe your dependency graph wasn't big enough to need this. Maybe there was some other mitigating factor that made your situation different. I don't want to speculate on any individual situation since I haven't seen the code and there's no universally correct answer. > I'm more the YAGNI type: those changes you're trying to anticipate? They likely won't happen. The one virtually certain thing about the future is that there will be change. Of course you mostly can't predict what will change, but you can be damn near certain that something will. YAGNI is not about predicting that nothing will ever change, it's only about avoiding guessing at what will. Tests on current functionality are not guessing about what will change. The whole point of a test suite is that it avoids ossification. What can be less rigid than something that can easily be pulled apart and put back together in some other form while being sure that it still works? Simple and flexible without that property of safe reorganization is still a landmine waiting to go off in the face of future change.