As a freelance developer / consultant, I am regularly hired by clients that outsource software development and don’t have somebody that looks at the sourcecode for quality. Unfortunately, I am also stuck dealing with this legacy spaghetti monster code that is in serious need of refactoring.
Whenever you deal with crappy code, you need to ensure you have enough time to understand how it functions (understanding!) and how you can minimize doing any damage as you add features or make changes. It is always good to have a general idea of how everything works in the software you are developing/improving if possible have a look at any specifications or design documents.
My first step is version control, tests, and then almost always to quickly read through the code and look for any duplication and if possible, my first step is to eliminate that duplication and break the code into smaller functions. Abstraction and encapsulation are our best tools when dealing with legacy and crappy code that we need to minimize time refactoring or adding features to.
I create my tests as I try to develop an understanding of the code which also allows me to safely make modifications and ensure the code still works as I inherited it.One of my other main tools is to ensure I’m using source control, so I can quickly rollback any changes that have broken the code without having to contend with large chunks of commented out code.
I always end up referring to my version of “Refactoring: Improving the Design of Existing Code” by Martin Fowler. I consider it the bible of working with legacy code.
Hope this helps!