Snippet is a weekly podcast where I share snippets of coding knowledge.
Originally posted on https://tiagojdf.com/
Today I want to talk about a coding principle that I think really shapes the way I code. This principle is “Less is more”.
Last week I was working on improving web vitals, namely on loading images on scroll. Usually I would just add the loading=”lazy” attribute to the images and be done with it.
The thing is, on this particular page we used some legacy code to load the images that used divs and background images. This code used some complex logic, and it impacted our templates, the frontend and the backend.
As I was working on the frontend version of the code I noticed that a parameter was never used. My first reaction was to delete it, but before I could do that I had to check if the equivalent version of the backend code used it. Turns out it did. I proceeded to see if any of these functions was ever called with that parameter. Turns out they weren’t, so I could safely delete the parameter and simplify the whole code.
This means that whoever implemented this code, did so thinking “What if we need this in the future?”. As someone from their future who had to work on their code, I can safely say we don’t need this, and we never did.
Whoever wrote that code spent time working on a future case that never happened, making their code more complex to use, harder to maintain, harder to read and harder to change. This code was 7 years old. By removing it I made life easier for me and for whoever has to work on it next.
So, next time you think “What if we need this?”, remember that you don’t need it right now, and that you can always add it later. Remember that if you add it, you are making your code harder to use, maintain, read and change for yourself and for others. Remember that less is more.
If you can subscribe to the podcast here.
Top comments (0)