Like most of you, I grew up in Casper, Wyoming, USA. Being from the wild west I grew up using the word "ain't" quite regularly. So when I first came across the YAGNI principal I could easily relate to the underlying message "You Ain't Gonna Need It". What does it mean and why is it still relevant to software development?
In a nutshell, YAGNI means that you should not write code that you do not need. That may seem painfully obvious, even to the novice developer, but even seasoned pros forget this sometimes. I'll give you an example:
At one of my jobs we had a website where we processed scores of transactions every day and we had several developers working on the code to maintain this website. While architecting some new functionality I ran across a function, let's call it addCardMethod (the names of the methods have been changed to protect the innocent). This didn't make sense because we also had createCardMethod which seemed to accomplish the same thing in a slightly different way. I traced the code for both methods thoroughly and found that the backend API and front end JavaScript were both regularly updated for both paths.
The problem was that addCardMethod was dead code, never called anywhere. It was written just in case we needed to change the way we add card methods if we change card processors.
It's the "just in case" part that is the problem here, and many other projects I've worked on. When you write code just in case you might need it some day then what happens if you never need it? You end up with dead code that still gets maintained. This wastes developer time, QA time, architect time and can cause bugs that simply didn't need to be introduced. In our case it wasted many hours of dev and QA time over several months and took me several hours to figure out.
So the next time you design some new functionality ask yourself if you actually need it right now. If not, save yourself some future pain and don't write it if you ain't gonna need it.
Top comments (0)