2

Note: topic is NOT about the for tag.


each, foreach and for-in-loop are very similar:

language constructs to iterate over a collection/array/list

but with different naming depending on the language:

  • each in jQuery
  • foreach in C# and PHP
  • for in in JavaScript, Objective-C, Swift, and AWK
  • for : in Java and C++ (NOT the for;; loop, there is a colon here)

The current description of foreach even claims to encompass the for in usages. And that is a source of confusion that could be addressed.

Idea one

Should we have (391 q) be a synonym of (17,441 q)? What about (3,286 q)?

Idea two

Should we clearly dissociate foreach and for in usage/tags, by changing the description of foreach and retagging JavaScript, Objective-C, Swift, and AWK usages to ?

Which way to go?


Note: topic is absolutely NOT about the for tag

17
  • 1
    I think if you make only one tag this might confuse people who are working in a language where it's used differently; so I'm not for Idea 1... (And no, people often don't read tag descriptions and differentiate (my personal pet is word vs. ms-word. ) And ForEach does work for VB-languages, so I'm OK with Idea 2 - but I'm not sure it's needed. Commented Apr 2, 2018 at 11:39
  • 1
    The concept behind these tags is so similar yet different that I don't see any practical usage for any of them. Would you be able to close a [foreach] question against another [foreach] question? Are any two questions on any of these tags similar to each other other than somewhere they have a keyword which happens to be the name of any of these tags?
    – Braiam
    Commented Apr 2, 2018 at 12:11
  • 3
    Are any of those loop tags actually useful?
    – ohmu
    Commented Apr 2, 2018 at 12:47
  • 4
    I could do without all these syntactic tags like foreach or if-statement. Commented Apr 2, 2018 at 14:06
  • @CindyMeister On the other hand, folks who don't read tag descriptions are also unlikely to proofread their questions after posting (my own pet peeve here is map-function questions being mistagged as map, which becomes dictionary). That being so, I think synonimising wouldn't be significantly confusing in this manner.
    – duplode
    Commented Apr 2, 2018 at 14:55
  • For...in and for...of loops have some very (extremely) specific behaviour that should not be conflated with other language constructs or iteration devices thereof.
    – user4639281
    Commented Apr 2, 2018 at 22:04
  • @TinyGiant As far as my answer is concerned, I had overlooked the JavaScript for...in the question -- it indeed is different enough from the other ones to not be lumped with them in a single tag. I will adjust the answer accordingly. (As for for...of, I'd it is similar enough to the foreach variants seen elsewhere.)
    – duplode
    Commented Apr 3, 2018 at 4:28
  • @duplode what about a question asking why const declarations in the definition of a for...of loop are evaluated outside of the loop in Firefox when the spec states that it should be evaluated inside the loop? (or some similar question on the topic) This is a specific question about the for...of loop construct in JavaScript and would make no sense if tagged foreach (there's no such thing as foreach in the standard JavaScript API's).
    – user4639281
    Commented Apr 3, 2018 at 14:20
  • Note that this is not the same for all for loops. Last I checked, Firefox handles const declarations in definition statements for for...in and for ;;; loops correctly.
    – user4639281
    Commented Apr 3, 2018 at 14:23
  • 1
    @TinyGiant [1/2] (1) According to the meaning I'm proposing, [foreach] wouldn't refer to any specific language construct, but to a general kind of loop, and so it would be appropriate for such a question. I realise it may be awkward to have it named after the C# feature, and not the JS one, but, unless we make up a language agnostic term, some tag community will have to live with it.
    – duplode
    Commented Apr 3, 2018 at 15:39
  • 1
    I agree with @cpburnz and MichaelKohl, I'm not sure those tags are even relevant. Can you "be an expert in foreach"? Sounds weird Commented Apr 3, 2018 at 21:24
  • 2
    @CamiloTerevinto the "expert" argument is nonsensical and has never been an appropriate reason to remove a tag. Tags are for connecting people searching for information to that information as efficiently as possible first, and for connecting answerers with questions to answer second. What good is an unsearchable heap of expertly answered questions?
    – user4639281
    Commented Apr 4, 2018 at 14:57
  • 1
    @TinyGiant So you are claiming that it's going to be "so much faster" for users to search for "c# foreach" or "javascript for-in" questions? I highly doubt there are so many questions for that search to be relevant at all, besides simple "how do I loop over a collection..." questions Commented Apr 4, 2018 at 15:02
  • 2
    @TinyGiant "the logical conclusion to your argument would be to have one loop tag and call it a day" -- Not quite. I'd still keep foreach-esque loops separate from explicitly condition-driven loops (the classic while, for;;, etc.). You have a point about searchability, assuming we can get people to actually use, say, [swift][for-in-loop] in the search box. As I said before, I feel it would be worth it for you to shape that into an answer.
    – duplode
    Commented Apr 4, 2018 at 15:03
  • 1
    @CamiloTerevinto JavaScript does not have such a thing as a foreach loop. How would anyone know to search using the foreach tag? It actually makes 0 sense in the JavaScript context from the standpoint of searchability. Sure it makes sense if you dont care, but others do. If we're going to make such a drastic change, it should make sense for all involved. Why take something that works and break it?
    – user4639281
    Commented Apr 4, 2018 at 15:18

2 Answers 2

3

Like Michael Kohl, I believe purely syntactic tags are by and large superfluous. In the case of , though, there is a bit more than mere syntax involved, to the extent it is relevant to discuss iteration over a data structure as a separate subconcept of iteration in general. The tag wiki, language agnostic as it is, fits that interpretation:

foreach is a looping construct that executes a given piece of code for each element in a list/collection/array. In contrast to a for loop, the foreach loop doesn't require the coder to maintain a counter variable to avoid off-by-one (fencepost) bugs. [...]

I believe that is enough to justify the existence of , though those of you with more experience in handling newbie questions in OO languages might be in a better position to judge.

Given this meaning of , it would be appropriate to get rid of any variants of the tag that only differ by syntax or choice of language by synonimising (i.e. option #1 -- and not option #2, which would just give us yet another syntactic tag). Let's consider the tags mentioned here one by one:

  • should become a synonym.

  • should not become a synonym, as JavaScript for..in loops are unlike the typical foreach loops we are discussing here. It is a tricky tag to handle, as there are quite a few languages which, unlike JavaScript use for...in as their typical foreach. We would need to decide whether to retag in such cases, which seem to constitute about a third of the tag, as the "Related Tags" counts in the sidebar suggest. Fortunately, it is a relatively small tag, with 391 questions as of now.

  • , on the other hand, should become a synonym, as it is about the JavaScript spelling of the typical foreach. It is a very small tag, with only 28 questions as of now.

P.S.: For the sake of convenience, here is a SEDE query for quickly viewing related tag counts (i.e. given a tag, how many questions also have another tag). It can be useful to evaluate how usage of the tags being discussed here is spread across language tags.

6
  • 2
    What about Swift which has both for in and foreach? They enumerate the same things at the same speed. Just, the former accepts a break and the later is unbreakable.
    – anon
    Commented Apr 4, 2018 at 0:10
  • @Cœur According to what I'm suggesting here, both should be tagged as [foreach], as they fall under the same concept -- the difference between them not being enough to justify separate tags. As a side effect, either the Swift folks would have to handle a small amount of chronic mistagging (about one question every three weeks, on average), or the JavaScript folks would have to have their [for-in-loop] tag renamed to something else. See also: my reply to Tiny Giant in the comments to the question.
    – duplode
    Commented Apr 4, 2018 at 2:56
  • Why not use a conceptual name for the tag instead of a syntax-evoking name? Something like collection-enumeration. There is a problem with collection-enumeration in that "collection" implies a fixed-size collection, and the language constructs in .NET languages support iterating over any enumerable object, e.g. an infinite sequence of numbers (the loop is exited when certain conditions are met). OTOH it seems to me that enumerable-enumeration is too .NET-specific; other languages might have different terminology for this concept.
    – Zev Spitz
    Commented Jun 6, 2018 at 6:42
  • @ZevSpitz This could work, given a bunch of tag synonyms and a good compromise target for them to point to. With respect to your specific suggetion of a name, "enumeration" also feels a bit language specific to me -- when I hear "enumeration", enum types are the first thing I think of.
    – duplode
    Commented Jun 6, 2018 at 15:30
  • How about item-looping? item-iteration? or something similar, as I think that is the real point of commonality between all of these -- the iteration is not based on a number or some other sort of index (this specifically excludes Javascript's for...in), but rather the iteration is item-based.
    – Zev Spitz
    Commented Jun 6, 2018 at 15:51
  • @ZevSpitz Perhaps, though I'd be a little bit wary of coining a new term as part of a tag reform. You might want to post your thoughts as a separate answer -- I feel they make for an alternative resolution worth considering.
    – duplode
    Commented Jun 8, 2018 at 17:51
0

As in this answer, the focus of the tag should be as language-agnostic as possible, focusing on the concept behind it -- looping over elements. I think we should keep away from any particular language's syntax. My first thought was one of:

However, I have an issue either of these choices, in that they limit this concept to looping over a fixed-size collection. The C# foreach (and corresponding constructs in other .NET languages) allows for enumerating over any sequence-like object1, which could even be an infinite sequence.

The question to be resolved is, is this a part of the language-agnostic foreach looping concept. If yes, then any conceptual alias will have to take it into account.

Some possibilities:


1. Technically, any object that implements the IEnumerable interface, which means that it implements the GetEnumerator method, providing an object that enables enumeration.

1
  • Honestly, it's like taking a star shaped hole, a circle shaped hole, and a square shaped hole then combine them into one hole that still makes sense. The problem is, such a hole will never make sense and nothing will fit into it nicely, not to mention you will now have a container filled with stars, circles, and squares instead of a nicely sorted set of boxes.
    – user4639281
    Commented Jun 10, 2018 at 0:02

You must log in to answer this question.