Pages: 1 2
Author Topic: Named loops vs numbered breaks  (82,273 Views)
Offline (Unknown gender) Josh @ Dreamland

Prince of all Goldfish
Developer
Joined: Feb 2008
Posts: 2,950
View profile
Posted on: September 08, 2012, 09:47:37 PM
Code (EDL) Select
for (int i = 0; i < 10; ++i) {
  for (int j = 0; j < 10; ++j) {
    if (target[i][j])
      break; // FUCK!
  }
}


Named Loops:
Code (EDL) Select
HOLYFUCKIDONTKNOWWHATTONAMETHISLOOP: for (int i = 0; i < 10; ++i) {
  for (int j = 0; j < 10; ++j) {
    if (target[i][j])
      break HOLYFUCKIDONTKNOWWHATTONAMETHISLOOP; // yay?
  }
}


Numbered breaks:
Code (EDL) Select
for (int i = 0; i < 10; ++i) {
  for (int j = 0; j < 10; ++j) {
    if (target[i][j])
      break 2; // (Y)
  }
}
Offline (Unknown gender) IsmAvatar

LateralGM Developer
LGM Developer
Joined: Apr 2008
Posts: 877
View profile
Reply #1 Posted on: September 11, 2012, 02:25:11 AM
I vote named loops primarily because with numbers, the off-by-one would confuse me.
Although I guess it makes sense - break 1 breaks the first loop, break 2 breaks out of the second loop, break 0 does nothing.
Offline (Unknown gender) Josh @ Dreamland

Prince of all Goldfish
Developer
Joined: Feb 2008
Posts: 2,950
View profile
Reply #2 Posted on: September 11, 2012, 04:35:23 AM
While "both" was originally a joke, I actually think I might do it. My main aversion to it was that it would make goto labels ambiguous, but in the end, (1) do we really want goto, and (2) why can't a loop name also be a label?

It would be simple to just warn on [snip]break 1;[/snip]. Did I mention I'm adding warnings?
Offline (Unknown gender) polygone

Contributor
Joined: Mar 2009
Posts: 794
View profile
Reply #3 Posted on: September 11, 2012, 04:43:28 AM
Having thought about it I now vote break << 1; break << 2; to break one loop back, two loops back.

I think adding both is over-kill and might be confusing, in fact named loops makes things more confusing by itself in that it will make it harder to read other people's code whereas numbered breaks can be universally understood.
Offline (Unknown gender) Josh @ Dreamland

Prince of all Goldfish
Developer
Joined: Feb 2008
Posts: 2,950
View profile
Reply #4 Posted on: September 11, 2012, 12:51:59 PM
That's pretty ugly, and it doesn't match other languages, so newcomers couldn't really guess it.  If we're going to have them learn something new, it should be that the number after break is the total number of loops to exit, not the additional number.

People are really used to named loops, too. We'll let the userbase decide.
Offline (Unknown gender) TheExDeus

Developer
Joined: Apr 2008
Posts: 1,860
View profile
Reply #5 Posted on: September 11, 2012, 01:36:03 PM
What warnings? For debug?
And I also have some problems with understanding labeled breaks. I checked some JS examples and it seemed that break 2; in Josh's example would stop both loops, but break label; in that example would break the inner loop and go to the outer loop. Although maybe the example I saw was just bs.
Offline (Unknown gender) Josh @ Dreamland

Prince of all Goldfish
Developer
Joined: Feb 2008
Posts: 2,950
View profile
Reply #6 Posted on: September 11, 2012, 07:13:23 PM
That is what [snip=edl]continue loop;[/snip] would do. Same as [snip=edl]continue 2;[/snip].
Offline (Unknown gender) luiscubal

Member
Joined: Jun 2009
Posts: 452
View profile
Reply #7 Posted on: September 11, 2012, 09:05:25 PM
I voted named loops because it's less confusing, more intuitive and less error-prone.
Besides, it's the way Java does it so there are already precedents showing it can work.

Regarding the "error prone" part, imagine if you replaced that if case with a switch case. Then you'd have to change "break 2" to "break 3". Would you remember? That would be a pretty messy bug to track down. With named loops, there is no such problem.

Regarding warnings to "break 1". Why even allow it in the first place? Just ban it with a compile error. Problem solved. One less horrible feature to test.

The question to ask about "numbered breaks" is not "can we add it easily?", which I have no doubt you can if you can also do named loops. The real question is "what would this improve?". I can not think of any situation where numbered breaks would beat named breaks.

BTW: Just call the loop "outer_loop: for(...)"
Offline (Unknown gender) TheExDeus

Developer
Joined: Apr 2008
Posts: 1,860
View profile
Reply #8 Posted on: September 12, 2012, 04:32:43 PM
QuoteRegarding warnings to "break 1".
Is that the warning Josh was talking about? And why would this even need a warning? Was is invalid about that statement?
Offline (Unknown gender) Josh @ Dreamland

Prince of all Goldfish
Developer
Joined: Feb 2008
Posts: 2,950
View profile
Reply #9 Posted on: September 12, 2012, 10:20:44 PM
It is the same as break, so why use it?
Offline (Unknown gender) luiscubal

Member
Joined: Jun 2009
Posts: 452
View profile
Reply #10 Posted on: September 13, 2012, 05:19:53 PM
Yet one more reason to avoid numbered breaks: Some people might expect "start at 0" while others will expect "start at 1".
Offline (Unknown gender) Josh @ Dreamland

Prince of all Goldfish
Developer
Joined: Feb 2008
Posts: 2,950
View profile
Reply #11 Posted on: September 13, 2012, 06:41:05 PM
I'd warn/error on break 0, too.

It's easier to to read break 2, in my opinion; even if you're in a huge nested loop, you have an idea of how many loops you're in, whereas you can easily forget what a loop is named. Granted, you can assume it breaks the big one or huge one based on context, but with break 3, you know it's breaking all of them.
Offline (Unknown gender) luiscubal

Member
Joined: Jun 2009
Posts: 452
View profile
Reply #12 Posted on: September 13, 2012, 06:48:45 PM
Well:

Code (edl) Select

z_loop: for (int z = 0; z < 100; ++z) {
   y_loop: for (int y = 0; y < 100; ++y) {
      x_loop: for (int x = 0; x < 100; ++x) {
          break z_loop;
      }
   }
}

and
Code (edl) Select

instance_loop: foreach (auto instance : instances) {
     adjacent_instances_loop: foreach (auto adjacent : instance.adjacent_instances()) {
         break instance_loop;
     }
}

Seems pretty clear to me.
Frankly, I'd generally avoid using integers for anything that can be reasonably named, but whatever.

Just remember: more features mean more test cases.

And regarding test cases, don't forget to add mixed loops/switch cases to your test suite.
Offline (Unknown gender) Josh @ Dreamland

Prince of all Goldfish
Developer
Joined: Feb 2008
Posts: 2,950
View profile
Reply #13 Posted on: September 13, 2012, 07:18:07 PM
I see both sides. I'm pretty sure I'll end up doing both; the mechanism that handles the double break will be the same, so the difference between the two implementations C++-side will be in identifying the loop to which the user is referring.
Offline (Unknown gender) The 11th plague of Egypt

Member
Joined: Dec 2009
Posts: 274
View profile
Reply #14 Posted on: September 26, 2012, 05:02:23 PM
If you have to do it, use named loops.

It looks like a good companion to goto+label, I think it's a nice fit for a pasta lover. :D
Pages: 1 2