Published on

Deleting Unused Code - The Best Form of Refactoring

Authors

At the heart of it if you think of it we generally write code to fulfil some sort of need. The problem with code is you need someone to write and then maintain it. So it essentially becomes a liability - a necessary evil. If you can achieve the same thing with less or better yet no code and still be as effective than all the better.

With this in mind when I am working on something I look very carefully and decide whether what is being done can be restructured to be done with less code and if some code can even be removed completely.

Today for example I had to update some Thymeleaf templates to match what business wanted. After spending about 30 minutes on one template where I tweaked and modified it heavily I saw that there was another template which seemed very similar. I copied what was in the template before my change and diffed that against this. Lo and behold 20 lines out of the 400 to 500 lines template were different. I simply used Thymeleaf's if directive, modified usages of the old file to use the new file and deleted the mostly duplicate file. This saved me about 20 minutes now and future developers tonnes of time.