5 ms·
You can't really safely alter legacy code unless it had decent unit tests. Red/Green programming (test drive development) is absolutely essential in cutting dow
by djsumdog 10y ago
You can't really safely alter legacy code unless it had decent unit tests. Red/Green programming (test drive development) is absolutely essential in cutting down on technical debt.
- hvidgaard 10y agoYou can, it just require more work. Once you manage to put some code under a test harness, you can write your own unit tests. If the code is too difficult to follow, then some amount of fuzzing is likely to be needed between the commits, with the old unaltered code as the reference, and any difference in behaviour can be considered a red unittest.
- struppi 10y agoI agree with you, but I would drop the word "unit". When you start to work on legacy code, you often can't write "real unit" tests. The important thing, at this point, is to bring the code under some kind of test harness (Golden Master, executable specs, ...). Later, when you have some test coverage, you can add unit tests. In some cases, you can even try to refactor without tests first, and then add tests once you have a testable API. That's the whole point of the "BabySteps Timer" kata I have linked in the post... Still, in the case I described in the post, a test harness would not have helped us. The only thing that would have helped us would have been to ask one of the few users who needed the features. And we didn't know them...
- ridiculous_fish 10y agoUnit tests don't help you determine whether a given behavior is incidental or necessary. For a lot of code, including the statistics code in the article, correctness is a dynamic property: dependent on its context, environment, clients, etc. So working with legacy code is as much project management and communication as it is engineering best practices. Consider Chrome's recent change to mark sites as insecure if they have a password field without TLS. Will my grandma's bank page be reported as insecure? There's no way to know a priori, or through isolated unit tests. The Chrome team has to go out into the Internet jungle, reaching out to the site owners and pushing until the major sites are ready, and any holdouts can be considered acceptable casualties. With that understanding, whether code is "legacy" isn't about age or budget or level of support; it's about how much influence you have over your clients. Some code is never used and so never becomes legacy, no matter how old it is. But other code is born legacy: it gets clients immediately and pays the price for being popular.
- dagw 10y agoAnother problem with fixing something like that statistics code is that it makes it to impossible compare to new results to old results. I have a piece of old code like that myself kicking around that tends to overestimate a certain value. I also have some new code that uses a better model and gives a more accurate value. The problem is I cannot really roll out the new code since it will make all the new things look worse than all the old things, even if they're actually the same or perhaps even better. So I use the old code, because even if it's wrong, at least it's wrong in that same way as all the other results and people can make relevant comparisons.
- josteink 10y ago> Unit tests don't help you determine whether a given behavior is incidental or necessary. That's his point. If you have a test-case for the behaviour, you know that this is how it should actually behave. Anything not covered in tests however, that will naturally be covered in uncertainty for any weirdness, quirks or odd behaviour. Should it be there or not? Hard to tell when it's not formalized in a unit-test.
- flukus 10y agoAssuming they were testing behavior and not shooting for some coverage metric. I come across plenty of bugs with tests against them. I have a rather strict rule personally, every test should start with a line that says what it's testing.
- jcrben 10y agoI commonly hear this, and yet it is so wrong. I don't understand how this idea that unit tests significantly protect against regressions has spread so far. Classic texts such as "Growing Object-oriented software with Tests" note, rightly, that end-to-end tests are for external reliability. Unit tests, which don't operate on that abstract high level, give you relatively little safety. Sometimes they can ensure that an algorithm is not broken, but algorithms is usually not where the legacy code has issues. Often unit tests are at the level of "assert that X function calls Y stub Z times".
- atilaneves 10y ago> "assert that X function calls Y stub Z times" That's a terrible test that should never pass code review. It's worse than no test at all - it's brittle and guarantees nothing. Writing good tests is _hard_.
- struppi 10y agoI totally agree with you. In the end you need different kinds of tests (end-to-end, unit, ...). And with legacy code, you probably want to start with end-to-end tests or integrated tests. But in that specific case, no amount of tests would have helped us...