I’m working on a fairly large vanilla PHP to Laravel rewrite at the moment and I wanted to share one tip that helped me cut down on the time on it took for me to rewrite parts of the application. As usual, working with legacy code can be pretty daunting. Most developers have different ideas of what legacy code means, but for the purpose of this article I’m going to use the following definition of what I think legacy code is: code that was written a while ago and with a range of very poor practices even for the time it was written in.
Focus on the Legacy Code's Current Behavior
The core of my approach is just focusing on the current behavior. So in order to do that you need to read the code and understand it. Sometimes you’ll have to even try to figure out intentions, but for the most part focus on the behaviors.
For instance, the application I’m working with builds a SQL query based on different conditions and then it displays the results.
Jot Down Notes
Now you’re going through this massive pile of code, how do you interpret it? Start by answering the following question: What is the purpose of this whole section? That’ll guide you to understand more granular parts of the code to then be able to answer “What is the purpose of the portion of the code?”.
The second part are the individual behaviors. For instance, you can say “based on X and Y conditions, this query is executed.”
Keep a numbered list of these behaviors. Now when you look at that massive pile of code you’ll have distilled the most important parts and it’ll make more sense to you. From there you’ll be able to rewrite the portions based on your notes and then double checking the legacy code your working with.
And that’s it.
Legacy code is hard to read, use those notes to help you with your memory.