In my early years as a programmer I was always afraid to touch existing code. Usually that code was written by someone else. It was so hard for me to understand it. I found out after a while that this kind of code is called “spaghetti code”. Now I am having fun making convoluted code understandable by using the techniques I master.
It was hard for me to learn these techniques because I practised them on my own. I had a lot of questions and worries when changing the code in ways that felt unsafe. To understand more what I was doing and what I could do better, I reached out for help to programmers I respect highly for their experience and ability to teach. Now I am happy I can teach these techniques to other programmers so they can apply them in production faster than I did learning by myself.
The thing with touching existing code is that you are always afraid you might do something wrong. While you get dirty with spaghetti sauce, you might not find the simplest necessary change in the code. What I am doing now is using gloves to make sure I will not smear my hands with the spaghetti sauce. My gloves are the automated tests that I trust will keep my hands clean.
I do not want to understand the code in the beginning, because from my experience it is almost impossible. I start writing some tests that will show me what the system does. These tests are called characterization tests, because they help you learn and document the system. I want to find the important tests to write in order to reach my purpose: change the system as less as possible.
The next very important thing is taking very small steps. A step is too large when you do not understand how the functionalities will be affected by the change. In the case you make too large steps, you need to try and make them smaller. My experience showed me that committing every 1-2 minutes when working on existing code is safer. I feel safe with this length of a step because my mind cannot comprehend on the spot the many implications a larger change can have.
Alex Bolboaca had a coding-talk with the title “You don’t need to read legacy code in order to understand it” at XP Days Hamburg in 2012. By following this video you can get an introduction of some of the mechanics.
We have a 2-day workshop Working FAST and safe with existing code where you can learn and practice in detail all these techniques.