Ask HN: What are your best tips for deciphering code in an inherited project?
I've recently been tasked with restoring an old software toolkit to working order.
Unfortunately, the original project maintainer is no longer available, and the code is about as tangled as a plate of spaghetti. There are lots of outdated dependencies, and documentation is (of course) practically non-existant. We've all been there, right?
SO: What are your best suggestions for auditing/editing an inherited project, especially one that seems to fight you the whole way?
5 comments
[ 3.1 ms ] story [ 16.9 ms ] threadI started by breaking down the code into a few major sections and isolating them into their own modules. This helped me focus on one section of the code at a time without having to understand the whole thing. Then I made as much of the code as possible private. As long as the interfaces were clean, I could fix up the internals at my leisure - or hand them out for others to fix. Then I tried to get it to compile. Then whenever I ran into a runtime problem, I was cleanup that section of code while I fixed the bug.
tl;dr - Divide and conquer. Julius Caesar for the win!
Did you find it difficult to decipher the other programmer's 'style'? The project I'm working on, in particular, has a number of crazy quirks and hacks to get it to work.
http://www.amazon.com/Working-Effectively-Legacy-Michael-Fea...
Run some code formatter over the source if you get angry about indentation or other stuff that works you up.
Don't underestimate the human behind the old code. It might look wild to you, but the other coder had probably something in mind. :)
EDIT: about the fighting: Make peace with the code. You can't time travel away the problems. For me, I need to constantly adjust my mental attitude towards unfamiliar code. Sometimes that even helps a bit.
Also if the project is small enough, it's often very informative to manually trace out the main code flows.
Also if you're having trouble understanding the programmer's style it's often useful to understand their background. Often the style or technique they use is because they have a background in a different language/library that uses it. EDITED: Personally I would avoid changing the style, because it's quite easy to make a typo or mistake that's hard to catch because you don't understand the application.