Recently, I had acquired a project that had 100K+ lines of code. Sure, 100K lines of code isn’t the largest project around… However, the code was not extremely organized – there wasn’t even a consistent coding style. This can take a project from hard, to unbearable. I, perhaps foolishly, decided that I would tackle the code nevertheless. As I did some searches on how best to read through a project that contains “Spaghetti Code”, I found that there really wasn’t any good advice on how to do it. Hopefully, this post can shed some light on how best to work with terrible code.
Find the Main Controlling File
The best thing I did to start understanding the code was to find the main file. This seemed like a logical place to start as this is where all of the action starts. After I found that file, I looked at the code inside. As usual, the code made reference to other files that undoubtedly (in most cases) played into the operation of the program.
Find Any Semblance of Order
As I said before, this project had little-to-no order. That obviously made this part hard… However, if there are ANY consistencies throughout the code, you better pick up on them quickly. You will want to recognize those consistencies as they will help you understand the code that you have to work with.
Start Completely Anew
It will take you much longer to try to understand and edit the code in the original files than to start in a completely new directory and try to organize the mess. This approach does more than just give you a fresh start, it also forces you to actually learn and look through ALL of the code so you do not miss a single feature. This way, you may also find ways to reduce the amount of code or even finding lost bugs while refactoring the code. Overall, it’s a win-win situation.
Standardize the Code
This one is pretty obvious… When looking through the code make sure to find ways to standardize it. Make it so that the code can be easily read and understood by your future self. Put it into one coding style and make sure you document that coding style so that if any other developers get involved in the project they will know what coding standards to comply to. This will help prevent unreadable code in the future.
There’s no doubt about it: there is no real easy way to look through a large amount of messy code and make complete sense of it. This is a skill that, unfortunately, not many programmers have under their belts. However, the best way to learn is to start reading through code and trying to figure out what it does. That will be an experience you could thank me for.