Learning from mistakes: Keeping your code DRY

Just yesterday, while having a friendly chat with a recruiter, she asked me to tell her one of the situations where I have learned from a mistake. Unfortunately I couldn’t recall one, even though it’s very common for me. 24 hours later, here I am, once again learning from a mistake.

I am making changes to a project I worked in mid-2014. The project is a job listing website, and the client needed to change some of the labels on filters (sort by asc, show latest first, etc.). These filters were shown on a lot of pages. When I looked into the code, what I have done was, instead of creating a reusable element/partial for filters, I had the same duplicated code on each page, which led me to modify 5 files instead of 1.

Screen Shot 2016-08-06 at 12.55.06 PM

I obviously have refactored the code, to use a partial instead of pasting duplicate code on each page that required to show the filters. If I followed the good old design principle Don’t Repeat Yourself (DRY),  I could have just edited 1 file and be done with the task.

I am not exactly sure why I did not keep my code DRY back then. Maybe I was lazy. What I do know is, it’s always important to go beyond the basic requirements. It’s always important to follow the best principles and patterns, because it doesn’t just help to build a better product, it also helps you as a developer to maintain the code over time.

Leave a Reply

Your email address will not be published. Required fields are marked *