{"id":964,"date":"2010-06-25T08:47:34","date_gmt":"2010-06-25T15:47:34","guid":{"rendered":"https:\/\/simpleprogrammer.com\/?p=964"},"modified":"2016-07-14T15:02:23","modified_gmt":"2016-07-14T19:02:23","slug":"dont-chain-failure-states-in-returns","status":"publish","type":"post","link":"https:\/\/simpleprogrammer.com\/dont-chain-failure-states-in-returns\/","title":{"rendered":"Don&#8217;t Chain Failure States in Returns"},"content":{"rendered":"<p>I had originally started writing this post thinking that I knew that answer to the problem I am about to demonstrate.<\/p>\n<p>What surprised me is how much more clean a try catch solution seems to work than my original solution.<\/p>\n<h2>The unclean code<\/h2>\n<div class=\"wlWriterEditableSmartContent\" id=\"scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:3bd0022f-f33f-4432-8d33-bdb7be5848ee\" style=\"display: inline; float: none; margin: 0; padding: 0;\">\n<p><script src=\"https:\/\/gist.github.com\/simpleprogrammer-shared\/bd6864613734446c6261f552c8978fc6.js\"><\/script><\/p>\n<\/div>\n<p>I am using a short example here, so the refactoring might not really jump out to you as necessary, but let\u2019s refactor it to be as clean as possible anyway.\u00a0 (This comes from a much larger example where the refactoring is a must.)<\/p>\n<h2>Refactor 1: Returning booleans<\/h2>\n<div class=\"wlWriterEditableSmartContent\" id=\"scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:aa2be422-8397-45a6-aee3-94208b7d6814\" style=\"display: inline; float: none; margin: 0; padding: 0;\">\n<p><script src=\"https:\/\/gist.github.com\/simpleprogrammer-shared\/784f85d4b9e55b4126be2bfd7ed7d70c.js\"><\/script><\/p>\n<\/div>\n<p>We\u2019ve broken each loop into its own method and separated the handling of each line and each column in each line into its own method.\u00a0 A good refactor that makes things a little more clear.<\/p>\n<p>Notice the bool return types on the new methods and the special handling to return early if a result is false.\u00a0 Most people when refactoring the first example (myself included) forget that the first example breaks out of both loops with an early return.<\/p>\n<p>If you don\u2019t take this into account, your behavior will be different.\u00a0 <strong>When you refactor, your behavior had better not be different.<\/strong><\/p>\n<p>This is the crux of the problem.\u00a0 Returning bools is bad.\u00a0 It doesn\u2019t make sense that our methods return bool.\u00a0 What does the bool mean?\u00a0 We happen to know it means success or failure, but will the next reader?\u00a0 It seems very out of place, and look at the verbosity to handle the return types.<\/p>\n<h2>Refactor 2: Storing error state<\/h2>\n<p>This next refactor was what this post was going to be titled.\u00a0 I was going to suggest in this post that you should not return boolean error states, but instead use the error state as part of your class.\u00a0 I still think this is a decent solution.<\/p>\n<div class=\"wlWriterEditableSmartContent\" id=\"scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:3d58e88f-e2ab-42b0-ac70-39e2de429aa0\" style=\"display: inline; float: none; margin: 0; padding: 0;\">\n<p><script src=\"https:\/\/gist.github.com\/simpleprogrammer-shared\/4e7646555440108c5bb78727c786fc99.js\"><\/script><\/p>\n<\/div>\n<p>What we have done here is stored some state information about our object instead of chaining result codes down the method calls.\u00a0 This is a huge improvement, because our meaning is much more explicit and our object is holding its own state.<\/p>\n<p>It still is quite verbose.\u00a0 Why do we have to check for a fatal error everywhere?\u00a0 Seems like we are likely to forget.<\/p>\n<p>Still, much better than returning booleans.\u00a0 If you are in a class don\u2019t be afraid to manipulate the state of that class internally.\u00a0 <strong>Many times you can eliminate return values and parameters passed into private methods in a class by simply making that data part of the class.<\/strong><\/p>\n<p>So, the verbosity was still bothering me.\u00a0 So, I tried this:<\/p>\n<h3>Refactor 3: Exception handler flow control<\/h3>\n<div class=\"wlWriterEditableSmartContent\" id=\"scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:154007d7-d1d7-404a-9aa1-bc284341a249\" style=\"display: inline; float: none; margin: 0; padding: 0;\">\n<p><script src=\"https:\/\/gist.github.com\/simpleprogrammer-shared\/88b7291fdf200f86c7e07c6a8f523bd3.js\"><\/script><\/p>\n<\/div>\n<p>This is surprisingly clean.\u00a0 Notice that we don\u2019t actually have to put if conditions in the loops to check whether or not we should return early?\u00a0 Instead, if an exception is thrown, flow will jump immediately to the catch block where we will log the error message.<\/p>\n<p>I\u2019m a little bit uncomfortable to be using exception handling as a flow control mechanism, but given that:<\/p>\n<ol>\n<li>When encountering the error, we are aborting the normal flow of the program completely (breaking out of two loops).<\/li>\n<li>It is so much more clean and clear.<\/li>\n<\/ol>\n<p>I think this is actually my preferred solution.\u00a0 I normally would never use exceptions where I could reasonably detect the failure condition, but I may have to rethink that viewpoint.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I had originally started writing this post thinking that I knew that answer to the problem I am about to demonstrate. What surprised me is how much more clean a try catch solution seems to work than my original solution. The unclean code I am using a short example here, so the refactoring might not&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[3378,2185,2426,1934,54712],"tags":[],"class_list":["post-964","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-best-practices","category-c","category-language","category-refactoring"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Don&#039;t Chain Failure States in Returns<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/simpleprogrammer.com\/dont-chain-failure-states-in-returns\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Don&#039;t Chain Failure States in Returns\" \/>\n<meta property=\"og:description\" content=\"I had originally started writing this post thinking that I knew that answer to the problem I am about to demonstrate. What surprised me is how much more clean a try catch solution seems to work than my original solution. The unclean code I am using a short example here, so the refactoring might not...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/simpleprogrammer.com\/dont-chain-failure-states-in-returns\/\" \/>\n<meta property=\"og:site_name\" content=\"Simple Programmer\" \/>\n<meta property=\"article:published_time\" content=\"2010-06-25T15:47:34+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2016-07-14T19:02:23+00:00\" \/>\n<meta name=\"author\" content=\"John Sonmez\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"John Sonmez\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/dont-chain-failure-states-in-returns\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/dont-chain-failure-states-in-returns\\\/\"},\"author\":{\"name\":\"John Sonmez\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#\\\/schema\\\/person\\\/1abc70edfb189184827740310672de67\"},\"headline\":\"Don&#8217;t Chain Failure States in Returns\",\"datePublished\":\"2010-06-25T15:47:34+00:00\",\"dateModified\":\"2016-07-14T19:02:23+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/dont-chain-failure-states-in-returns\\\/\"},\"wordCount\":561,\"commentCount\":5,\"articleSection\":[\"Algorithms\",\"Best Practices\",\"C#\",\"Language\",\"Refactoring\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/simpleprogrammer.com\\\/dont-chain-failure-states-in-returns\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/dont-chain-failure-states-in-returns\\\/\",\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/dont-chain-failure-states-in-returns\\\/\",\"name\":\"Don't Chain Failure States in Returns\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#website\"},\"datePublished\":\"2010-06-25T15:47:34+00:00\",\"dateModified\":\"2016-07-14T19:02:23+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#\\\/schema\\\/person\\\/1abc70edfb189184827740310672de67\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/dont-chain-failure-states-in-returns\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/simpleprogrammer.com\\\/dont-chain-failure-states-in-returns\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/dont-chain-failure-states-in-returns\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/simpleprogrammer.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Don&#8217;t Chain Failure States in Returns\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#website\",\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/\",\"name\":\"Simple Programmer\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/simpleprogrammer.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#\\\/schema\\\/person\\\/1abc70edfb189184827740310672de67\",\"name\":\"John Sonmez\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g\",\"caption\":\"John Sonmez\"},\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/author\\\/jsonmez\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Don't Chain Failure States in Returns","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/simpleprogrammer.com\/dont-chain-failure-states-in-returns\/","og_locale":"en_US","og_type":"article","og_title":"Don't Chain Failure States in Returns","og_description":"I had originally started writing this post thinking that I knew that answer to the problem I am about to demonstrate. What surprised me is how much more clean a try catch solution seems to work than my original solution. The unclean code I am using a short example here, so the refactoring might not...","og_url":"https:\/\/simpleprogrammer.com\/dont-chain-failure-states-in-returns\/","og_site_name":"Simple Programmer","article_published_time":"2010-06-25T15:47:34+00:00","article_modified_time":"2016-07-14T19:02:23+00:00","author":"John Sonmez","twitter_card":"summary_large_image","twitter_misc":{"Written by":"John Sonmez","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/simpleprogrammer.com\/dont-chain-failure-states-in-returns\/#article","isPartOf":{"@id":"https:\/\/simpleprogrammer.com\/dont-chain-failure-states-in-returns\/"},"author":{"name":"John Sonmez","@id":"https:\/\/simpleprogrammer.com\/#\/schema\/person\/1abc70edfb189184827740310672de67"},"headline":"Don&#8217;t Chain Failure States in Returns","datePublished":"2010-06-25T15:47:34+00:00","dateModified":"2016-07-14T19:02:23+00:00","mainEntityOfPage":{"@id":"https:\/\/simpleprogrammer.com\/dont-chain-failure-states-in-returns\/"},"wordCount":561,"commentCount":5,"articleSection":["Algorithms","Best Practices","C#","Language","Refactoring"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/simpleprogrammer.com\/dont-chain-failure-states-in-returns\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/simpleprogrammer.com\/dont-chain-failure-states-in-returns\/","url":"https:\/\/simpleprogrammer.com\/dont-chain-failure-states-in-returns\/","name":"Don't Chain Failure States in Returns","isPartOf":{"@id":"https:\/\/simpleprogrammer.com\/#website"},"datePublished":"2010-06-25T15:47:34+00:00","dateModified":"2016-07-14T19:02:23+00:00","author":{"@id":"https:\/\/simpleprogrammer.com\/#\/schema\/person\/1abc70edfb189184827740310672de67"},"breadcrumb":{"@id":"https:\/\/simpleprogrammer.com\/dont-chain-failure-states-in-returns\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/simpleprogrammer.com\/dont-chain-failure-states-in-returns\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/simpleprogrammer.com\/dont-chain-failure-states-in-returns\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/simpleprogrammer.com\/"},{"@type":"ListItem","position":2,"name":"Don&#8217;t Chain Failure States in Returns"}]},{"@type":"WebSite","@id":"https:\/\/simpleprogrammer.com\/#website","url":"https:\/\/simpleprogrammer.com\/","name":"Simple Programmer","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/simpleprogrammer.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/simpleprogrammer.com\/#\/schema\/person\/1abc70edfb189184827740310672de67","name":"John Sonmez","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/dc2f18dfa76e017566ce607de002d7abe4acfd5ec19a6db82c180b00867b8d94?s=96&d=mm&r=g","caption":"John Sonmez"},"url":"https:\/\/simpleprogrammer.com\/author\/jsonmez\/"}]}},"_links":{"self":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/posts\/964","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/comments?post=964"}],"version-history":[{"count":0,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/posts\/964\/revisions"}],"wp:attachment":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/media?parent=964"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/categories?post=964"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/tags?post=964"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}