views:

2017

answers:

17

I'm just curious what peoples' thoughts are on this topic. Let's say I have an Array of Objects, and I want to loop through them to see if the Objects contain certain values, and if so, I want to stop the loop. Which is better practice - a for loop with a break, or a conditional loop?

The pseudo-code in the example I have provided is for argument's sake only (it is also in ActionScript, since that is my primary language of late). Also, I am not looking for best practice ideas on syntax.

for loop with break:

var i:int;

var isBaxterInMilwaukee:Boolean;    

for (i = 0; i < arrayLen; i++)
{
    if (myArray[i]["name"] == "baxter"
         && myArray[i]["location"] == "milwaukee")
    {
        isBaxterInMilwaukee = true;

        barkTwice();

        break;
    }
}

conditional loop:

var i:int;

var isBaxterInMilwaukee:Boolean;    

while (!isBaxterInMilwaukee && i < arrayLen)
{
    if (myArray[i]["name"] == "baxter"
         && myArray[i]["location"] == "milwaukee")
    {
        isBaxterInMilwaukee = true;

        barkTwice();
    }

    i++;
}
+3  A: 

I would say it depends. In this case the loop with the break seems clearer to me.

Ferruccio
+2  A: 

I've always disliked the use of breaks in code... In this case it doesn't seem to matter but on more involved loops, it can be awfully confusing to another coder reading it and not understanding how the loop may terminate until he spots the nested break deep in the loop. By specifying a flag condition that's checked each iteration of the loop, it makes this much clearer.

This problem would be similar to having return statements that are deep in the body of a method where they're not easily spotted (rather than setting a retVal variable and returning at the end of the method). With a small method, this seems fine, but the bigger it gets, the more confusing this will be.

It's not an efficiency of operation thing, it's a maintainability thing.

Ask your coworkers what's readable and understandable for a particular situation... That's what really matter.

Andrew Flanagan
I like this idea: Check for the flag regardless of the type of loop.
Nerdling
Using flag is good but it is beneficial in larger loops, in short loops (few lines) that you can easily overlook I prefer to use breaks.
Jiri
+2  A: 

The one that makes the most sense is going to be the one that conveys the idea to the human reading the code the best. Remember code readability first, and you'll usually make the correct choice. Usually, you do not want to use something like break unless you really need to, because it can make things hard to follow if done often or even just in a deeply-nested set of expressions. continue can serve the same purpose as a break sometimes, and the loop will then exit normally instead of because it was broken. In this case, there are a couple of different ways I might write this.

Probably the best thing you want here is a modification of your while loop:

while(!isBaxterInMilwaukee || i < arrayLen) {
  if(myArray[i]["name"] == "baxter" && myArray[i]["location"] == "milwaukee") {
    isBaxterInMilwaukee == true;
    barkTwice()
  } else {
    i++;
  }
}

That's clear and doesn't use break or continue, so you can tell at a glance that you'll always terminate as a result of one of the conditions specified in the while expression.

ETA: Probably should be i < arrayLen in the while loop otherwise it fails the first time through unless the input value is the same as the target value...

Michael Trausch
basically, i don't want the boolean value to get reset. that's the purpose of the question.
Eric Belair
+1  A: 

I would say break, clearer (even if you put in a comment why you break out of the loop) Imho the while loop is not clear, i would go for break

PoweRoy
+2  A: 

I see the break in both loops, is that correct?

Anyway:

  • I would choose FOR loop when there is known number (maximum number) of iterations before loop starts.
  • I would choose WHILE otherwise.
  • In FOR loop I use BREAK freely.
  • In WHILE loop I prefer to use complex condition instead of BREAK (if it is possible).
Jiri
+4  A: 

In a for loop you can also early exit by putting the early exit criteria in the for loop declaration. So for your example you could do it this way:

var i:int;

var isBaxterInMilwaukee:Boolean;    

isBaxterInMilwaukee = false;

for (i = 0; i < arrayLen && !isBaxterInMilwaukee; i++)
{
    if (myArray[i]["name"] == "baxter"
        && myArray[i]["location"] == "milwaukee")
    {
        isBaxterInMilwaukee = true;

        barkTwice();
    }
}

That way you don't need a break, and it's still more readable than a while loop.

Joseph
+16  A: 

In short, you should go with whichever version is the easiest to read and maintain.

In slightly older times, I know breaking out of a loop was considered to be a no-no (on par with a goto statement). Loops were supposed to break on the loop condition and nowhere else. Thus, the while-loop would have been the way to go.

(This is probably a holdover from assembly, where loops are basically a block of code with a go-to-the-beginning-if-true jump statement at the end. Multiple conditional-jump statements in the block make it exceedingly hard to debug; thus they were to be avoided and combined into one at the end.)

I feel this idea seems to be changing a bit today, especially with foreach loops and the managed world; it's really a matter of style now. Break-on-found for-loops have perhaps come to be acceptable to many, save some purists of course. Note that I would still avoid using break in a while-loop, however, as this can muddle the loop condition and make it confusing.

