<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.0.5" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/">
<channel>
	<title>Comments on: The Foreach Inconsistency</title>
	<link>http://www.terminally-incoherent.com/blog/2008/02/15/the-foreach-inconsistency/</link>
	<description>Utterly random, incoherent and disjointed rants and ramblings...</description>
	<pubDate>Thu, 20 Nov 2008 11:36:51 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.0.5</generator>

	<item>
		<title>by: Luke Maciak</title>
		<link>http://www.terminally-incoherent.com/blog/2008/02/15/the-foreach-inconsistency/#comment-8133</link>
		<pubDate>Sat, 16 Feb 2008 02:10:00 +0000</pubDate>
		<guid>http://www.terminally-incoherent.com/blog/2008/02/15/the-foreach-inconsistency/#comment-8133</guid>
					<description>LOL! The street language cracked me up!</description>
		<content:encoded><![CDATA[<p>LOL! The street language cracked me up!
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Ian Clifton</title>
		<link>http://www.terminally-incoherent.com/blog/2008/02/15/the-foreach-inconsistency/#comment-8132</link>
		<pubDate>Fri, 15 Feb 2008 21:22:04 +0000</pubDate>
		<guid>http://www.terminally-incoherent.com/blog/2008/02/15/the-foreach-inconsistency/#comment-8132</guid>
					<description>Your PHP example works for me, but you just broke all my scripts ;)  Actually, they could switch to that and allow both forms based on the presence of "in" or "as" and avoid breaking anything.

I agree, it's really just about consistency, but each language somehow wants to prove itself, as you were saying.  The "as" word in PHP makes some sense in that foreach loops are creating new variables and setting their values according to the array (unless you pass by reference).  Essentially, you are dealing with the value as a new variable.  It wouldn't make (as much) sense in a context where you are always dealing with references.

I like every array and object having an "each" method.  Maybe it's not quite as readable (in English), but it makes sense in coding terms.  Perhaps it can use "as" (e.g., "array.each as element") and be as simple as that.  I think our biggest problem is that we base programming on English and English is inconsistent.

&lt;pre lang="street"&gt;while array be havin parts
    # do stuff&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Your PHP example works for me, but you just broke all my scripts <img src="http://www.terminally-incoherent.com/blog/wp-includes/images/smilies/icon_wink.gif" alt=")" class="wp-smiley" />   Actually, they could switch to that and allow both forms based on the presence of &#8220;in&#8221; or &#8220;as&#8221; and avoid breaking anything.</p>
<p>I agree, it&#8217;s really just about consistency, but each language somehow wants to prove itself, as you were saying.  The &#8220;as&#8221; word in PHP makes some sense in that foreach loops are creating new variables and setting their values according to the array (unless you pass by reference).  Essentially, you are dealing with the value as a new variable.  It wouldn&#8217;t make (as much) sense in a context where you are always dealing with references.</p>
<p>I like every array and object having an &#8220;each&#8221; method.  Maybe it&#8217;s not quite as readable (in English), but it makes sense in coding terms.  Perhaps it can use &#8220;as&#8221; (e.g., &#8220;array.each as element&#8221;) and be as simple as that.  I think our biggest problem is that we base programming on English and English is inconsistent.</p>

<div class="wp_syntax"><div class="code"><pre>while array be havin parts
    # do stuff</pre></div></div>

]]></content:encoded>
				</item>
	<item>
		<title>by: Luke Maciak</title>
		<link>http://www.terminally-incoherent.com/blog/2008/02/15/the-foreach-inconsistency/#comment-8130</link>
		<pubDate>Fri, 15 Feb 2008 20:13:14 +0000</pubDate>
		<guid>http://www.terminally-incoherent.com/blog/2008/02/15/the-foreach-inconsistency/#comment-8130</guid>
					<description>Well, PHP could easily do:

&lt;pre lang="php"&gt;foreach ($key=&gt;$element in $array)&lt;/pre&gt;

Which could be read as "for each key/element pair in array" or something like that. Again, I'm not saying this is the best possible syntax. I'm just saying I like that one. PHP and Perl examples are perfectly logical but not as readable. 

Ruby example is elegant (each being a method) but it suffers from the "WTF is this shit" problem - because unless you used Smalltalk you probably won't recognize the &#124;element&#124; notation. But arguably it is the smarter, cooler way of doing things:

