{"id":4260,"date":"2014-03-30T09:33:37","date_gmt":"2014-03-30T09:33:37","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-many-types-is-too-many-for-an-object-collection-of-common-programming-errors\/"},"modified":"2014-03-30T09:33:37","modified_gmt":"2014-03-30T09:33:37","slug":"how-many-types-is-too-many-for-an-object-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-many-types-is-too-many-for-an-object-collection-of-common-programming-errors\/","title":{"rendered":"How many types is too many for an object?-Collection of common programming errors"},"content":{"rendered":"<p>A class could extend 12 interfaces, but if those 12 contracts represent an intersection of behavior such that the object still has just one responsibility, that is A-OK. Time and again, in large enterprisey projects the pieces which are most difficult to maintain are the classes which try to do too many <em>different<\/em> things. Here&#8217;s an example:<\/p>\n<p>An application which does routing and recommendation for first responders (police, fire, etc) has a class Mapping, which does:<\/p>\n<ul>\n<li>geocoding and reverse geocoding (turning lat &amp; long into an address, and vice-versa)<\/li>\n<li>drawing the map image to screen, with traffic and routing &#8216;highlights&#8217;<\/li>\n<li>finding routes between different locations<\/li>\n<li>analyzing traffic patterns provided by third party data sources<\/li>\n<\/ul>\n<p>The original designer of the system must have thought &#8220;well, these are all map &amp; location related tasks, so yeah this should be one class&#8221;. Unfortunately, this made it impossible to change one of those bits of functionality later because everything was so tightly coupled.<\/p>\n<p>Find seams in the functionality provided by various components in your application. Separate those seams into cohesive contracts (interfaces) between the components, where each unit is capable of providing a valuable &#8216;service&#8217; (not in the webservice sense) to its consumers. To go back to my previous example, we eventually broke up the Mapping class (Project Yoko) into:<\/p>\n<ul>\n<li>Geocoder<\/li>\n<li>MapRenderer<\/li>\n<li>RouteRenderer<\/li>\n<li>RouteBuilder<\/li>\n<li>TrafficRenderer (inherited from RouteRenderer)<\/li>\n<li>TrafficDatasource<\/li>\n<\/ul>\n<p>The individual components had runtime resolved dependencies, which allowed us to introduce Unit and System Tests, finding several bugs in the pre-existing routing logic, besides providing a host of other benefits. In general, if you can see a logical separation of responsibilities in a class, you probably should break it off.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A class could extend 12 interfaces, but if those 12 contracts represent an intersection of behavior such that the object still has just one responsibility, that is A-OK. Time and again, in large enterprisey projects the pieces which are most difficult to maintain are the classes which try to do too many different things. Here&#8217;s [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4260","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4260","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4260"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4260\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4260"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4260"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4260"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}