{"id":191,"date":"2006-06-26T14:55:35","date_gmt":"2006-06-26T14:55:35","guid":{"rendered":"http:\/\/kubasek.com\/blog\/pragmatic_craftsman\/?p=191"},"modified":"2006-06-26T14:55:35","modified_gmt":"2006-06-26T14:55:35","slug":"special-exceptions-ruin-oo-projects","status":"publish","type":"post","link":"https:\/\/pragmaticcraftsman.kubasek.com\/2006\/06\/26\/special-exceptions-ruin-oo-projects\/","title":{"rendered":"Special Exceptions Ruin OO Projects"},"content":{"rendered":"<p>In an object-oriented world, special exceptions are evil. To me, they ruin a well-structured framework\/project. Yet in the business world, they are fairly common. What do you do?<\/p>\n<p>I think the worse thing you can do is to actually add a special exception to a special-case free project. Once you do that, your project will take a downhill drive towards unmaintainability.<\/p>\n<p>Let&#8217;s say that you have a nice inheritance and polymorphism in place: a well-structured OO system. To be more specific, let&#8217;s say you have a base class Shape and a Circle and a Square as subclasses. Everything works nicely: you only care about the Shape class and everything else takes its polymorphic runtime form.<\/p>\n<p>Then, a business person comes in, with the usual time pressure, and asks you to implement something that resembles a Shape but it works a little differently. What do you do? You don&#8217;t want to spend some extra time to refactor the original structure. And you don&#8217;t want to implement a new structure as well &#8212; too time consuming.<\/p>\n<p>So what do you do? You add a special exception: you create a special-cased Shape. From now on, whenever you use the Shape object, you add the special exception to make sure something different is performed. Just a few lines of code. Everything works. You&#8217;re happy. You&#8217;re done. Quick.<\/p>\n<p>But you did something else. You just ruined a well structured OO project. From now on, anytime you use the Shape object, you have to duplicate the special exception. It&#8217;s only a matter of time when somebody forgets to do that. Ooops. A bug is let into the system. You created the opening with the special exception.<\/p>\n<p>What else did you do? A level of frustration to the maintainer. The system is much more difficult to maintain after that.<\/p>\n<p>Never do that! Take the time to do it right. Never put a special condition to a perfectly working OO system (with  inheritance and polymporphism). Unless, of course, you don&#8217;t care&#8230; and you consider yourself a crappy programmer.<\/p>\n<p>I like the following saying: If you don&#8217;t have the time to do it right, will you have the time to do it over?<\/p>\n<p>You save some time at the time of implementation, but you will pay for it several times later.<\/p>\n<p>So, anytime you need to add a special condition, and it&#8217;s inevitable, refactor the code to support that, but don&#8217;t add a special condition. It never works.<\/p>\n<p>A better coder is a happy coder, don&#8217;t spoil that. \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In an object-oriented world, special exceptions are evil. To me, they ruin a well-structured framework\/project. Yet in the business world, they are fairly common. What do you do? I think [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-191","post","type-post","status-publish","format-standard","hentry","category-better-coder"],"_links":{"self":[{"href":"https:\/\/pragmaticcraftsman.kubasek.com\/wp-json\/wp\/v2\/posts\/191","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pragmaticcraftsman.kubasek.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pragmaticcraftsman.kubasek.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pragmaticcraftsman.kubasek.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pragmaticcraftsman.kubasek.com\/wp-json\/wp\/v2\/comments?post=191"}],"version-history":[{"count":0,"href":"https:\/\/pragmaticcraftsman.kubasek.com\/wp-json\/wp\/v2\/posts\/191\/revisions"}],"wp:attachment":[{"href":"https:\/\/pragmaticcraftsman.kubasek.com\/wp-json\/wp\/v2\/media?parent=191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pragmaticcraftsman.kubasek.com\/wp-json\/wp\/v2\/categories?post=191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pragmaticcraftsman.kubasek.com\/wp-json\/wp\/v2\/tags?post=191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}