Delete your code!

Too long; didn't read (tl;dr): Delete unused code as soon as possible. It will improve your code quality.

So you have to fix some code. It is a lot of code that was written by someone else a long time ago. (In software development terms "a long time ago" is about three months) So you pack up your gear and start diving into the code.

Scene in Kattiawar, Travellers and Escort, Artist: Stanfield, W Clarkson (1794-1867), Date: 1830

Your journey takes you to strange places in code land. You will see recursive functions, code that is called asynchronously, callbacks and configuration scripts. At some point you will pass the deployment processes and maybe some code that is run periodically by a scheduler. You will read different languages and see a lot of different code styles. You will travel to far away branches in the repository and eventually you will arrive at the spot where you find this little flaw you set out to iron out.

If the code is well written the journey can be a delight. But often the code was written in a hurry and there was no time for proper refactoring, no time for maintaining a certain code quality. In this case the journey can become a trip through a war zone. You will see dead variables lying around everywhere. Configuration options whose purpose is unknown. Large chunks of code that are commented out and no hint if this code is of any use. You will find entirely abandoned branches where no one has been for years. There will be forgotten functions, where no one knows if they are called at some point. Some code looks like from an ancient time long gone. Maybe there might be dragons in this code. All these are distractions on your way. It slows you down on the journey to the issue you want to solve, your destination.

The solution to have a nicer and faster journey is easy: delete code! I am now 15 years in the software development business and I have maintained a lot of code in a variety of programming languages in my professional career. When I start to maintain a new project I start with reading the code so I get an overview of how the code is organized. When I have a general understanding of the code I start deleting dead code.

There are different types of dead code:

  • Branches that were useful sometime in the past but are abandoned.
  • Big chunks of code that is commented out but still in the source files.
  • Functions, methods and variables that are never used.
  • Configuration options that do not have any impact.

Let's see on how to clean up the code in a efficient way:

First start with the branches. You need to know what every branch is for. If you are in luck you can contact the person who created a branch and ask him or her what branch is the main one and what the other branches are for. If you can not talk to the maintainer of the software you have to find the current branch. Most of the time this is the branch with the latest commits. (Sometimes the branch with the latest commits can also be a never finished attempt to switch to a new dependency (like a new version of the framework you are using) so please be careful) This will probably be the main one, all other branches are subject to deletion. Compare every branch with the main one. If the changes in the branch are already merged into the main branch, delete it. So now you have still some branches left. If the last commit in the branch is over a year old it is probably safe to delete is as well. Now you have just a few branches left. Repeat this step after some time (depending of the scope of the project 3, 6 or 12 months). (Create a calendar notification to remember you) The branches you have not touched in the meantime can be deleted.

Now you focus on code that is commented out in the code. Sometimes developers comment out parts of code when they refactor it. The want to have the old code to check if the new code does exactly the same as the old code. This commented out code, if not documented with the reason why it was commented out, can be deleted. It just makes developers insecure when they see it. Get rid of it.

Functions, methods and variables that are never used are harder to identify. It makes no sense to search for this kind of code. The better approach is to create a habit: whenever you work on a function check if there are no useless variable defined in it. If you are working in a file or class check if all the functions in it are used somewhere. If you happen to stumble upon something you are sure is not used, delete it. If you are not 100% sure if it is used somewhere, delete it anyway. (Your massive test suite will tell you if you broke something. You have a massive test suite, haven't you?) Modern IDEs also show you unused variables or imports. Delete them.

Another thing are unused configuration options. In a web project you have a lot of configuration: for the web server, the database, the database connection, the application server and more. Sometimes you have some properties in the config file you do not even know what they are doing. Search for the configuration options in your code. If the option is not used, delete it. But be sure to test the system thoroughly afterwards, because sometimes configuration options are used from outside code.

Every line of code can contain bugs. Every line of code costs more because it needs to be maintained and tested. Every line of code less in your system will improve the quality of your product and will reduce the cost of maintenance. Make sure to build a habit to delete unused code as soon as possible.

By deleting code your software will get better, and as a bonus you will make the journey of the next developer more enjoyable. They will thank you for that.

Have a safe journey and don't forget about the picture postcard!