5 ms·
The biggest benefit of unit test isn't catching bugs, but making aggressive refactorings possible.
by BurningFrog 10y ago
The biggest benefit of unit test isn't catching bugs, but making aggressive refactorings possible.
- chrismorgan 10y agoHuh. Here was me thinking that was what static type systems were about. ;-)
- daveguy 10y agostatic type systems != unit tests
- BMorearty 10y agoStatic typing does not allow you to refactor code with confidence that you didn't break the application logic. Only automated tests can do that, regardless of type system.
- chrismorgan 10y agoSure. But an awful lot of failures in refactoring in dynamically typed languages would be avoided by a static type system. As it happens, I am a Rust fanboy with all that entails, so I (like everyone else in such discussions) am clearly biased. My point was in jest, as the “;-)” (because HN simply discarded a U+1F609) indicated.
- crdoconnor 10y agoThat's what strong type systems are about. A strong explicit dynamic type system with appropriate tests can track down bugs more easily than a statically typed weak type system.
- goldenkey 10y agoOne could also say that bad unit tests make aggressive refactoring impossible. Both sides of the coin don't necessarily make for a persuasive argument.
- xjia 10y agounits are likely to be aggressively refactored, no?
- vigilant 10y agoMy experience is that more often than not, unit tests get thrown away in aggressive refactorings, and system level tests survive. If you have a system that depends on units A, B and C, and refactor it to use a totally different structure divided into units D, E and F, then the unit tests for A B and C get thrown away, as they don't really have an equivalent in D, E and F. But the system level test stays, as the system as a whole does the same thing. So, the system level test is what guarantees the safety of the refactor. That is not always the case, but that happens in most of the large refactorings i've been involved in.
- bluejekyll 10y agoThis is true of any code you throw out. If you throw out the code, you should definitely throw out the corresponding tests. It doesn't invalidate the fact that unit tests increase your confidence in your own code.
- ryanbrunner 10y agoThe point is, as your tests get more and more fine grained, and the units themselves get more and more fine grained (in my experience these go hand-in-hand with a TDD approach), the changes that the units themselves maintain intact approaches zero, because otherwise the only refactorings possible are completely trivial.
- kmiroslav 10y agoYou can achieve this with non unit tests as well.
- BurningFrog 10y agoSure. I should have just said "test".
- K0nserv 10y agoUnit tests can also prevent aggressive refactoring by being heavily coupled to the implementation due to the nature of mocking essentially private details of the code under tests. You then have to aggressively refactor your tests along with your code and all the safety that the test would give is gone.