4 ms·
The problem is that the design will often prove to miss things or be broken in ways you did not anticipate because people are poor at anticipating future needs.
by vidarh 13y ago
The problem is that the design will often prove to miss things or be broken in ways you did not anticipate because people are poor at anticipating future needs. This adds cost whenever someone needs to read/understand or modify the code in the future.
YAGNI is not an excuse for "throwing code at slight variations of a problem until they're buried in spaghetti".
It does on the other hand mean that introducing the Pet/Dog separation before you have chickens and cats may not be a good idea.
As you say, dog, chickens and cats all needs to move, and that's not likely where YAGNI will benefit you. Where it will benefit you is that in a lot of cases it will turn out that the extensions you thought would get added won't, but others will. Say you believe all pets will walk, and someone decides to add eagles. And what about fish - do you treat them as moving? They certainly won't follow the player around.
Or maybe no other pets than dogs will get added at all. This latter point is important, as a huge contributor to code complexity in many projects is simply unnecessary structure because people want to "be prepared". That makes sense for closed source libraries written for external consumption, where you need to predict your users needs and providing extension points etc. is crucial to making your software usable. But for open source, or internal projects it makes more sense to instead wait until you can get actual feedback from someone who wants to use the code.
When you add another type of pet, you should still do it cleanly rather than hack it in. The point is not to defer proper structure, but to implement only the structure that is justified directly by the current code.
At the same time, it is worthwhile to ensure that you keep this intent of future restructuring in mind - you should keep coupling down anyway, but if you apply YAGNI, louse coupling becomes even more important as it keeps refactoring costs down. E.g. if you refer to the Dog class by name all over the place, that's not a great idea.