top | item 46394797

(no title)

morcus | 2 months ago

Wow, I had no idea JavaScript has labeled break! Thanks for the comment.

discuss

order

jacquesm|2 months ago

It's a terrible feature, really. If you need a labeled break what you really need is more decomposition. I'm pretty sure that Dijkstra would have written a nice article about it, alas, he is no longer with us.

whizzter|2 months ago

I doubt he would, goto heavy code really jumped all over the place in incomprehensible manners. Labelled break/goto is used in perhaps 1% of loops due to actually implementing some algorithm that would've needed extra flags,etc. and EVEN THEN don't break the scoped readability.

There's a huge difference between reining in real world chaos vs theoretical inelagancies (ESPECIALLY if fixing that would introduce other complexity to work around the lack of it).

dwattttt|2 months ago

It depends on how big or trivial the loops you need are. If they're only 3 or 4 lines, extracting their body into a function doesn't improve anything.

cyberax|2 months ago

Dijkstra was wrong on this one. Breaks and continues help to keep the code more readable by reducing the amount of state that needs to be tracked.

Yes, from the purely theoretical standpoint, you can always rewrite the code to use flags inside the loop conditions. And it even allows formal analysis by treating this condition as a loop invariant.

But that's in theory. And we all know the difference between the theory and practice.