{"id":650,"date":"2020-03-14T11:42:00","date_gmt":"2020-03-14T17:42:00","guid":{"rendered":"https:\/\/www.great-white-software.com\/blog\/?p=650"},"modified":"2020-03-10T12:18:39","modified_gmt":"2020-03-10T18:18:39","slug":"decide-by-not-deciding","status":"publish","type":"post","link":"https:\/\/www.great-white-software.com\/blog\/2020\/03\/14\/decide-by-not-deciding\/","title":{"rendered":"Decide by not deciding"},"content":{"rendered":"\n<p>I was doing some reading on the weekend and came across some really good thoughts on how to make decisions in code &#8211; but not by using conventional if or select branching mechanisms or others that masquerade as flow control statements under other guises.<\/p>\n\n\n\n<p>There&#8217;s a whole movement for &#8220;if-less programming&#8221;.<\/p>\n\n\n\n<p>And it fits very well with using OOP and the capabilities of polymorphism in ways that make your code clearer and easier to maintain and extend.<\/p>\n\n\n\n<p>Let&#8217;s say you have a class that represents vehicles. Your program permits you to have many types of vehicles with various capabilities. And somewhere in that class that represents a vehicle you might have code like :<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Function calculateMaximumWeightCapacity() as Double\n  if ( vehicleType = \"Transport\" ) then\n     \/\/ we have to account for the fact this has 18 wheels and 5 axles\n  elseif ( vehicleType = \"Truck\" ) then\n     \/\/ we have to account for what kind of light duty truck this is\n  elseif ( vehicleType = \"Car\" ) then\n     \/\/ we have to account for what kind of car this is as a mini cant\n     \/\/ carry as much as a impala\n  end if\nEnd Function<\/code><\/pre>\n\n\n\n<p>Yes this is contrived but you get the point<\/p>\n\n\n\n<p>IF you have classes that display this kind of code then you probably should consider using polymorphism in a way it can make you life easier.<\/p>\n\n\n\n<p>In your application, if you have this kind of code, then EVERY time you want to add a new kind of vehicle you have code to adjust. You&#8217;ll need to update every possible place this kind of selection goes on. And if you miss one you have bugs.<\/p>\n\n\n\n<p>If However we used polymorphism to help us we could be sure that any time we added a new vehicle type we definitely covered all the required functionality AND that we didnt have to adjust a lot of code in many places.(1)<\/p>\n\n\n\n<p>Instead if we had<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Class Vehicle\n    Private Sub Constructor()\n      \/\/ this is so we make this class \"abstract\" and \n      \/\/ you cannot create new vehicles - you MUST create \n      \/\/ instances of subclasses only\n    End Sub\n\n    Function calculateMaximumWeightCapacity() as Double\n      \/\/ since we have NO way to FORCE a subclass to override this \n      \/\/ we put a break in here so we can figure out which subclass \n      \/\/ did NOT override this\n      BREAK\n    End Function\nEnd Class\n\nClass Transport\n  Inherits Vehicle\n    Function calculateMaximumWeightCapacity() as Double\n        \/\/ we have to account for the fact this has 18 wheels and 5 axles\n        return 30000 \/\/ just for fun !\n     End Function\nEnd Class\n\nClass Car\n  Inherits Vehicle    \nEnd Class\n\nClass Mini\n  Inherits Car\n    Function calculateMaximumWeightCapacity() as Double\n      \/\/ we have to account for what kind of car this is as a mini cant\n      \/\/ carry as much as a impala\n      return 500\n     End Function\nEnd Class\n\nClass Impala\n  Inherits Car\n    Function calculateMaximumWeightCapacity() as Double\n      \/\/ we have to account for what kind of car this is as a mini cant\n      \/\/ carry as much as a impala\n      return 1000\n     End Function\nEnd Class\n\n.. and so on ...<\/code><\/pre>\n\n\n\n<p>And this gets rid of the need to use an IF to test what kind of vehicle it is to determine how to compute the weight. The specific subclass just does it knowing that its a transport, a truck, a mini or an impala.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Function calculateMaximumWeightCapacity() as Double\n  return self.calculateMaximumWeightCapacity()\nEnd Function\n<\/code><\/pre>\n\n\n\n<p>Note that this code is much simpler to follow.<\/p>\n\n\n\n<p>And when you need to add a new kind of vehicle you add a new class type, recompile and off your program goes because you havent had to hunt around and find all the places where you need to check vehicle types and maybe introduce new bugs.<\/p>\n\n\n\n<p>Polymorphism is definitely your friend here.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>footnotes <\/p>\n\n\n\n<p>(1) there are some things that Xojo does NOT provide that would make life simpler. A means of forcing a subclass to override a specific method defined in a super class does NOT exist. IF we use a base class we can only catch that a subclass has not overridden a method. Some languages have a means for a base class to define a method that subclasses MUST implement. Xojo does not.<\/p>\n\n\n\n<p>Its possible to use an interface to force implementors of an interface to implement a method BUT this MAY be the wrong approach for the specific problem.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was doing some reading on the weekend and came across some really good thoughts on how to make decisions in code &#8211; but not by using conventional if or select branching mechanisms or others that masquerade as flow control statements under other guises. There&#8217;s a whole movement for &#8220;if-less programming&#8221;. And it fits very &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.great-white-software.com\/blog\/2020\/03\/14\/decide-by-not-deciding\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Decide by not deciding&#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_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-650","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/650","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=650"}],"version-history":[{"count":1,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/650\/revisions"}],"predecessor-version":[{"id":651,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/650\/revisions\/651"}],"wp:attachment":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/media?parent=650"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/categories?post=650"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/tags?post=650"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}