i'd put money on this being "there was formerly functionality here that was made easier by a foreach that was removed and the loop wasn't updated" as someone who finds this dumb shit in my own codebase all the time
It makes the code harder to read, and less obviously correct. True that it's not inherently a defect, but it will make future defects harder to see (Say, if you add a method that's like if (element.IsEmpty) { continue; }, which would make element not match up with cubes[i], which might be a source of bugs.
Also, it's possible in similar cases with more complex data structures (not this exact one) where the iterator on cubes does not return elements in the same order as get (1..n). So the exact functionality could change.
Ngl, this is a good reason why there should be the ability to access the current enumeration index with a foreach, which should be provided in the IEnumerable interface as well
It looks like this comment contains a code block delimited with triple backticks. Unfortunately reddit does not have universal support for this syntax and your comment will not render correctly on old reddit and most mobile apps.
For the benefit of people on old reddit, this link will take you to a correct rendering of the comment.
/u/EdgarDrake, it would be appreciated, but not required, if you could edit your comment to use the more compatible four space indention format. For single lines or inline code you can use single backticks.
It's not a perfect solution but you can zip it with an infinite counter enumerable and then deconstruct. Can even write an extension method for it, like:
public static IEnumerable<(T value, int index)> WithIndex<T>(this IEnumerable<T> x) {
return x.Zip(Enumerable.Range(0, int.MaxValue), (a, b) => (a, b));
}
What language has an indexOf method for Sets? Java and JavaScript both have indexOf methods for arrays (arraylist for Java). It just returns the first element that stores the same reference.
Has nothing to do with the actual code, but for some reason it took me several reads of the foreach to realize it was saying “Rectangle Shape” rather than “Rectang le Shape”.
Don't you just hate it when you absolutely need the element index but the whole project is written with fancy foreach/streams and you need to use a weird C style for?
Though I’m pretty sure the extra iterator would get optimized out in the generated assembly anyway .... so, it probably doesn’t make much of a difference in the end.
Not specifically but I do get the impression that once people start using them a lot to the point of habit for more complex things you can end up with weird things like this with extra counters etc, particularly if they are more junior and see foreach as just a "replacement" for regular for loop.
Probably should rephrase, I dislike them outside of certain circumstances. But youre right its probably just from a sense of frustration trying to read almost completely unreadable code and asking myself "why didnt you just use a regular for loop here?"
It's just as easy to change "foreach(elem in arr)" it to a for loop and add "elem = arr[i]" at the top so you don't have to change all of your references to elem.
IMO it's harder to read and also hides (not fully, but it's less obvious) that you expect both array to have same size. There is also problem that ienumerable could potentially be changed to something with unknown length (query, or some custom ienumerable). With for loop you would force person changing that type to fix that loop because it would not compile.
Sort of. It's not a dedicated method, but it has a .Select(x => x + 1) which is like .map in python / rust. That also has an overload that's .Select((x, idx) => x + idx). So if you give it a function that takes one argument, it's not enumerate, and if you give it a function that takes two, it is.
the new {} is just an anonymous class. The property names default to being the name you give it (A bit like the name inference in rust's struct construction)
61
u/ohnoahshark Jan 07 '21
i'd put money on this being "there was formerly functionality here that was made easier by a foreach that was removed and the loop wasn't updated" as someone who finds this dumb shit in my own codebase all the time
85
u/Heather_Currie Jan 07 '21
Image Transcription: Code
I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
87
u/[deleted] Jan 07 '21
That's actually something I could do. It's that kind of thing that you don't realize until some time passes or somebody else tells you
41
u/noreal Jan 07 '21
Hahaha
24
u/AlFasGD Jan 07 '21
Ngl, this is a good reason why there should be the ability to access the current enumeration index with a
foreach, which should be provided in theIEnumerableinterface as well13
u/NinDTendo Jan 07 '21 edited Jan 07 '21
actually how I did it a while a go xDD
4
u/fuxximus Jan 07 '21
Does it bother anyone else that the array is named plural cube but what comes is not cube but rectangles?
It should either cube or rectangles, or the elements should be cube
10
u/__archaeopteryx__ Jan 07 '21
Ahhh... some actual bad code! Refreshing.
5
u/GrizonII Jan 07 '21
Has nothing to do with the actual code, but for some reason it took me several reads of the foreach to realize it was saying “Rectangle Shape” rather than “Rectang le Shape”.
2
u/itzNukeey Jan 07 '21
Wish there was for each that would also give you i without declaring it tho
3
u/potbread Jan 07 '21
Is it badcode because the coder used window.Draw(cubes[i]); instead of window.Draw(cubes[element]); ?
4
u/Come_along_quietly Jan 07 '21
Clearly he (I’m assuming gender here) should have .... incremented “i” on the index expression like this:
Window.draw(cubes[i++]);
:-)
/s
1
u/[deleted] Jan 07 '21
Ugh...
1
u/Alusaar Jan 07 '21 edited Jan 07 '21
Cute ^ ^
1
u/fuxximus Jan 07 '21
cubes[i++]);
}
1
u/CoffeeVector Jan 07 '21
I have done this before when I wanted the foreach, but I also wanted the index. Python's enumerate is so great.
1
u/bdking71 Jan 07 '21
Too Funny. Everyone in a journey which begins as a newbie. I know that I've written similar code when I began.
1
u/[deleted] Jan 07 '21
It's reassuring to know for my imposter syndrome that for every 10 people better at it than I am, there are 10 more who may be worse off
1
u/HereComesCunty Jan 07 '21
Lol. I did this today. Or rather I tried to do something like:
someArray.forEach((i, foo) => { someOtherArray[i] = foo })
or something. I like the syntactical sugar but no built in overload for getting an iterator there, gotta be a regular old for loop
1
u/gnovos Jan 07 '21
This made me laugh out loud.
1
u/[deleted] Jan 07 '21
Big brain time
1
u/[deleted] Jan 07 '21
Jesus. Some people just like pain.
1
u/gt4495c Jan 08 '21
The only issue is using
cubes[i]instead ofelement.It makes the loop O(n2 ) possibly instead of O(n).
1
u/DarthNihilus1 Jan 08 '21
i's just along for the ride
1
u/aziad1998 Jan 08 '21
(mapcar #'window-draw cubes)
1
u/nati9931 Jan 08 '21
I did something similar to that... but I ddon'tthink my version is that bad.
for(Rect shape : Objects) shape.draw();(example in pseudo code)1
u/Dudecor3 Jan 08 '21
This is something I'd have done when I was learning how to code. The right thinking is there in this code though, not the worst I've ever seen
1
u/edoCgiB Jan 08 '21
Don't you just hate it when you absolutely need the element index but the whole project is written with fancy foreach/streams and you need to use a weird C style for?
1
u/dreysion Jan 07 '21
Well, I mean... it works
-7
u/DillonisaCunt Jan 07 '21 edited Jan 07 '21
I see this exact thing and much worse on a daily basis in my current workplace's codebase. One of the reasons I dislike using foreach loops
Edit: should rather say I prefer regular for loops
0
u/[deleted] Jan 07 '21
[deleted] — view deleted comment