0

Is there a name for including a limitation in a loop structure to prevent it from running if its primary condition becomes unwieldy.

For example

for (var i = 0; i < len; i++){

   some_function();

}

but if len is somewhere set to ten million, I don't want to run it, so I would do something like:

for (var i = 0; i < len && i < 50; i++){

   some_function();

}

Is there a name for this type of hard-coded condition?

6
5

This seems to be a type of a guard. See: http://en.wikipedia.org/wiki/Guard_(computer_science)

In computer programming, a guard is a boolean expression that must evaluate to true if the program execution is to continue in the branch in question.

In your case, i < 50 is the guard on the loop, preventing it from executing if i is too large.

I think a guard would usually be written as:

if (len< MAX_VALUE)
{
    for (var i = 0; i < len; i++)
    {
        do_stuff();
    }
}
//optional else-clause to warn when guard-condition is violated.
//else 
//   ERROR("Value for i is too big!!"); 

This form would allow you to write an else clause (where you could warn the user about invalid value of i, perhaps). Your compact form, where you include it in the loop condition doesn't allow for an else, but maybe you don't want one.

As some commenters have also noted, your loop will still execute up to 50 iterations, even if len is larger than 50. Using a traditional if..else guard as written above will not allow you to do this, so it's not equivalent with your code (which also checks the guard condition on each iteration).

4
  • 3
    Not quite the same - that check happens once, instead of each iteration – Izkata Dec 9 '13 at 16:36
  • @Izkata: Yes, that's a very good point! – FrustratedWithFormsDesigner Dec 9 '13 at 16:44
  • 3
    if (i< MAX_VALUE_FOR_I) { loop-by-i; } looks slippery. Did you by chance mean if (len< MAX_VALUE_FOR_I)? – gnat Dec 9 '13 at 17:20
  • 2
    @gnat Even if so, the correct equivalent wouldn't prevent the loop from running, the OP's code limits the maximum iterations. To get the same effect: len = min(len, MAX_VALUE_FOR_I) – Izkata Dec 9 '13 at 17:32
5

I'd call it a boundary condition.

Some boundary conditions have special names, such as timeouts for example, and other boundary conditions are indicative of logical errors, such as an "off-by-one" error.

6
  • Good answer. Might want to clarify that the i < len condition is a boundary condition too. – Some Guy Dec 9 '13 at 16:11
  • @SomeGuy "hardcoded boundary condition"? – 1252748 Dec 9 '13 at 16:13
  • I think this is a good answer. Do you feel saying "hardcoded boundary condition" is a meaningful way to distinguish this condition from that which @SomeGuy described? – 1252748 Dec 9 '13 at 16:15
  • @thomas: I'm not totally comfortable with "magic numbers" like the one in your if. Generally, you would define a constant somewhere, like MAX_VALUE, and use the constant for the condition instead of the magic number. – Robert Harvey Dec 9 '13 at 16:15
  • 1
    The 50 is a magic number. Normally, instead of "hardcoding" a stop value there, you would define a constant at the top of your script, such as MAX_VALUE, and say for (var i = 0; i < len && i < MAX_VALUE; i++) But, your mileage may vary; if this is just a small bit of Javascript, it may not matter. My point is that the "hard coding" aspect of this is not particularly significant. – Robert Harvey Dec 9 '13 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.