logoalt Hacker News

jacquesmyesterday at 11:12 PM3 repliesview on HN

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.


Replies

whizztertoday at 9:29 AM

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).

dwattttttoday at 1:29 AM

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.

cyberaxtoday at 5:04 AM

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.

show 1 reply