Comments on: The Foreach Inconsistency http://www.terminally-incoherent.com/blog/2008/02/15/the-foreach-inconsistency/ I will not fix your computer. Tue, 04 Aug 2020 22:34:33 +0000 hourly 1 https://wordpress.org/?v=4.7.26 By: Luke Maciak http://www.terminally-incoherent.com/blog/2008/02/15/the-foreach-inconsistency/#comment-8133 Sat, 16 Feb 2008 02:10:00 +0000 http://www.terminally-incoherent.com/blog/2008/02/15/the-foreach-inconsistency/#comment-8133

LOL! The street language cracked me up!

Reply  |  Quote
]]>
By: Ian Clifton http://www.terminally-incoherent.com/blog/2008/02/15/the-foreach-inconsistency/#comment-8132 Fri, 15 Feb 2008 21:22:04 +0000 http://www.terminally-incoherent.com/blog/2008/02/15/the-foreach-inconsistency/#comment-8132

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.

while array be havin parts
    # do stuff
Reply  |  Quote
]]>
By: Luke Maciak http://www.terminally-incoherent.com/blog/2008/02/15/the-foreach-inconsistency/#comment-8130 Fri, 15 Feb 2008 20:13:14 +0000 http://www.terminally-incoherent.com/blog/2008/02/15/the-foreach-inconsistency/#comment-8130

Well, PHP could easily do:

foreach ($key=>$element in $array)

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 |element| notation. But arguably it is the smarter, cooler way of doing things:

(1..10).each {|i| puts i}

Naturally you could still do:

for i in (1..10) puts i end

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 as keyword I wouldn’t cry either. :) As long as it is consistent I can learn to live with it.

Reply  |  Quote
]]>
By: Ian Clifton http://www.terminally-incoherent.com/blog/2008/02/15/the-foreach-inconsistency/#comment-8128 Fri, 15 Feb 2008 18:42:21 +0000 http://www.terminally-incoherent.com/blog/2008/02/15/the-foreach-inconsistency/#comment-8128

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 => $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.”

Reply  |  Quote
]]>