{"id":1379,"date":"2011-08-14T21:25:52","date_gmt":"2011-08-15T04:25:52","guid":{"rendered":"https:\/\/simpleprogrammer.com\/?p=1379"},"modified":"2018-04-12T01:38:32","modified_gmt":"2018-04-12T05:38:32","slug":"making-switch-refactorings-better-defaultable-dictionary","status":"publish","type":"post","link":"https:\/\/simpleprogrammer.com\/making-switch-refactorings-better-defaultable-dictionary\/","title":{"rendered":"Making Switch Refactorings Better &#8211; Defaultable Dictionary"},"content":{"rendered":"<p>I\u2019ve written before on the idea of <a href=\"https:\/\/simpleprogrammer.com\/2010\/08\/17\/pulling-out-the-switch-its-time-for-a-whooping\/\">refactoring a switch<\/a> to a Map or Dictionary.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/downloadsoftwarestore.com\/software_images\/75\/63\/00046375\/English_Dictionary_Toolbar-screenshot.jpg\" alt=\"\" width=\"485\" height=\"364\" \/><\/p>\n<p>There is one major problem that I have been running into though.\u00a0 Switch statements and dictionaries are not functionally equivalent for one major reason\u2026<\/p>\n<h2>Switches allow for default<\/h2>\n<p>I kept struggling with this when I would implement a dictionary to replace a switch.\u00a0 How can I deal with the default case?<\/p>\n<p>There are of course many ways to deal with the default case in a dictionary or map, but I didn\u2019t really like any of the solutions because they either required me to remember to check to see if my entry was in the dictionary before looking it up, or to rely on catching an exception.<\/p>\n<p>Let me give you an example:<\/p>\n<div id=\"scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:e177ff43-8154-4138-8f72-5911df394c74\" class=\"wlWriterEditableSmartContent\" style=\"display:inline;float:none;margin:0;padding:0;\">\n<p><script src=\"https:\/\/gist.github.com\/simpleprogrammer-shared\/3d887ab7745399af34393ef357eeeae4.js\"><\/script><\/p>\n<\/div>\n<p>Converting this to a dictionary we get something like:<\/p>\n<div id=\"scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:fc164edc-458d-4cbb-82b7-5fa532b513cf\" class=\"wlWriterEditableSmartContent\" style=\"display:inline;float:none;margin:0;padding:0;\">\n<p><script src=\"https:\/\/gist.github.com\/simpleprogrammer-shared\/7369e79208cc4563483956552d9be62b.js\"><\/script><\/p>\n<\/div>\n<p>So clunky, just to handle the default case.<\/p>\n<p>Would be much better do something like this:<\/p>\n<div id=\"scid:C89E2BDB-ADD3-4f7a-9810-1B7EACF446C1:1e8f4a47-088d-492e-bcea-c864b91b0c7a\" class=\"wlWriterEditableSmartContent\" style=\"display:inline;float:none;margin:0;padding:0;\">\n<p><script src=\"https:\/\/gist.github.com\/simpleprogrammer-shared\/99db39b6734240df733af59c055c19f3.js\"><\/script><\/p>\n<\/div>\n<p>Well now you can!<\/p>\n<p>Enter <a href=\"https:\/\/github.com\/jsonmez\/Defaultable-Dictionary\">DefaultableDictionary<\/a>!<\/p>\n<p>Also the first thing I ever put on GitHub!<\/p>\n<p>The idea is pretty simple, I am just creating a decorator for IDictionary.<\/p>\n<p>The DefaultableDictionary has a constructor that takes an IDictionary and a default value.<\/p>\n<p>It then delegates all of the methods to the passed in IDictionary reference.\u00a0 For the methods that look up a value, it handles returning the default if the key doesn\u2019t exist in the dictionary.<\/p>\n<p>I created an extension method that lets you just put a .WithDefaultValue() on the end of your dictionary declaration in order to auto-magically give you a DefaultableDictionary back with that default value.<\/p>\n<h2><\/h2>\n<h2><\/h2>\n<h2>Sleep well my friend<\/h2>\n<p>Knowing that you can not create a dictionary that has a default value which is returned instead of throwing an exception if the key passed in is not found.<\/p>\n<p>I have no doubt that in 3rd world countries children are still starving, but in 1st world countries children with VS Express hacking away at iPhone applications using MonoTouch will not have to catch exceptions from dictionaries that do not know how to just return a default value.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.buzzinn.net\/img\/bizarre-and-odd\/unusual-begging-methods\/unusual-begging-methods19.jpg\" alt=\"\" width=\"499\" height=\"375\" \/><\/p>\n<p>So now there is no excuse!\u00a0 Refactor those switches!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I\u2019ve written before on the idea of refactoring a switch to a Map or Dictionary. There is one major problem that I have been running into though.\u00a0 Switch statements and dictionaries are not functionally equivalent for one major reason\u2026 Switches allow for default I kept struggling with this when I would implement a dictionary to&#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,2426,5603054],"tags":[],"class_list":["post-1379","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-c","category-github"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Making Switch Refactorings Better - Defaultable Dictionary - Simple Programmer<\/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\/products\/careerguide\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Making Switch Refactorings Better - Defaultable Dictionary - Simple Programmer\" \/>\n<meta property=\"og:description\" content=\"I\u2019ve written before on the idea of refactoring a switch to a Map or Dictionary. There is one major problem that I have been running into though.\u00a0 Switch statements and dictionaries are not functionally equivalent for one major reason\u2026 Switches allow for default I kept struggling with this when I would implement a dictionary to...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/simpleprogrammer.com\/products\/careerguide\" \/>\n<meta property=\"og:site_name\" content=\"Simple Programmer\" \/>\n<meta property=\"article:published_time\" content=\"2011-08-15T04:25:52+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-04-12T05:38:32+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/downloadsoftwarestore.com\/software_images\/75\/63\/00046375\/English_Dictionary_Toolbar-screenshot.jpg\" \/>\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=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/products\\\/careerguide#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/making-switch-refactorings-better-defaultable-dictionary\\\/\"},\"author\":{\"name\":\"John Sonmez\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#\\\/schema\\\/person\\\/1abc70edfb189184827740310672de67\"},\"headline\":\"Making Switch Refactorings Better &#8211; Defaultable Dictionary\",\"datePublished\":\"2011-08-15T04:25:52+00:00\",\"dateModified\":\"2018-04-12T05:38:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/making-switch-refactorings-better-defaultable-dictionary\\\/\"},\"wordCount\":359,\"commentCount\":17,\"image\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/products\\\/careerguide#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/downloadsoftwarestore.com\\\/software_images\\\/75\\\/63\\\/00046375\\\/English_Dictionary_Toolbar-screenshot.jpg\",\"articleSection\":[\"Algorithms\",\"C#\",\"GitHub\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/simpleprogrammer.com\\\/products\\\/careerguide#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/making-switch-refactorings-better-defaultable-dictionary\\\/\",\"url\":\"https:\\\/\\\/simpleprogrammer.com\\\/products\\\/careerguide\",\"name\":\"Making Switch Refactorings Better - Defaultable Dictionary - Simple Programmer\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/products\\\/careerguide#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/products\\\/careerguide#primaryimage\"},\"thumbnailUrl\":\"http:\\\/\\\/downloadsoftwarestore.com\\\/software_images\\\/75\\\/63\\\/00046375\\\/English_Dictionary_Toolbar-screenshot.jpg\",\"datePublished\":\"2011-08-15T04:25:52+00:00\",\"dateModified\":\"2018-04-12T05:38:32+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/#\\\/schema\\\/person\\\/1abc70edfb189184827740310672de67\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/products\\\/careerguide#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/simpleprogrammer.com\\\/products\\\/careerguide\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/products\\\/careerguide#primaryimage\",\"url\":\"http:\\\/\\\/downloadsoftwarestore.com\\\/software_images\\\/75\\\/63\\\/00046375\\\/English_Dictionary_Toolbar-screenshot.jpg\",\"contentUrl\":\"http:\\\/\\\/downloadsoftwarestore.com\\\/software_images\\\/75\\\/63\\\/00046375\\\/English_Dictionary_Toolbar-screenshot.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/simpleprogrammer.com\\\/products\\\/careerguide#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/simpleprogrammer.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Making Switch Refactorings Better &#8211; Defaultable Dictionary\"}]},{\"@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":"Making Switch Refactorings Better - Defaultable Dictionary - Simple Programmer","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\/products\/careerguide","og_locale":"en_US","og_type":"article","og_title":"Making Switch Refactorings Better - Defaultable Dictionary - Simple Programmer","og_description":"I\u2019ve written before on the idea of refactoring a switch to a Map or Dictionary. There is one major problem that I have been running into though.\u00a0 Switch statements and dictionaries are not functionally equivalent for one major reason\u2026 Switches allow for default I kept struggling with this when I would implement a dictionary to...","og_url":"https:\/\/simpleprogrammer.com\/products\/careerguide","og_site_name":"Simple Programmer","article_published_time":"2011-08-15T04:25:52+00:00","article_modified_time":"2018-04-12T05:38:32+00:00","og_image":[{"url":"http:\/\/downloadsoftwarestore.com\/software_images\/75\/63\/00046375\/English_Dictionary_Toolbar-screenshot.jpg","type":"","width":"","height":""}],"author":"John Sonmez","twitter_card":"summary_large_image","twitter_misc":{"Written by":"John Sonmez","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/simpleprogrammer.com\/products\/careerguide#article","isPartOf":{"@id":"https:\/\/simpleprogrammer.com\/making-switch-refactorings-better-defaultable-dictionary\/"},"author":{"name":"John Sonmez","@id":"https:\/\/simpleprogrammer.com\/#\/schema\/person\/1abc70edfb189184827740310672de67"},"headline":"Making Switch Refactorings Better &#8211; Defaultable Dictionary","datePublished":"2011-08-15T04:25:52+00:00","dateModified":"2018-04-12T05:38:32+00:00","mainEntityOfPage":{"@id":"https:\/\/simpleprogrammer.com\/making-switch-refactorings-better-defaultable-dictionary\/"},"wordCount":359,"commentCount":17,"image":{"@id":"https:\/\/simpleprogrammer.com\/products\/careerguide#primaryimage"},"thumbnailUrl":"http:\/\/downloadsoftwarestore.com\/software_images\/75\/63\/00046375\/English_Dictionary_Toolbar-screenshot.jpg","articleSection":["Algorithms","C#","GitHub"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/simpleprogrammer.com\/products\/careerguide#respond"]}]},{"@type":"WebPage","@id":"https:\/\/simpleprogrammer.com\/making-switch-refactorings-better-defaultable-dictionary\/","url":"https:\/\/simpleprogrammer.com\/products\/careerguide","name":"Making Switch Refactorings Better - Defaultable Dictionary - Simple Programmer","isPartOf":{"@id":"https:\/\/simpleprogrammer.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/simpleprogrammer.com\/products\/careerguide#primaryimage"},"image":{"@id":"https:\/\/simpleprogrammer.com\/products\/careerguide#primaryimage"},"thumbnailUrl":"http:\/\/downloadsoftwarestore.com\/software_images\/75\/63\/00046375\/English_Dictionary_Toolbar-screenshot.jpg","datePublished":"2011-08-15T04:25:52+00:00","dateModified":"2018-04-12T05:38:32+00:00","author":{"@id":"https:\/\/simpleprogrammer.com\/#\/schema\/person\/1abc70edfb189184827740310672de67"},"breadcrumb":{"@id":"https:\/\/simpleprogrammer.com\/products\/careerguide#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/simpleprogrammer.com\/products\/careerguide"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/simpleprogrammer.com\/products\/careerguide#primaryimage","url":"http:\/\/downloadsoftwarestore.com\/software_images\/75\/63\/00046375\/English_Dictionary_Toolbar-screenshot.jpg","contentUrl":"http:\/\/downloadsoftwarestore.com\/software_images\/75\/63\/00046375\/English_Dictionary_Toolbar-screenshot.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/simpleprogrammer.com\/products\/careerguide#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/simpleprogrammer.com\/"},{"@type":"ListItem","position":2,"name":"Making Switch Refactorings Better &#8211; Defaultable Dictionary"}]},{"@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\/1379","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=1379"}],"version-history":[{"count":0,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/posts\/1379\/revisions"}],"wp:attachment":[{"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/media?parent=1379"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/categories?post=1379"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/simpleprogrammer.com\/wp-json\/wp\/v2\/tags?post=1379"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}