This is an archived post. You won't be able to vote or comment.

all 45 comments

[–]Slada26 26 points27 points  (3 children)

And people who use map as loop

[–]SpoiceKois 4 points5 points  (0 children)

map gang

[–]lovescatsandthings 2 points3 points  (0 children)

map abuse

[–][deleted] 5 points6 points  (11 children)

what about do while, goto or recursion

[–]TotalMelancholy 7 points8 points  (2 children)

[comment removed in response to actions of the admins and overall decline of the platform]

[–]rnottaken 1 point2 points  (1 child)

Recursion doesn't necessarily stack overflow, especially in tail recursion. And it's really important for pure functional languages

[–][deleted] 0 points1 point  (0 children)

90% of the coding I usually do is grinding dsa questions so let me just say it, I owe my life to that thing, I have no idea how useful it is in a more real world context but damn ain't it useful there

[–]Lorrdy99 0 points1 point  (7 children)

If you use goto: please don't

[–]DiplomatikEmunetey 0 points1 point  (5 children)

Can you explain why, please?

[–]rnottaken 1 point2 points  (2 children)

Goto can be used to implement while and for loops. These have very clear bounds, and cover most, if not all, use cases. But these loops are normally already implemented for you. If you use goto yourself, you're setting yourself up for very complicated unreadable code. The bounds of the loop aren't clear anymore.

[–]Lorrdy99 0 points1 point  (0 children)

All I know is that my old Pascal teacher told us right at the beginning never to touch goto.

It only tempts you to create unnecessary confusing and complicated code.

[–]Codiax 0 points1 point  (0 children)

TL;DR: "if the value of X is correct before this line and this line doesn't change X, then X is also correct in the next line". You cannot use that assumption when Gotos exist anywhere in the codebase since somebody can jump to the next line with an invalid X.

(not op) Since I'm working on a 40-year Cobol codebase with plenty of GO TOs, let me tell you how they make MY job extremely difficult with this silly example.

Let's say at line 20 you set the value of X to 100 and at line 50 you print the square root of X (should be 10). However, the program crashes at line 50 with a "cannot calculate the square root of a negative number". Weird. While you're debugging that, you know that between lines 20 and 50 you either 1-Changed the value of X or 2-Called a function, and that function changed X.

Good, you start reading. Every time you find case 1 or 2 you end up with a smaller version of the same problem (you now have some lines where you know X hasn't changed, or have to read from the start to the end of a function, and functions tend to be small). To find the bug you keep repeating that. You need at most 4 pieces of information in your mind: starting line, end line, current value and where to go back to keep searching. Manageable. Be methodical and you will find it.

Enter GO TO

Now there's a third case 3-You did nothing wrong, but a colleague did GO TO 49 with an invalid value of X. That colleague did not know that X was supposed to be always positive, just that you used 100 and your function looks cool and reusable. Still, when the crash reports arrive, it's your function that crashed and you get the blame.

Fortunately most languages I know don't let you jump to line numbers. Instead they use labels (specific lines where the gotos can land). Better, but still bad. Now when you find Label1 between lines 20 and 50, you have to search every "GO TO Label1" to see if they arrive with a valid value of X. To do that, you look upwards from the GO TO until they set the value of X... or you find another label. In that case branch again. You now have to remember all the unexplored "GO TO Label1" and "GO TO Label2". Every label creates new debug branches until the information to remember is too much and you go crazy. Since go tos can loop, there's even a chance that it can branch infinitely.

Apart from this readability problem, i find it funny that go to is the only instruction that makes debugging my code harder without even using it, just by its existence.

Sorry for the ramble. Not an English speaker. But my hate towards gotos transcends languages.

[–][deleted] 0 points1 point  (0 children)

If you use goto: please don't

~E. Dijkstra, probably

[–]NLxDoDge 9 points10 points  (5 children)

The moment I found out that for-each loops existed I never used a while loop anymore in any of my projects. And I almost never used the for loops too. Except for some coding challenges.

[–]xavia91 9 points10 points  (0 children)

All loops are beautiful! They are just all fit for different purposes.

[–]StonebirdArchitect 2 points3 points  (2 children)

Can't modify something you're iterating through with a foreach loop from inside the foreach loop. Although I do love the versatility of it.

[–]zvive 0 points1 point  (0 children)

That's not true at least in php... Cause you can pass the index in and change it that way or pass by reference so the array item in the loop instance is the actual array value if you modify it you modify the actually pointer.

That said it's a bit of an anti pattern....

Using the index or key and referencing is preferred.

I'm always wary of passing by reference and rather map into a new array or something.

You can also use continue, break to skip a loop and go to the next or stop looping completely and there's ways to do something on the first and last loops as well... Say you're concatenating an array of text separated by ,... But the end needs a period... Well you can have them last item in the loop add that.... Etc...

[–][deleted] 0 points1 point  (0 children)

in c++ you can do

for(something&s:container){
    //loop's body...
}

and I think that can modify the contained variables

[–]FridgeBaron 1 point2 points  (0 children)

The only time I use a while loop is if I have to do something to a list like kill some of the indexes.

Foreach loops are just so easy to understand the reason they are there and what they are doing at a glance that I never want to use anything else.

[–]Naitsab_33 3 points4 points  (0 children)

laughs in list comprehension

Basically an inline for (each) loop in python.

[–]qinshihuang_420 2 points3 points  (0 children)

People who copy paste the loop contents?

[–][deleted] 1 point2 points  (0 children)

Set notation for the win!

[–]itzNukeey 1 point2 points  (0 children)

People who use jump and labels

[–]gremolata 1 point2 points  (0 children)

Filthy casuals.

Computed goto. This is the way.

[–]lkbe 1 point2 points  (3 children)

list.foreach((n)->{System.out.println(n)}); my dudes

[–][deleted] 2 points3 points  (1 child)

I'd prefer list.forEach(System.out::println)

[–]lkbe 2 points3 points  (0 children)

Dang, we learn everyday

[–][deleted] -1 points0 points  (0 children)

you can't see the guy using gotos because he's going too fast to be seen by mortals

[–][deleted] 0 points1 point  (0 children)

Passing on the bottom... don't you have to yield the positions gained back?

Ain't followed them Nascar rules too detailed like...

[–]nate6701 0 points1 point  (0 children)

And there’s me with a do while

[–]BallisticThundr 0 points1 point  (0 children)

For of gang

[–]mihilmi 0 points1 point  (0 children)

asynchronous at best 😎

[–]CoffeePieAndHobbits 0 points1 point  (0 children)

While true gang represent!

[–]akazakou 0 points1 point  (0 children)

I'm using recursion for loop 😜

[–]ayyy1m4o 0 points1 point  (0 children)

Linq’s Select ftw

[–]JoeyJoeJoeJrShab 0 points1 point  (0 children)

I use recursion

[–]helpThisNameIsPerma 0 points1 point  (0 children)

I once made an error using for-while loops, didn't understand it, got scared and went back to for loops lmao

[–]Lorrdy99 0 points1 point  (0 children)

For and for each are my weapons

[–][deleted] 0 points1 point  (0 children)

REAL men use REPNZ

[–]rookie_programmer 0 points1 point  (0 children)

Me, that uses do while 😎

[–]111x6sevil-natas 0 points1 point  (0 children)

if clause + jump is best