{"id":462,"date":"2011-11-09T12:40:53","date_gmt":"2011-11-09T12:40:53","guid":{"rendered":"http:\/\/www.themissingdocs.net\/wordpress\/?p=462"},"modified":"2011-11-09T12:40:53","modified_gmt":"2011-11-09T12:40:53","slug":"list-contains-considered-harmful","status":"publish","type":"post","link":"https:\/\/www.themissingdocs.net\/?p=462","title":{"rendered":"List.Contains Considered Harmful"},"content":{"rendered":"<p>So this post isn&#8217;t going to add much value to the universe &#8211; it is just a rant.<\/p>\n<p>The number of times where I have seen the word &#8216;Contains&#8217; and cringed is far, far more than I would have ever expected.\u00a0 I can&#8217;t say the same for IndexOf, but it certainly carries a similar risk.<\/p>\n<pre>foreach (int newValue in input)\n    if (!list.Contains(newValue))\n        list.Add(newValue);<\/pre>\n<p>This little snippet and similar versions are the most common cringe worthy occasion, but there are certainly plenty of others.\u00a0 Of course it works just fine, until input contains a hundred thousand entries, or more (or less&#8230;).<\/p>\n<p>I think a major part of the problem is that Contains is so innocuous, just one little word, so easy.\u00a0 If we forced everyone to write the double nested loop, maybe there would be less cringe-worthy moments.<\/p>\n<p>Most of the time this is a case of poorly chosen data structure &#8211; they don&#8217;t actually want a list, they want a set.\u00a0 SortedSet\/HashSet either one will probably do.\u00a0 If that isn&#8217;t it they might sometimes want something like multiset from c++, or Dictionary&lt;T, int&gt; aka a counting set.\u00a0 Rarely they may even want an OrderedSet &#8211; elements have specific (non-sorted) order, no repeats and hence want a fast contains check &#8211; although I&#8217;ve never seen such a collection in use.\u00a0 (I see a few future additions to TMD.Algo&#8230;)<\/p>\n<p>Sometimes changing the data structure is not an option. (Legacy code, I am looking at you!)\u00a0 While this is not a happy place to be when it comes to performance, temporarily placing the data into the correct data structure during the important code points is still a huge win in many scenarios.<\/p>\n<p>But simply knowing the above is probably not enough, I suspect people are going to fall into the same trap time and time again.\u00a0 Maybe I should open a Connect ticket asking Contains to be renamed CheckEachExistingEntryForTheGivenInput&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So this post isn&#8217;t going to add much value to the universe &#8211; it is just a rant. The number of times where I have seen the word &#8216;Contains&#8217; and cringed is far, far more than I would have ever expected.\u00a0 I can&#8217;t say the same for IndexOf, but it certainly carries a similar risk. &hellip; <a href=\"https:\/\/www.themissingdocs.net\/?p=462\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">List.Contains Considered Harmful<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-462","post","type-post","status-publish","format-standard","hentry","category-net-stuff"],"_links":{"self":[{"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=\/wp\/v2\/posts\/462","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=462"}],"version-history":[{"count":0,"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=\/wp\/v2\/posts\/462\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=462"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=462"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.themissingdocs.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=462"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}