3

Recently, I've been in a few situations where I needed to quickly come up to speed on old code and fix it sufficiently for people to be able to use it again. The code I've worked on tends to have the usual issues (no documentation, no tests, etc.), but compounding this is that I often have absolutely no idea (on a technical level) what the code does.

For example, I was recently tasked with fixing some software that did something called "stress analysis". Even after extensive googling, I still ended up getting hopelessly lost in the code itself and taking weeks to fix what were probably simple bugs (and I wasn't totally confident about the fixes either).

In situations where you need to fix code which does something you don't understand (and don't have time to learn the material), what are some best practices?


I think this is different from What is the most effective way to add functionality to unfamiliar, structurally unsound code? (which is a duplicate of two others) in that I'm not necessarily looking to improve the code. Typically, I'm trying to do two things:

  1. Run the code. Although it sounds simple, I find it to be one of the most challenging parts, particularly if there's no GUI (or my personal favorite - a GUI which isn't hooked up to the code) or if it requires a file format I'm not familiar with.

  2. Ensure that what I think is a bug is, in fact, a bug. This is most nerve-wracking when I'm looking at "truth tables" in the code or logic flow.

  • 1
    Did you try exploratory testing ? – Spotted May 15 '18 at 7:09
  • 3
  • 2
    @gnat similar, but I was personally looking for some method of figuring out "what the code does" when you're unfamiliar with the entire field. – ace May 15 '18 at 7:21
  • 4
    Read Michael Feathers' book, and accept a job such as this takes as long as it takes. If you are given a dead line to work to, your manager or customer has two choices, live with not changing it, or live with the risk the change is wrong. Legacy code is the 'Elephant in the Room' of software engineering few in the industry are yet admitting to. – mattnz May 15 '18 at 8:50
  • 2
    You cannot test code into correctness, especially if using C/C++, remember unit tests can pass even though a defect relying on UB exists. Simple example is uninitialized variable, under test code, always has the same value but under production code has a different value. I regularly find this kind of problem in code review, even though the unit tests always pass before reviews are submitted. – mattnz May 15 '18 at 9:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.