The Law of Demeter is important in object-oriented programming. But where does “Demeter” come from?
Here’s a simplified example of the law: code that lets us ask an owner, “What is your dog’s breed?”
class Dog
belongs_to :owner
belongs_to :breed
def breed_name
breed.name
end
end
class Owner
has_one :dog
# ✅ Owner only knows about 'breed_name',
# not what 'breed' and 'name' are.
def dog_breed_name
dog.breed_name
end
# ❌ Owner knows that dog has a 'breed', and
# that 'breed' has a 'name.'
def dog_breed_name
dog.breed.name
end
end
This name comes from the Demeter project, so named because the goddess Demeter is related to Zeus. So, in a way, the name and the idea aren’t very related. But, I found this online:
Later we promoted the idea that Demeter-style software development is about growing software as opposed to building software. We introduced the concept of a growth plan which is basically as sequence of more and more complex UML class diagrams. Growth plans are useful for building systems incrementally.
Since Demeter was the goddess of fertility and harvests, I think that there’s some conceptual overlap with how a system grows and stays maintainable, based on the “Principle of Least Knowledge.”