YAGNI vs Program to interfaces rather than concrete implementations

321 Views Asked by At

YAGNI (You aren't going to need it) rule says we should the minimum number of elements in our code to do the job.

But whenever I create a class it's recommended that we should first create an interface then extend it by having a child class as per principle "Program to interfaces rather than concrete implementations" so that I can accommodate future requirements better and have more loosely coupled code.

But according to YAGNI, from the beginning, we should avoid creating unnecessary interfaces.

So which to follow and when to follow?

1

There are 1 best solutions below

2
jaco0646 On

From Martin Fowler's bliki,

Yagni only applies to capabilities built into the software to support a presumptive feature, it does not apply to effort to make the software easier to modify.

In general, programming to abstractions falls into the latter category.