&lt;pre lang="ruby"&gt;(1..10).each {&#124;i&#124; puts i}&lt;/pre&gt;

Naturally you could still do:

&lt;pre lang="ruby"&gt;for i in (1..10) puts i end&lt;/pre&gt;

But why would you want to do that if each collection type object has an each method defined. 

I'm not saying every language should conform to the same syntax for this type of loop. I'm just looking for some sort of consistency where possible.

If we would want to follow the PHP like convention of using the &lt;em&gt;as&lt;/em&gt; keyword I wouldn't cry either. :) As long as it is consistent I can learn to live with it.</description>
		<content:encoded><![CDATA[<p>Well, PHP could easily do:</p>

<div class="wp_syntax"><div class="code"><pre class="php"><span style="color: #b1b100;">foreach</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$key</span>=&gt;<span style="color: #0000ff;">$element</span> in <span style="color: #0000ff;">$array</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Which could be read as &#8220;for each key/element pair in array&#8221; or something like that. Again, I&#8217;m not saying this is the best possible syntax. I&#8217;m just saying I like that one. PHP and Perl examples are perfectly logical but not as readable. </p>
<p>Ruby example is elegant (each being a method) but it suffers from the &#8220;WTF is this shit&#8221; problem - because unless you used Smalltalk you probably won&#8217;t recognize the |element| notation. But arguably it is the smarter, cooler way of doing things:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span>..<span style="color:#006666;">10</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span>|i| <span style="color:#CC0066; font-weight:bold;">puts</span> i<span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>Naturally you could still do:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">for</span> i <span style="color:#9966CC; font-weight:bold;">in</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">1</span>..<span style="color:#006666;">10</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#CC0066; font-weight:bold;">puts</span> i <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>But why would you want to do that if each collection type object has an each method defined. </p>
<p>I&#8217;m not saying every language should conform to the same syntax for this type of loop. I&#8217;m just looking for some sort of consistency where possible.</p>
<p>If we would want to follow the PHP like convention of using the <em>as</em> keyword I wouldn&#8217;t cry either. <img src="http://www.terminally-incoherent.com/blog/wp-includes/images/smilies/icon_smile.gif" alt=")" class="wp-smiley" />  As long as it is consistent I can learn to live with it.
</p>
]]></content:encoded>
				</item>
	<item>
		<title>by: Ian Clifton</title>
		<link>http://www.terminally-incoherent.com/blog/2008/02/15/the-foreach-inconsistency/#comment-8128</link>
		<pubDate>Fri, 15 Feb 2008 18:42:21 +0000</pubDate>
		<guid>http://www.terminally-incoherent.com/blog/2008/02/15/the-foreach-inconsistency/#comment-8128</guid>
					<description>A syntax that reads "For each element in array" makes the most sense for readability when dealing with a simple example.  The problem is that the foreach construct might be able to do more than that (or that the concept of an "array" is sometimes different from language to language).

For instance: foreach ($array as $key =&#62; $value) with PHP would maybe be translated as "For each (element in) $array (use it) as $key with $value" or something like that.  The good thing is that the format is consistent with how arrays are assigned with key/value pairs.  I'm not sure if there is a way of writing that in code in a way that can logically/easily be read as English (without making it excessively verbose).

The other thought is whether the goal is really to make it "readable" in the English sense or readable in a different, logical sense.  For instance, think of a nested foreach.  In a language that specifies array then element, you could essentially say that you are taking an array, getting an element from that (which is also an array), and getting an element from it.  In other words, least specific to most specific.  If element is specified before array, you end up taking an element (which is also an array) from an array and then taking an element from the first element (or is it first array?).

I'm not sure if I am making any sense, so maybe we should take it away from coding.  Giving a thief directions: "In the parking garage, locate every car.  In each car, open the ashtray.  In each ashtray take the money."  The alternative: "Locate every car in the parking garage.  Open the ashtray in each car.  Take the money in each ashtray."</description>
		<content:encoded><![CDATA[<p>A syntax that reads &#8220;For each element in array&#8221; makes the most sense for readability when dealing with a simple example.  The problem is that the foreach construct might be able to do more than that (or that the concept of an &#8220;array&#8221; is sometimes different from language to language).</p>
<p>For instance: foreach ($array as $key =&gt; $value) with PHP would maybe be translated as &#8220;For each (element in) $array (use it) as $key with $value&#8221; or something like that.  The good thing is that the format is consistent with how arrays are assigned with key/value pairs.  I&#8217;m not sure if there is a way of writing that in code in a way that can logically/easily be read as English (without making it excessively verbose).</p>
<p>The other thought is whether the goal is really to make it &#8220;readable&#8221; in the English sense or readable in a different, logical sense.  For instance, think of a nested foreach.  In a language that specifies array then element, you could essentially say that you are taking an array, getting an element from that (which is also an array), and getting an element from it.  In other words, least specific to most specific.  If element is specified before array, you end up taking an element (which is also an array) from an array and then taking an element from the first element (or is it first array?).</p>
<p>I&#8217;m not sure if I am making any sense, so maybe we should take it away from coding.  Giving a thief directions: &#8220;In the parking garage, locate every car.  In each car, open the ashtray.  In each ashtray take the money.&#8221;  The alternative: &#8220;Locate every car in the parking garage.  Open the ashtray in each car.  Take the money in each ashtray.&#8221;
</p>
]]></content:encoded>
				</item>
</channel>
</rss>

<!-- Dynamic Page Served (once) in 1.350 seconds -->
