A long time ago somewhere in India empty spaces were creating a lot of problems. Numbers were being written without zeros, and believe me, it’s hard to write numbers when you replace zero with an empty space. 11 and 1 1 looked almost the same. That’s why zero was created.
In its early beginnings, zero meant “intentionally left empty”.
Let’s now travel back to the present, and see if empty spaces are causing trouble in the life of an average software developer. There are probably places in your code base where it’s not clear if something is a mistake or if it was left intentionally empty. Sometimes we don’t need the implementation of
initialize() in every child method, so we would just create an empty implementation instead of an abstract method.
I know that comments are not great, but in this case, I would suggest adding them. With the added comment, there is no confusion whether the empty method is a mistake or not.
Another example when we need an empty method is when we want to omit the behavior of the parent’s implementation. This clearly breaks Liskov substitution principle.
However, sometimes you can’t control all the code that you are using, so you have to make compromises.
Then, there are those notorious listener implementations, with a lot of methods that are usually not needed. Here is one example from the Android world:
Finally, when working on a new feature, you can’t build it all at once. Inevitably, you will have parts of the code that are unfinished. This is not really a problem, it just needs to be clearly marked as unfinished with a simple
Please don’t leave your methods empty. If the method is intentionally left empty, simply leave a comment for your fellow developers or for your future self.
Thanks for reading! If you enjoyed this story, please click the ❤️ button and share to help others find it! Feel free to leave a comment 💬 below.
Have feedback? Let’s be friends on Twitter.