{"id":505,"date":"2020-01-10T12:34:00","date_gmt":"2020-01-10T19:34:00","guid":{"rendered":"https:\/\/www.great-white-software.com\/blog\/?p=505"},"modified":"2019-12-31T15:52:40","modified_gmt":"2019-12-31T22:52:40","slug":"the-law-of-demeter","status":"publish","type":"post","link":"https:\/\/www.great-white-software.com\/blog\/2020\/01\/10\/the-law-of-demeter\/","title":{"rendered":"The Law of Demeter"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Recently I re-read The <a href=\"https:\/\/en.wikipedia.org\/wiki\/The_Pragmatic_Programmer\">Pragmatic Programmer<\/a> mostly while on a break in Mexico. I also did my fair share of sitting in the sun, eating and event drinking but I read a LOT as well.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">And I keep learning, or at least reminding myself, of several things I think are really useful to all of us.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In terms of writing OOP code the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Law_of_Demeter\">Law Of Demeter<\/a> is one that is really useful. Basically in this <em>Law<\/em> you should only access :<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">&#8211; items (properties, methods, constants, etc) of the of the object you are part of<br>&#8211;  items (properties, methods, constants, etc) you are passed as parameters<br>&#8211;  items (properties, methods, constants, etc) of objects you create in your code<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This limits the knowledge your code needs of the internals of other objects.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Suppose we have a set up something like :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Global Dim someGlobalVar as integer\n\nClass FooBar\n   Dim i as integer\n   Sub fubaz() as string\n   End Sub\nEnd Class\n\nClass Bar\n   Dim fubar as FooBar\n   Sub Constructor()\n     fubar = new FooBar\n   End Sub \n \n   Sub Baz()\n   End Sub\nEnd Class\n\nClass Foo\n  Dim BarInstance as Bar\n\n  Sub Constructor\n    self.BarInstance = new Bar\n  End Sub\n\n  Function HasInstance() as Boolean\n    \/\/ this one is OK !!!!!\n    return (BarInstance is nil) = false   \n  End Function\n\n  Sub CallBazOfBar()\n    \/\/ this one is OK !!!!!\n    BarInstance.Baz()\n  End function\n\n  Function CallBazOfPassedInBar(b as Bar)\n    \/\/ this one is OK !!!!!\n    b.Baz()\n  End Function\n\n  Function CreateAndCallBazOfBar()\n    \/\/ this one is OK !!!!!\n    dim b as new Bar\n    b.Baz()\n  End Function\n\n  Function ReachesThroughBarToFooBar() as integer\n    return BarInstance.fubar.i\n  End function\n\n  Function ReliesOnGlobal() as integer\n    return someGlobalVar\n  End function\n\n  \nEnd Class <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then for us to adhere to this principle the following new methods on our Class Foo would NOT be ok :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Function ReachesThroughBarToFooBar() as integer\n   return BarInstance.fubar.i\nEnd function<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This one is NOT ok because it requires the Foo class to know more about the internals and implementation of the Bar class AND FooBar class than it should. Instead the Bar class should have a method on it that can be invoked to get details of its properties or classes it owns.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Function ReliesOnGlobal() as integer\n   return someGlobalVar\nEnd function<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This one is NOT ok because it requires the Foo class to rely on a global variable that is outside the scope of the instance and the method using it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Methods in our Foo class should ONLY use :<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>methods in the instance itself (HasInstance is OK)<\/li><li>methods of any parameters passed to the methods like CallBazOfPassedInBar(b as Bar)<\/li><li>Any objects created\/instantiated within\u00a0the method like CreateAndCallBazOfBar<\/li><li>methods in the instances properties (in this case methods of Bar like CallBazOfBar)<\/li><li>A global variable, accessible by\u00a0the instance, that is in the scope of\u00a0the method (this last one is harder to explain)<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">I like the Wikipedia summary which is<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>\u00a0For many modern object oriented languages that use a dot as field identifier, the law can be stated simply as &#8220;use only one dot&#8221;. That is, the code\u00a0<code>a.b.Method()<\/code>\u00a0breaks the law where\u00a0<code>a.Method()<\/code>\u00a0does not.<\/p><cite>https:\/\/en.wikipedia.org\/wiki\/Law_of_Demeter<\/cite><\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently I re-read The Pragmatic Programmer mostly while on a break in Mexico. I also did my fair share of sitting in the sun, eating and event drinking but I read a LOT as well. And I keep learning, or at least reminding myself, of several things I think are really useful to all of &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.great-white-software.com\/blog\/2020\/01\/10\/the-law-of-demeter\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;The Law of Demeter&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[1],"tags":[],"class_list":["post-505","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/505","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/comments?post=505"}],"version-history":[{"count":1,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/505\/revisions"}],"predecessor-version":[{"id":506,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/505\/revisions\/506"}],"wp:attachment":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/media?parent=505"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/categories?post=505"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/tags?post=505"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}