{"id":8793,"date":"2011-12-27T07:00:00","date_gmt":"2011-12-27T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2011\/12\/27\/introducing-the-for-if-anti-pattern\/"},"modified":"2011-12-27T07:00:00","modified_gmt":"2011-12-27T07:00:00","slug":"introducing-the-for-if-anti-pattern","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20111227-00\/?p=8793","title":{"rendered":"Introducing the for-if anti-pattern"},"content":{"rendered":"<p>\nOver the years, I&#8217;ve seen a bunch of coding anti-patterns.\nI figured maybe I&#8217;ll share a few.\n<\/p>\n<p>\nToday, I&#8217;ll introduce\nwhat I&#8217;m calling the <i>for-if<\/i> anti-pattern,\nalso known as\n&#8220;<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2010\/04\/27\/10002865.aspx\">We&#8217;ll sell you the whole seat, but you&#8217;ll only need the edge<\/a>.&#8221;\nThis is a special case of the <i>for-case<\/i> anti-pattern, where\nall but one of the cases is null.\n<\/p>\n<pre>\nfor (int i = 0; i &lt; 100; i++) {\n  if (i == 42) { do_something(i); }\n}\n<\/pre>\n<p>\nThis can naturally be simplified to\n<\/p>\n<pre>\ndo_something(42);\n<\/pre>\n<p>\nThe <i>for-if<\/i> anti-pattern arises in many forms.\nFor example:\n<\/p>\n<pre>\nforeach (string filename in Directory.GetFiles(\".\"))\n{\n    if (filename.Equals(\"desktop.ini\", <a HREF=\"http:\/\/blogs.msdn.com\/b\/michkap\/archive\/2005\/09\/22\/472596.aspx\">StringComparison.OrdinalIgnoreCase<\/a>))\n    {\n        return new StreamReader(filename);\n    }\n}\n<\/pre>\n<p>\nThis enumerates all the files in a directory looking for a specific one,\nand if it&#8217;s found, it returns a stream on it.\nThe slightly-less-crazy version would be\n<\/p>\n<pre>\nif (File.Exists(\"desktop.ini\"))\n{\n    return new StreamReader(\"desktop.ini\");\n}\n<\/pre>\n<p>\nNote that both versions of the code fragment have the same race condition:\nIf the file <code>desktop.ini<\/code> initially exists but gets deleted\nbefore you get around to creating a new <code>Stream&shy;Reader<\/code>,\nyou will get a <code>File&shy;Not&shy;Found&shy;Exception<\/code>.\n<\/p>\n<p>\nOne final example:\n<\/p>\n<pre>\nforeach (object o in hashtable.Keys)\n{\n    if (o == \"target\") return hashtable[\"target\"];\n}\n<\/pre>\n<p>\nAlso known as\n<\/p>\n<pre>\nreturn hashtable[\"target\"];\n<\/pre>\n<p>\nI bet these people hate going to the library to get a book by title,\nbecause it takes so darn long:\nThey go up to the librarian and say,\n&#8220;Please give me all the books you have,&#8221;\nand then they fill up their cart with thousands of books,\nthen sit in the corner saying,\n&#8220;Nope, the title of this book is wrong.\nNope, not that one either.\nStill the wrong title.\nHow about this book?\nNope, not this one either.\nMan, this is taking <i>forever<\/i>&#8230;&#8221;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Over the years, I&#8217;ve seen a bunch of coding anti-patterns. I figured maybe I&#8217;ll share a few. Today, I&#8217;ll introduce what I&#8217;m calling the for-if anti-pattern, also known as &#8220;We&#8217;ll sell you the whole seat, but you&#8217;ll only need the edge.&#8221; This is a special case of the for-case anti-pattern, where all but one of [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-8793","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Over the years, I&#8217;ve seen a bunch of coding anti-patterns. I figured maybe I&#8217;ll share a few. Today, I&#8217;ll introduce what I&#8217;m calling the for-if anti-pattern, also known as &#8220;We&#8217;ll sell you the whole seat, but you&#8217;ll only need the edge.&#8221; This is a special case of the for-case anti-pattern, where all but one of [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/8793","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=8793"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/8793\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=8793"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=8793"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=8793"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}