4 ms·
Unit tests are absolutely fantastic during refactoring. I once had to rewrite a piece of code where nobody really knew what it did or what it had to do, and the
by mcv 6y ago
Unit tests are absolutely fantastic during refactoring. I once had to rewrite a piece of code where nobody really knew what it did or what it had to do, and the original author just made some guesses about the intention.
I started out by writing unit tests for everything, which became my handhold and documentation for what the system originally did. Then I started reorganising the code into a more structured and more readable form, without changing the functionality, as proven by my unit tests. Then I started asking domain experts what exactly it should do, unit tests in hand, asking if these answers were correct. If they weren't, I changed the unit test, and then changed the code to match.
Surprisingly painless for something that by all reasonable standards was a terrible mess.
- theshrike79 6y agoYou invented the same refactoring technique Michael Feathers suggests in his book[1]. You write tests to document the current state of the legacy software and then start slowly changing it. Great book BTW, should be on a top10 must read list for software developers. (#1 will always be Peopleware[2]) [1] https://www.goodreads.com/book/show/44919.Working_Effectively_with_Legacy_Code https://www.goodreads.com/book/show/44919.Working_Effectivel... [2] https://www.goodreads.com/book/show/67825.Peopleware https://www.goodreads.com/book/show/67825.Peopleware
- Aeolun 6y agoPeopleware is great, but seeing that it was written more than 30 years ago, also completely pointless. All the advice that book gives is ignored by literally every leader I’ve ever met. Our CIO was regaling us with the story about how they were going to change our office today, make it more open, fancy like Google or Facebook, free desk. But when I asked if they’d considered dealing with the noise issues we were having, no, no they hadn’t considered that. It just blows my mind. I just really wanted to ask them what the hell they thought they were doing modifying the office layout without asking the people who need to work there.
- antris 6y agoI would argue that having a robust type system and some (not too many) end-to-end tests for your software makes unit testing almost completely useless overhead.
- smabie 6y agoConventional type systems don't really help you when your code is pretty much just taking in vectors/matrices of floats and returning vectors/matrices of floats.
- antris 6y agoYou are correct, however that's a niche use case that would warrant using a non-conventional type system. Conventional type systems are mostly for just dumping a bunch of strings and integers to and from a database and formatting them neatly.
- MaxBarraclough 6y agoYou still have the option of introducing new types, even there. In Ada, the programmer is discouraged from using the Ada equivalent of int directly, and is encouraged to instead introduce a subtype that reflects the specific use of int (including automatic range checking). This isn't as natural in C++ but is still possible. Boost offers a BOOST_STRONG_TYPEDEF [0] to deliberately introduce an incompatible type. (I do recall having trouble getting it to behave, but it's been a while.) Whether this makes sense in most mathematical code, I'm not sure, but it seems like it's an option. [0] https://www.boost.org/doc/libs/1_73_0/boost/serialization/strong_typedef.hpp https://www.boost.org/doc/libs/1_73_0/boost/serialization/st...
- jeffreygoesto 6y agoIf you are working with physicial quantities in C++ there are also [0] https://github.com/nholthaus/units https://github.com/nholthaus/units [1] https://github.com/mpusz/units https://github.com/mpusz/units
- kortilla 6y ago
- 1337shadow 6y ago"Fantastic" or "The only way" ? has anyone seen a case of refactoring without test end well ? I haven't. Seems like that "refactoring untested code" would be a well established recipe for disaster.
- MaxBarraclough 6y ago> "Fantastic" or "The only way"? Not sure if it counts as a 'way', but a static type system helps too.
- thom 6y agoI'm intrigued what level you write these tests at, because one of the most painful things to me is having to do large refactorings in a codebase with lots of little niggly unit tests, but few integration or functional tests. During a refactoring, you're constantly breaking impelementation details, and many people feel compelled to have test coverage of these details. My favourite systems are ones where use cases are explicitly modeled, or at least you have a well-defined (and tested) service layer. This is often something you'll introduce first on top of a legacy system. In systems built this way, it's far easier modify the guts, knowing you're not breaking anything with business value, but without the overhead of small unit tests that are highly coupled to implementation details.