If you'll allow me to use a foreach loop, I consider the code below to be a lot easier to read than its while-loop brother:

bool isBaxterInMilwaukee;    

foreach (var item in myArray)
{
    if (item.name == "baxter" && item.location == "milwaukee")
    {
        isBaxterInMilwaukee = true;    
        barkTwice();
        break;
    }
}

However, as the logic grows in complexity, you may want to consider a prominent comment near the break statement lest it become buried and hard to find.


Arguably, this whole thing should be refactored into its own function which doesn't break on found, but actually returns the result (feel free to use the for-loop version instead):

bool isBaxterInMilwaukee(Array myArray)
{      
    foreach (var item in myArray)
    {
        if (item.name == "baxter" && item.location == "milwaukee")
        {
            barkTwice();
            return true;
        }
    }
    return false;
}

As Esko Luontola pointed out, it would probably be best to move the call to barkTwice() outside of this function as the side-effect is not evident from the function's name, nor related to finding Baxter in every case. (Or add a boolean parameter BarkTwiceIfFound and change the line to read if(BarkTwiceIfFound) barkTwice(); to make the side-effect clear.)


For the record, you can also do the flag check in the for-loop without a break, but I feel this actually hurts readability because you don't expect an extra condition in a for-loop definition:

var i:int;

var isBaxterInMilwaukee:Boolean;    

for (i = 0; !isBaxterInMilwaukee && i < arrayLen; i++)
{
    if (myArray[i]["name"] == "baxter"
         && myArray[i]["location"] == "milwaukee")
    {
        isBaxterInMilwaukee = true;    
        barkTwice();
    }
}

You can also simulate auto-incrementing mechanics with a while-loop. I don't like this for a few reasons - you have to initialize i to be one less than your real starting value, and depending on how your compiler short-circuits the loop-condition logic, your value of i on exiting the loop may vary. Nevertheless, it is possible and for some people, this can improve readability:

var i:int = -1;

var isBaxterInMilwaukee:Boolean;    

while (!isBaxterInMilwaukee && ++i < arrayLen)
{
    if (myArray[i]["name"] == "baxter"
         && myArray[i]["location"] == "milwaukee")
    {
        isBaxterInMilwaukee = true;
        barkTwice();
    }
}
lc
Refactoring the code to its own function is the clearest. It would also be good to move barkTwice() outside isBaxterInMilwaukee(), because the function's name does not indicate in any ways the side-effect of barking twice.
Esko Luontola
For example like this: if (isBaxterInMilwaukee(myArray)) { barkTwice(); }
Esko Luontola
Definitely agreed there. I didn't want to make my explanation any longer than it already is, but it is certainly worth a mention. Edited.
lc
Refactoring into its own function is far, far better than any of the other alternatives.
mquander
+1  A: 

It depends a lot on what the specific circumstances are. But in your example, you want to walk an array of bounded length, and using a for loop makes it easy to do that and guard against running off the end. In your while loop example you have to do your own incrementing--which can be problematic if you wanted to use a continue statement to skip into the next cycle--and make a more complex conditional expression (which, by the way, has a bug; I think you meant && i != arrayLen ). You're just having to do extra code to accomplish the effect that for loops help provide.

Of course, some purists will argue that break and continue should not be used and that you should use if-else and boolean variables if needed rather than continue or break out of a loop. But I think that can make the loop look much more ugly, especially if its relatively short and easy to grasp in general like this example. For a loop with much longer code where a break or continue could easily hide from notice, the purist approach might be more clear, since the loop is already complicated to grasp in that case. But you can always do this as part of the for loop, just add it as part of the conditional.

It's also better practice to test the array bound with i < arrayLen rather than for an exact equality, in case something caused i to skip over the exact value (I actually saw this happen in a Y2K bug, which could have been avoided by the better practice).

Rob Parker
+1  A: 

I have a C++ background and so I still have moments where I try to "think like the compiler". While loops tend to result in tighter code, and so for loops were only ever considered if you knew you were going to iterate over every element in the array, every time.

EDIT: I now believe this is overkill, if you're using .Net or whatever you aren't going to make up for the VM overhead with a couple of tight loops. I do think remembering the "why" of certain practices is a good thing though.

overslacked
+1  A: 

My theory is that there is a useful programming abstraction similar to "signal-to-noise ratio", which is "problem-to-tool ratio" - goodness can be measured in one dimension by how much time I spend thinking about the problem and its solution, compared to the time I spend thinking about how to use the tool (in this case language syntax).

By that measure, I try to use fewer constructs more frequently, because I (and hopefully those who follow) can grok the essence of my code structure more quickly and accurately. And since variations of "for loops" do a pretty good job of covering the cases where the others might be used (without distortion), I use them as a first preference, when they are interchangeable.

And it's nice to have everything you need to know (grokwise) about the loops rules in a single line at the top of the "for" loop. I also tend to put the "default" switch first in the tests for the same reason.

But consistency and clarity is the ovveriding consideration. YMMV, of course.

le dorfier
It's a mistype for "Your mileage may vary". :)
le dorfier
+1  A: 

