LoD questions

Paul Winkler slinkp23 at yahoo.com
Tue Sep 18 14:32:20 EDT 2001


I've been reading about the Law of Demeter which says, in a nutshell,
that methods should only talk to their "close friends", a concept
which seems to be vague by intent.

My question boils down to this: How does one find an appropriate
balance between minimizing the number of dots and managing the number
of methods in the parent object?

If I have an object, foo, that contains lots of information, the LoD
says that however I organize it, I don't want to directly dig deep
inside like so:

x = foo.bar.bat.baz.baf()

But it may not be practically possible to put methods for every
possible thing directly in foo, or else I'll have hundreds of methods
that look like

x = foo.get_bar_bat_baz("baf")

So what's a reasonable compromise? Is it generally considered OK under
the LoD to reach directly in two layers? e.g.

x = foo.bar.get_bat_baz("baf")

So we only need to know about foo that it contains an object bar with
a method a get_bat_baz("baf") and don't need to worry how that method
works.

Comments?

--Paul Winkler




More information about the Python-list mailing list