{"id":23,"date":"2008-04-07T20:49:46","date_gmt":"2008-04-08T01:49:46","guid":{"rendered":"http:\/\/vgable.com\/blog\/2008\/04\/07\/foreach-for-the-win\/"},"modified":"2008-04-07T20:53:58","modified_gmt":"2008-04-08T01:53:58","slug":"foreach-for-the-win","status":"publish","type":"post","link":"https:\/\/vgable.com\/blog\/2008\/04\/07\/foreach-for-the-win\/","title":{"rendered":"foreach For The Win"},"content":{"rendered":"<p>I love foreach.  What I really mean is that I really like dead-simple ways to iterate over the items in a container, one at a time.  It goes beyond syntactic sugar; making errors harder to write, and easier to spot.<\/p>\n<p>I&#8217;m using the term &#8220;foreach&#8221;, because the first time I realized the utility of simple iteration was seeing <a href=\"http:\/\/mjtsai.com\/blog\">Michael Tsai&#8217;s foreach macro<\/a>.  I instantly fell in love with the &#8216;keyword&#8217;, because it greatly simplifies iterating over items in a Cocoa container. My reaction was so strong, because the (then current) Cocoa iteration idiom I was using was so bad.  As <a href=\"http:\/\/rentzsch.com\/papers\/improvingCocoaObjCEnumeration\">Jonathan &#8216;Wolf&#8217; Rentzsch says:<\/a><\/p>\n<blockquote><p>\nI have a number of issues with enumeration in Cocoa\/ObjC. Indeed, I have a problem with <b>every one<\/b> of the three lines necessary in the standard idiom. It even goes beyond that &#8212; I have an problem with the very fact it&#8217;s <b>three lines of code<\/b> versus <b>one<\/b>.\n<\/p><\/blockquote>\n<p>Objective-C 2.0 (Leopard and later) introduced <a href=\"http:\/\/developer.apple.com\/documentation\/Cocoa\/Conceptual\/ObjectiveC\/Articles\/chapter_7_section_1.html\">Fast Enumeration<\/a>. It&#8217;s a better way of handling enumeration then Michael Tsai&#8217;s macro, but if you are stuck using Objective-C 1.0, then I highly recommend using <a href=\"http:\/\/mjtsai.com\/blog\/2006\/07\/15\/cocoa-foreach-macro\/\">his foreach<\/a> macro.<\/p>\n<p>I do not like the C++ stl <a href=\"http:\/\/www.sgi.com\/tech\/stl\/for_each.html\">for_each<\/a> idiom.  It&#8217;s not simple enough.<\/p>\n<p>To explain what&#8217;s wrong with <code>for_each<\/code> I should explain what a &#8220;foreach&#8221; <em>should<\/em> look like.  It should be a two-argument construct: &#8220;<code>foreach (item, container)<\/code>&#8220;, and no more. <code>container<\/code> should be evaluated only once. Syntax details aren&#8217;t terribly important, as long as they make sense.<br \/>\n<code>foreach(child, naughtyList)<\/code><br \/>\n<code>iterate(child, naughtyList)<\/code><br \/>\n<code>ForEach(String child, naughtyList)<\/code><br \/>\n<code><a href=\"http:\/\/www.ibiblio.org\/g2swap\/byteofpython\/read\/for-loop.html\">for child in naughyList:<\/code><\/a><br \/>\n<code><a href=\"http:\/\/developer.apple.com\/documentation\/Cocoa\/Conceptual\/ObjectiveC\/Articles\/chapter_7_section_1.html\">for(NSString *child in naughtyList)<\/code><\/a><br \/>\nAre all fine constructs.<\/p>\n<p>The most obvious benefits of a foreach is that it&#8217;s less code, easier to write, and less work to read.  Implemented correctly, it also makes bugs harder to write &#8212; mostly because it minimizes side effects in the loop construct.  For example, do you see the bug in the following code?<\/p>\n<p><code>for( list&lt;shared_ptr&lt;const Media&gt; &gt;::iterator it = selectedMedias->selectedMediaList().begin(); it != selectedMedias-&gt;selectedMediaList().end(); ++it )<\/code><\/p>\n<p>I didn&#8217;t; and it&#8217;s kind of a trick question. <code>->selectedMediaList()<\/code> isn&#8217;t an accessor function &#8212; it constructs a new list every time it is called.  So <code>selectedMediaList() != selectedMediaList()<\/code> because it returns a different list each time.  The loop never terminates because <code>it<\/code> will never equal <code>end()<\/code>, since it is the end of a <em>different<\/em> list.  But you have no way of knowing this without knowing details of what <code>selectedMediaList()<\/code> does.<\/p>\n<p>Using <a href=\"http:\/\/www.boost.org\/doc\/html\/foreach.html\">BOOST_FOREACH<\/a> avoids the problem:<br \/>\n<code><a href=\"http:\/\/www.boost.org\/doc\/html\/foreach.html\">BOOST_FOREACH<\/a>(shared_ptr&lt;const CSMediaLib::Media&gt; media, selectedMedias->selectedMediaList())<\/code><br \/>\nworks regardless of how <code>selectedMediaList()<\/code> is implemented, because it is only evaluated once.  It&#8217;s also easier to write, and to read.  I haven&#8217;t used  <a href=\"http:\/\/www.boost.org\/doc\/html\/foreach.html\">BOOST_FOREACH<\/a> much, but it&#8217;s been totally positive so far. (Yes, the name is ugly, but that&#8217;s not important).<\/p>\n<p>Loops are a staple of programming.  Simplifying and error-proofing the most common kind of loop is a huge productivity win.   foreach <a href=\"http:\/\/mjtsai.com\/blog\"> regardless <\/a> <a href=\"http:\/\/developer.apple.com\/documentation\/Cocoa\/Conceptual\/ObjectiveC\/Articles\/chapter_7_section_1.html\"> of<\/a> <a href=\"http:\/\/www.ibiblio.org\/g2swap\/byteofpython\/read\/for-loop.html\"> it&#8217;s<\/a> <a href=\"http:\/\/www.boost.org\/doc\/html\/foreach.html\"> flavor<\/a>, <a href=\"http:\/\/perl.about.com\/od\/perltutorials\/a\/foreachloop.htm\">is<\/a> worth a try.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I love foreach. What I really mean is that I really like dead-simple ways to iterate over the items in a container, one at a time. It goes beyond syntactic sugar; making errors harder to write, and easier to spot. I&#8217;m using the term &#8220;foreach&#8221;, because the first time I realized the utility of simple [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,6,12,5,4,11],"tags":[],"class_list":["post-23","post","type-post","status-publish","format-standard","hentry","category-c","category-cocoa","category-design","category-objective-c","category-programming","category-research"],"_links":{"self":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/23","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/comments?post=23"}],"version-history":[{"count":0,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/posts\/23\/revisions"}],"wp:attachment":[{"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/media?parent=23"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/categories?post=23"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/vgable.com\/blog\/wp-json\/wp\/v2\/tags?post=23"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}