How to Untangle Your Old Code

“Did I really write this?”, is what I always say after finishing the code

Satrio Wibowo
4 min readMay 24, 2022
Tangled blue and green rope on a container
Photo by cottonbro: https://www.pexels.com/photo/blue-and-green-ropes-5370855/

Every programmer must have ever experienced writing code in a time-crunch manner, meaning that you need to deliver as quickly as possible regardless of how you wrote the code. While there is nothing wrong with delivering as quickly as possible, writing code without planning will bite you. Especially if you are a freelancer who keeps maintaining your old client’s website you develop a few years ago. Argh

So, what to do when you encounter such a condition then? There are a few steps that you can take to untangle your old code (and prevent such cases from happening in the future)

Fix Your Code Style

When writing codes in a time-crunch, some people may ignore things like indentations type (tab or spaces), spaces width, braces placement, etc. While it looks like not an important thing to do, it will make code debugging hard to do down the road. To prove it, look at the following example:

public function test() {
let test = 'abc';
let testB = 'cdef';
if (test=='abc') {
console.log(test);
if(testB=='cdef'){
console.log(test);
}
}

Notice the bug yet? Let’s compare it to this:

public function test() {
let test = 'abc';
let testB = 'cdef';
if (test == 'abc') {
console.log(test);
if (testB == 'cdef') {
console.log(test);
}
}
(The bug is here, missing closing brace)

It is much easier to look for the error, right? This is one of the reasons why you need to keep track of your indentation regardless it is in a time crunch or not. If you are unable to fix most of your codebase indentation issues, you can use things like PHPCBF (PHP Code Beautifier and Fixer), ReSharper (C#), or similar software to help you with that.

Convert to Design Pattern

Styling error, check! Now it’s time to think about the design pattern that you want to use. This is of course only necessary when you haven’t thought about the design pattern before building your old app.

Why do design patterns matter, you ask? Well, deciding on a design pattern is important for a few reasons:

Improves code readability: When you are deciding on a pattern, it will help you choose the naming scheme for certain classes or functions. It also will help you on how coarse it will need to split the codes.

Improves code maintainability: By using a design pattern, other maintainers of your code will understand better where to look and what to expect from a certain class/function since the naming and code split is clear.

Improves Communication: If you are not working alone, a design pattern will help you to communicate design goals with your partner. You can talk about what and what not to put in the classes/function on the function you are working on.

Enables Code-Reuse: Writing repeatedly about the same function over and over again will increase the risk of bugs. Using a design pattern will help reduce this issue. Also, you will write less code by reusing the same code. DRY KISS* is the king!

*DRY KISS = Don’t Repeat Yourself and Keep It Simple, Stupid!

Trace, Rewrite, and Document your Code

After you fix your code styling error and decide on which design pattern you want to use, now you need to try to understand how your code work. Usually what I did to make it easy to trace is by rewriting the code into a separate function. While doing this, you can also optimize it too! Just keep in mind that the output of the code must not be replaced to prevent errors in your application. Keep DRY KISS in mind when you are rewriting/organizing your code.

Photo by Pixabay: https://www.pexels.com/photo/rope-tie-secure-tying-45873/

Congratulation! Now your code is successfully untangled. Do I think that this will take time? Of course, rewriting your old code will take time. But remember, if you plan to maintain your old code for a long time, untangling your code is worth it. You won’t need to trace again line by line the poorly written code that you wrote every single time there are maintenance or bug fixes. Or… You can also rewrite all your code from scratch if you rather do it, it still is an option. Just keep in mind the tips above 😄

Disclaimer: This article is based on my personal experience as a software developer, so any disagreement is expected.*wink*

--

--

Satrio Wibowo

Just a programmer that loves coding and learning new tech