I would definitely go with the for+break. The ‘for’ is an instantly-recognisable idiom for “iterate over sequence” and it's easier to understand “iterate over sequence; end early if value found” than the combined loop-and-stop condition.

There may be evidence for this in the way you seem to have made two mistakes in your conditional loop code!

  • the while condition (!isBaxterInMilwaukee || i == arrayLen) — did you mean “(!(isBaxterInMilwaukee || i == arrayLen))”?

  • break statement is unnecessary if you're using a terminate-loop variable.

Personally I find a simple ‘break’ much easier to read than trying to track a terminate-loop variable.

bobince
The break isn't necessary to end the particular while loop example, but if it lets the i++ statement execute then i is not left pointing at the position which was found (which might have been handy to keep). So avoiding the extra increment (by a break or an if-else, etc) is probably more useful.
Rob Parker
+3  A: 

There is a conceptual difference between the two. for loops are for iterating over discrete sets and while loops are for repeating statements based on a condition. Other languages add in finally clauses and looping constructs like foreach or until. They tend to have considerably fewer traditional for loops.

In any case, the rule that I use is that for loops iterate and while loops repeat. If you see something like:

while (counter <= end) {
   // do really cool stuff
   ++counter;
}

Then you are probably better off with a for loop since you are iterating. However, loops like:

for (int tryCount=0; tryCount<2; ++tryCount) {
    if (someOperation() == SUCCESS) {
       break;
    }
}

should be written as while loops since they are really repeating something until a condition is true.

The thought of not using break since it is just as evil as goto is pretty nonsensical. How can you justify throwing an exception then? That's just a non-local and non-deterministic goto! By the way, this isn't a rant against exception handling, just an observation.

D.Shawley
+1  A: 

There are two aspects of the problem:

  • What to do (eg: find if one of the items contain the specified person in the location)
  • How to do it (eg: use an index, iterate etc)

Both of the examples mix the two and is hard to understand the what from the how. The best would be if we could express in code only the what part. Here is an example (c# 3.5) that does this using the specification pattern

// what we are looking for?
IsPersonInLocation condition = new IsPersonInLocation("baxter", "milwaukee");

// does the array contain what we are looking for?
bool found = myArray.Find(item => condition.IsSatifiedBy(item));

// do something if the condition is satisfied
if (found) {
    barkTwice();
}

For completeness here is the class definition for the condition:

class IsPersonInLocation {
    public string Person { get; set; }
    public string Location { get; set; }
    public IsPersonInLocation(string person, string location) {
        this.Person = person;
        this.Location = location;
    }
    bool IsSatifiedBy(item) {
        return item["name"] == this.Person
            && item["location"] == this.Location;
    }
}
Aleris
I agree with the principle. But that awful mess of boilerplate for such a simple search is just not viable.
John Nilsson
Reducing complexity usually requires writing more code. This might seem counterintuitive until acknowledging that the problem is not the total amount of code but the amount of code you have to look at a certain moment to understand it.
Aleris
Yes. But my reaction was more a reaction to the choice of language. I posted an alternative version of your comment in Java Script to illustrate the difference.
John Nilsson
A: 

My general stance is:

If it has a loop counter use for() (like while while loop does).

cletus
A: 

I vote while because breaks reduce grokability.

You may not realize the loop contains a break if the loop has grown too long and you insert code you expect to run and it does not.

But I subscribe to the don't make me think model of coding.

Trampas Kirk
+1  A: 

I guess neither is actually vary interesting. You should look for higher level constructs if it's readability you're after.

In JS:

if(myArray.some(function(o) { o.name == "baxter" && o.location == "milwaukee" }))
  barkTwice();

or with some utils of your own

if(myArray.containsMatch({name:"baxter",location:"milwaukee"})
  barkTwice();
John Nilsson
A: 

Encapsulate the loop in its own method and use return to end processing when your match condition has succeeded.

Some example C# code:

class Program
{
   static bool IsBaxterInMilwaukee(IList<WhoAndWhere> peopleAndPlaces)
   {
      foreach (WhoAndWhere personAndPlace in peopleAndPlaces)
      {
         if (personAndPlace.Name == "Baxter" 
            && personAndPlace.Location == "Milwaukee")
         {
            return true;
         }
      }
      return false;
   }

   static void Main(string[] args)
   {
      List<WhoAndWhere> somePeopleAndPlaces = new List<WhoAndWhere>();
      somePeopleAndPlaces.Add(new WhoAndWhere("Fred", "Vancouver"));
      somePeopleAndPlaces.Add(new WhoAndWhere("Baxter", "Milwaukee"));
      somePeopleAndPlaces.Add(new WhoAndWhere("George", "London"));

      if (IsBaxterInMilwaukee(somePeopleAndPlaces))
      {
         // BarkTwice()
         Console.WriteLine("Bark twice");
      }
   }

   public class WhoAndWhere
   {
      public WhoAndWhere(string name, string location)
      {
         this.Name = name;
         this.Location = location;
      }

      public string Name { get; private set; }
      public string Location { get; private set; }
   }

}
Wedge