Monthly Archives: February 2015

Law of Demeter – Don’t talk to strangers

One of my favorite design principles is the Law of Demeter which enforces one of the strongest principles of object-oriented programming; loose coupling. Loose coupling states that each given object should only be aware and interact with the components closest to it, in other words the object shouldn’t talk to strangers.

Bust of the Greek goddess Demeter.

Demeter – Greek goddess of agriculture. Used to signify the bottom-up principle embodied in the law.

By using a divide and conquer strategy you separate problems into smaller ones and once you’ve solved each one you will find that the bigger problem has also been solved. So an object only really needs to know about itself and solve its own problems. This makes your code easy to maintain and adapt to changes to it in the future since objects won’t be dependent on the internal structure of other objects.

To follow these guidelines you should ensure that each object only ever accesses parameters sent into its methods and interacts with its own components.

I usually like to have music on in the background when I code to drown out background noise, but every once in a while a song that has a meaning to me pops up. I’d like to paraphrase the lyrics to “Don’t talk to strangers” by Dio which inspired me to write this blog post and give you my interpretation of it in order to bring home the meaning of the Law of Demeter to you.

And by all means do listen to the song, its awesome!

Spotify: Dio – Don’t Talk To Strangers
YouTube: Don’t talk to strangers

Stop! Go listen to the song first.

Rock legend Ronnie James Dio

Rock legend

Don’t talk to strangers
Cause they’re only there to do you harm

Like the law of Demeter says. Only use objects that know of each other.

Don’t write in starlight
‘Cause the words may come out real

Don’t write code when you are too tired. It’ll haunt you the next day when you try to debug the damn thing.

Don’t hide in doorways
You may find the key that opens up your soul

Let your errors fail as soon as possible close to the problematic code, don’t catch them without doing anything! This way you’ll find any bugs fast and your problems will disappear shortly.

Don’t go to Heaven cause it’s really only Hell

Don’t use large God objects that reaches into every other object or use global variables.

Don’t smell the flowers
They’re an evil drug to make you lose your mind
Don’t dream of women
‘Cause they’ll only bring you down

Again, stay away from reaching into the seductive objects around you to find information or you’ll find that your code is that much more difficult to change later.

Hey you, you know me, you’ve touched me, I’m real

We have all made the mistake of breaking the Law of Demeter in our laziness and because it’s so easy.

I’m forever the one that lets you look and see and
Feel me I’m danger, I’m the stranger

By leaving access modifiers open, we give classes outside the scope access to internal methods and increase chances of introducing bugs.

And I, I’m darkness, I’m anger, I’m pain
I am master, the evil song you sing inside your brain
Drive you insane

Luke: Is the dark side stronger?
Yoda: No, no, no. Quicker, easier, more seductive.

Don’t talk, don’t let ‘em inside your mind
Run away, run away, girl

Driving the Law of Demeter home, don’t talk to strangers.

No, no, no don’t let them in your mind
Protect your soul

Use access modifiers correctly.

Don’t dance in darkness
You may stumble and you’re sure to fall

Know what you are doing or you are sure to introduce bugs.

Don’t write in starlight
‘Cause the words may come out real
Don’t talk to strangers
‘Cause they’re only there to make you sad
Don’t dream of women
‘Cause they’ll only bring you down

 ROCK ON!

If you like it, spread it.facebooktwittergoogle_plusredditlinkedin