{"id":55,"date":"2009-01-06T00:58:20","date_gmt":"2009-01-06T05:58:20","guid":{"rendered":"http:\/\/factormystic.net\/blog\/?p=55"},"modified":"2009-03-04T00:31:06","modified_gmt":"2009-03-04T05:31:06","slug":"a-quick-lesson-in-overengineering","status":"publish","type":"post","link":"https:\/\/factormystic.net\/blog\/a-quick-lesson-in-overengineering","title":{"rendered":"A Quick Lesson In Overengineering"},"content":{"rendered":"<p>I was tapping out a quick for-fun project where I wanted to iterate through all the letters in the alphabet.<\/p>\n<p>[sourcecode language=&#8221;csharp&#8221;]<br \/>\n\/\/other logic<br \/>\nforeach(char letter in \/\/hmm&#8230;<br \/>\n[\/sourcecode]<\/p>\n<p>I didn&#8217;t really want to manually initialize an array <code>new char[]{\"A\", \"B\", \"C\"<\/code> and I recalled how other languages have shortcuts for building lists, like <code>[\"A\"..\"Z\"]<\/code>. It&#8217;s called &#8220;List Comprehension&#8221; and it&#8217;s available in a lot of <a href=\"http:\/\/dutherenverseauborddelatable.wordpress.com\/downloads\/comprehension-for-ocaml\/\">cool languages<\/a>.<\/p>\n<p>So I began to think about how to accomplish this in C#. The <code>..<\/code> syntax obviously didn&#8217;t work. <a href=\"http:\/\/www.google.com\/search?q=c%23+list+comprehension\">Googling the topic<\/a> brought me to <a href=\"http:\/\/en.wikipedia.org\/wiki\/List_comprehension\">Wikipedia<\/a>, which lists a couple of examples using <code>Enumerable.Range<\/code>.<\/p>\n<p>&#8220;Okay,&#8221; I mused, &#8220;I can build on that easily!&#8221; <code>Enumerable.Range(65, 26)<\/code> will be the starting point since &#8220;A&#8221; is ASCII 65, and there are 26 letters in the alphabet. If I can just convert each one of these to a <code>char<\/code>, I&#8217;m golden. The function I found was <code>ConvertAll<\/code>, but that&#8217;s a LINQ extension method for the <code>List&lt;T&gt;<\/code> type. No problem, all I need is <code>Enumerable.Range(65, 26).ToList().ConvertAll<\/code>. It&#8217;s simple to convert an <code>int<\/code> to its <code>char<\/code> by casting, so my implementation of List Comprehension in C# for the alphabet was<\/p>\n<p>[sourcecode language=&#8221;csharp&#8221;]<br \/>\nEnumerable.Range(65, 26).ToList().ConvertAll(delegate(int value) { return (char)value; })<br \/>\n[\/sourcecode]\u00a0<\/p>\n<p>Check it out, it works great, what a clever programmer I am!<\/p>\n<p>[sourcecode language=&#8221;csharp&#8221;]<br \/>\nforeach(char letter in Enumerable.Range(65, 26).ToList().ConvertAll(delegate(int value) { return (char)value; }))<br \/>\n{<br \/>\n\/\/breakpoint here to confirm<br \/>\n}<br \/>\n[\/sourcecode]<\/p>\n<p>\u00a0<\/p>\n<p>And now, the moral of the story: In C#, the <code>String<\/code> class implements <code>IEnumerable&lt;char&gt;<\/code>. Which means that all I needed to do was write:<\/p>\n<p>[sourcecode language=&#8221;csharp&#8221;]<br \/>\nforeach(char letter in &#8220;ABCDEFGHIJKLMNOPQRSTUVWXYZ&#8221;)<br \/>\n{<br \/>\n\/\/how about that<br \/>\n}<br \/>\n[\/sourcecode]<br \/>\nIt&#8217;s so much simpler, shorter, and it&#8217;s instantly understandable. Learn from me, don&#8217;t be an overengineer!<\/p>\n<p>\u00a0<\/p>\n<p>P.S. I could have saved myself the embarassment if I had remembered that\u00a0<a href=\"https:\/\/stackoverflow.fogbugz.com\/default.asp?W29014\">the only reasonable numbers you should see in source code are 0, 1, and 2<\/a>.<\/p>\n<p>\u00a0<\/p>\n<p>Special thanks to the #cobol crew for showing me the error of my ways.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was tapping out a quick for-fun project where I wanted to iterate through all the letters in the alphabet. [sourcecode language=&#8221;csharp&#8221;] \/\/other logic foreach(char letter in \/\/hmm&#8230; [\/sourcecode] I didn&#8217;t really want to manually initialize an array new char[]{&#8220;A&#8221;, &#8220;B&#8221;, &#8220;C&#8221; and I recalled how other languages have shortcuts for building lists, like [&#8220;A&#8221;..&#8221;Z&#8221;]. [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-55","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/factormystic.net\/blog\/wp-json\/wp\/v2\/posts\/55","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/factormystic.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/factormystic.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/factormystic.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/factormystic.net\/blog\/wp-json\/wp\/v2\/comments?post=55"}],"version-history":[{"count":8,"href":"https:\/\/factormystic.net\/blog\/wp-json\/wp\/v2\/posts\/55\/revisions"}],"predecessor-version":[{"id":62,"href":"https:\/\/factormystic.net\/blog\/wp-json\/wp\/v2\/posts\/55\/revisions\/62"}],"wp:attachment":[{"href":"https:\/\/factormystic.net\/blog\/wp-json\/wp\/v2\/media?parent=55"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/factormystic.net\/blog\/wp-json\/wp\/v2\/categories?post=55"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/factormystic.net\/blog\/wp-json\/wp\/v2\/tags?post=55"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}