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.

ace
  • 41
  • 2
  • 1
    Did you try [exploratory testing](https://en.wikipedia.org/wiki/Exploratory_testing) ? – Spotted May 15 '18 at 07:09
  • 1
    @Spotted This has been my main approach (didn't realize it had a name, though - thanks!), but I was hoping for something a little more formulaic. – ace May 15 '18 at 07:15
  • 3
    Possible duplicate of [What is the most effective way to add functionality to unfamiliar, structurally unsound code?](https://softwareengineering.stackexchange.com/questions/135311/what-is-the-most-effective-way-to-add-functionality-to-unfamiliar-structurally) – gnat May 15 '18 at 07:15
  • 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 07: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 08: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 09:03
  • Honestly, I cannot see why your question is different from the linked dupe, the answers to that question and the other dupes seem to fit very well to yours. – Doc Brown May 15 '18 at 12:13
  • The "best practice" for code you cannot understand, or will never have time to understand, is to leave it alone. You are more likely to cause a regression than to improve it. – Frank Hileman May 15 '18 at 23:23

0 Answers0