Pages: 1 2 »
  Print  
Author Topic: Named loops vs numbered breaks  (Read 16036 times)
Offline (Male) Josh @ Dreamland
Posted on: September 08, 2012, 04:47:37 pm

Prince of all Goldfish
Developer
Location: Pittsburgh, PA, USA
Joined: Feb 2008
Posts: 2950

View Profile Email
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)
  }
}
Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Offline (Female) IsmAvatar
Reply #1 Posted on: September 10, 2012, 09:25:11 pm

LateralGM Developer
LGM Developer
Location: Pennsylvania/USA
Joined: Apr 2008
Posts: 877

View Profile Email
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.
« Last Edit: September 10, 2012, 09:32:18 pm by IsmAvatar » Logged
Offline (Male) Josh @ Dreamland
Reply #2 Posted on: September 10, 2012, 11:35:23 pm

Prince of all Goldfish
Developer
Location: Pittsburgh, PA, USA
Joined: Feb 2008
Posts: 2950

View Profile Email
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?
Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Offline (Male) polygone
Reply #3 Posted on: September 10, 2012, 11:43:28 pm

Contributor
Location: England
Joined: Mar 2009
Posts: 794

View Profile
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.
« Last Edit: September 10, 2012, 11:47:55 pm by polygone » Logged
I honestly don't know wtf I'm talking about but hopefully I can muddle my way through.
Offline (Male) Josh @ Dreamland
Reply #4 Posted on: September 11, 2012, 07:51:59 am

Prince of all Goldfish
Developer
Location: Pittsburgh, PA, USA
Joined: Feb 2008
Posts: 2950

View Profile Email
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.
Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Offline (Unknown gender) TheExDeus
Reply #5 Posted on: September 11, 2012, 08:36:03 am

Developer
Joined: Apr 2008
Posts: 1860

View Profile
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.
Logged
Offline (Male) Josh @ Dreamland
Reply #6 Posted on: September 11, 2012, 02:13:23 pm

Prince of all Goldfish
Developer
Location: Pittsburgh, PA, USA
Joined: Feb 2008
Posts: 2950

View Profile Email
That is what [snip=edl]continue loop;[/snip] would do. Same as [snip=edl]continue 2;[/snip].
Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Offline (Unknown gender) luiscubal
Reply #7 Posted on: September 11, 2012, 04:05:25 pm
Member
Joined: Jun 2009
Posts: 452

View Profile Email
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(...)"
Logged
Offline (Unknown gender) TheExDeus
Reply #8 Posted on: September 12, 2012, 11:32:43 am

Developer
Joined: Apr 2008
Posts: 1860

View Profile
Quote
Regarding 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?
Logged
Offline (Male) Josh @ Dreamland
Reply #9 Posted on: September 12, 2012, 05:20:44 pm

Prince of all Goldfish
Developer
Location: Pittsburgh, PA, USA
Joined: Feb 2008
Posts: 2950

View Profile Email
It is the same as break, so why use it?
Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Offline (Unknown gender) luiscubal
Reply #10 Posted on: September 13, 2012, 12:19:53 pm
Member
Joined: Jun 2009
Posts: 452

View Profile Email
Yet one more reason to avoid numbered breaks: Some people might expect "start at 0" while others will expect "start at 1".
Logged
Offline (Male) Josh @ Dreamland
Reply #11 Posted on: September 13, 2012, 01:41:05 pm

Prince of all Goldfish
Developer
Location: Pittsburgh, PA, USA
Joined: Feb 2008
Posts: 2950

View Profile Email
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.
Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Offline (Unknown gender) luiscubal
Reply #12 Posted on: September 13, 2012, 01:48:45 pm
Member
Joined: Jun 2009
Posts: 452

View Profile Email
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.
Logged
Offline (Male) Josh @ Dreamland
Reply #13 Posted on: September 13, 2012, 02:18:07 pm

Prince of all Goldfish
Developer
Location: Pittsburgh, PA, USA
Joined: Feb 2008
Posts: 2950

View Profile Email
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.
Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Offline (Unknown gender) The 11th plague of Egypt
Reply #14 Posted on: September 26, 2012, 12:02:23 pm
Member
Joined: Dec 2009
Posts: 274

View Profile
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
Logged
Pages: 1 2 »
  Print