a peek into my MIND

November 25, 2009

Law of Demeter

Filed under: General — Bharat Kondeti @ 4:54 pm

Also know as ‘Principle of least knowledge’.

“It means a object should only know about objects that it can directly act upon”. Following this principle will result in a modular code that is easily testable.

A really nice analogy for this principle is from The Clean Code Talks – Don’t Look For Things! by Misko Hevery.

You are in a store and the item you are purchasing costs 10 dollars.

Will you give the clerk 10 dollars ? or Will you give the clerk your wallet and let him retrieve the 10 dollars?

Of-course a sane person will give the clerk 10 dollars. Same principle applies when creating the objects. You should only pass into the object that is relevant and that it can directly act upon.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: