[rspec-users] Associations should be private

David Chelimsky dchelimsky at gmail.com
Wed Oct 1 22:06:56 EDT 2008

On Wed, Oct 1, 2008 at 8:38 PM, Mark Wilden <mark at mwilden.com> wrote:
> On Wed, Oct 1, 2008 at 6:22 PM, Ashley Moran <ashley.moran at patchspace.co.uk>
> wrote:
>> On Oct 01, 2008, at 8:17 pm, Mark Wilden wrote:
>>> Sounds like future-proofing to me. In the case of libraries, that can be
>>> a good thing. For application code, it flies in the face of YAGNI.
>> Actually I don't think that's a YAGNI.  You need *an* interface to your
>> models, the question is whether to build one that litters trainwrecks
>> 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:



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


> However, I say that there are too many things that need doing right now -
> right this second - to waste time on what may or may not happen in the
> future. Put another way, what are you willing to give up in order to add
> this layer?
> "Litters trainwrecks," BTW, I believe begs the question.
>>> Embrace change" - Kent Beck. The whole white book is predicated on what
>>> you just pointed out about Rails.
>> You meant to say Ruby there, right? ;o)
> I was responding to the "convention over configuration" remark. But yeah,
> Ruby certainly does make changing stuff easier than many other languages.
> ///ark
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

More information about the rspec-users mailing list