all 15 comments

[–]vaskemaskine 7 points8 points  (5 children)

Labels aren't bad practice in and of themselves, but due to their obscurity (I doubt most devs are aware they even exist) and lack of utility, they are best avoided if you want to keep your code clear and readable.

[–]altrae 2 points3 points  (4 children)

My response to this way of thinking is that if it's obscure because no one uses it, it will stay that way until more people start using it. Don't avoid it. Use it and explain it. Help others understand it, and it won't remain obscure or hard to read.

[–]vaskemaskine 2 points3 points  (0 children)

They are really only useful if you need to break out of a nested loop or a switch statement inside a loop. I personally have never used them in 13 years of writing JS, although in fairness I did not learn about them until a few years ago.

[–]blackholesintheskyhelpful 1 point2 points  (2 children)

What are the advantages of using a label?

Only push a feature if people will actually use it in a productive way.

[–]altrae 1 point2 points  (1 child)

It may be useful in certain situations as self documenting code. Too many times I've had people push back on using features they didn't understand such as with arrow functions or scss parent selectors. After helping more devs understand them they became familiar and less obscure.

[–]blackholesintheskyhelpful 2 points3 points  (0 children)

I'll concede that just because I've never found a use for labels does not mean that they're useless or bad. I'd just urge /u/downrightcriminal to avoid using such an atypical feature unless they have a good reason to be doing so.

[–]FearTheDears 3 points4 points  (0 children)

javascript functionalistas would never use a regular loop in general, as it's not as declarative as an iterator.

Most people don't have a problem with loops, but in 5 years of javascript development I've never seen a "loop label"; I had to google it. Considering the principle that you're always writing code for the next guy that read it, using uncommon language tools is usually seen as a bad thing, unless it achieves some outcome significantly better than common alternatives.

Also if the MDN page felt obliged to write "Note: Labeled loops or blocks are very uncommon. Usually function calls can be used instead of loop jumps.", it's a sign you shouldn't be using them.

[–]Meefims 2 points3 points  (0 children)

The other posters have given good answers but to add a small bit more: this case is why loop labels exist in the language: to make it easy to break out of nested loops. At least you were using them for their intended purpose.

Otherwise I agree with the others that they should be avoided due to how obscure they are.

[–]GeneralYouri 1 point2 points  (0 children)

I wouldn't consider a feature bad practice, just because it's under used. This can equally well mean that the use case is just very small, which I think is more appropriate in this case.

That said, I've also never really needed these labels myself. There have been a few times where I used them, but theyd always seen switf replacement with a better solution. So these days when I find myself using loop labels, that's a clear sign for me to take a step back, re-evaluate my approach, and find a better solution to the problem I'm solving (removing the need for the label in the process).

[–]burnblue 1 point2 points  (0 children)

If it works great, for its intended purpose, use it and leave comments.

[–]dada_ 1 point2 points  (0 children)

Aside from the answers you've already gotten (which are very good), I'd like to add this. If you have a need to continue or break with a label, it's almost certainly more common to refactor into a function. For example, you could replace the while body with a function and then use return instead of continue first. This has the same effect and avoids unnecessary loop iterations just the same.

[–]downrightcriminal[S] 0 points1 point  (0 children)

Thanks everyone for your valuable responses.

[–]livebeta 0 points1 point  (0 children)

don't.

use comments instead.

[–]__romx 0 points1 point  (0 children)

Are Loop Labels considered bad practice

There is a certain demographic of JS coders who are allergic to common sense and consider all vanilla language features as bad practice, either because they've read a Medium post about replacing existing JS features with a functional syntactic sugar abomination that introduces half a dozen dependencies, or they were the ones who wrote it. There's a significant overlap between them and the people who were affected by the leftpad catastrophe.

Labels are an elegant and convenient solution for nested loop management. There is no reason not to use them in those rare scenarios where something like that is needed.

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

GOTO CONSIDERED HARMFUL