Removing code without removing used functionality

3 minute read

dist files

In this post, we are going to talk about removing code. Removing code means maintaining less code and the less code that you have, the fewer bugs in your codebase.

Remove code without removing used functionality

Removing code is good if you don’t remove functionality, unless you have been asked to do so or that you detect that some functionality is not used.

To know if something is used or not, using data helps. Imagine that you have a tool that allows you to create different kinds of promo codes.

You have different kinds of promo codes and you see in the data storage that only one kind of promo code is created. Here you have a good candidate to remove the code that allows you to create the other kinds of promo codes. Before doing it you should double-check with your business partner that this functionality is not used.

Don’t be afraid to do that and don’t be attached to that piece of code. Just remove it.

The magic of removing code

When you remove code your code coverage which is the number of lines of code covered by unit tests can increase (in case that the code that you are removing was not covered by unit tests) and that’s positive too.

It’s better to have good unit tests and good functional tests rather than having really high code coverage testing “getters” and “setters”, I talk about this in this post that I wrote some time ago.

As I said in a previous post, we are not paid to write code and if we can keep the same functionalities with less code it means that our codebase is simpler with fewer dependencies and simpler to read, comprehend and maintain.

In my experience developers agree about deleting code, it can happen that they didn’t know about the code that will be deleted, but then the code review it’s a good way to share that this code is not needed anymore.

Sources of dead code

  • Legacy product features: We have talked about this in the previous paragraph. When we detect that some feature is not used we can get rid of it. No matter if this was requested by some product owner guru or some important person in your company. Just remove it.
  • Wizard-generated UI code: Some tools allow you to do some starter code. This can be a source of code to be considered to remove.
  • Return values of functions not used: This is an example of a code that is not used. Return values if are not used by the caller means that the lines of code that calculate/process this value can be considered to get removed.

How to remove code

The first thing that you need to do is to create a branch of the repository that you want to remove code. Quite helpful to do this removal are grey colors in methods or attributes in Intellij. If there’s a method that is not used the IDE warns you with this color and encourages you to take some action on that.

There’s an option also in Intellij that allows you to inspect your code. You have to go to Analyze > Inspect Code

dist files

With this option, you can find code smells, warnings, potential bugs that you can have in your code and that you should be aware of. Some of them you will have to fix, some code will be eligible to be removed and some others will be necessary to refactor.

dist files

Conclusion

In this post, we have talked about the importance of removing code, this way we reduce the number of lines of code without decreasing the functionality of our product/service, which will lead to fewer bugs and we will have more time to do other things because the time of maintenance is lower.

I won't give your address to anyone else, won't send you any spam, and you can unsubscribe at any time.
Disclaimer: Opinions are my own and not the views of my employer

Updated:

Comments