On Wed, Oct 1, 2008 at 8:38 PM, Mark W. [email protected] wrote:
through your code, or one that is internally refactorable.
ActiveRecord already provides an interface to your models. If you add on to
that because it might things easier in the future, that’s where I call
“YAGNI.” It reminds me of a book on Java by a well-known author which said
that you should declare an interface for every class, because it will make
things easier if you need to change the implementation. He’s absolutely
right, of course.
Au contraire! You’re comparing apples and cadillacs here. Demeter is
about encapsulation. Interfaces are about abstraction and structure.
Completely different animals.
Unless you’re developing an library that will be directly consumed
outside your team, the cost of not adding the Java Interface until the
need for an abstraction arises is very low. Finding all of the
instantiations of the class is easy, and converting them to us a
factory call is easy too.
Tracking down all of the trainwrecks in a system is not quite so
simple. First of all, they are not guaranteed to look the same:
trainer.animals.dogs.first
vs
t = trainer
a = trainer.animals
d = trainer.dogs
dog = dogs[0]
So when the design seems to want to categorize animals into domestic
and wild, the first call has to be changed to
trainer.animals.domestic.dogs.first. Good luck tracking down the
second example.
Principles/guidelines like YAGNI and DRY and even the
ever-threatening-sounding Law of Demeter are NOT LAWS. They are
indicators. Red flag triggers. Red flags are warnings, not errors.
In this case, we’ve got two of these in direct conflict with each
other, so which one wins? Gotta look at the costs and benefits of each
and proceed wisely and in context, not with a face stained with
kool-aide.
FWIW,
David