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
  • 1
    It might be a form of a guard: en.wikipedia.org/wiki/Guard_(computer_science) Commented Dec 9, 2013 at 16:19
  • @FrustratedWithFormsDesigner This sounds pretty much exactly right. Why did you submit as a comment and not an answer? Commented Dec 9, 2013 at 16:20
  • Ok, I'll write it up as an answer. Commented Dec 9, 2013 at 16:23
  • Aren't guards usually in a separate if statement, or at least separated by an else clause? Commented Dec 9, 2013 at 16:25
  • @RobertHarvey: I think this could be considered a more compact guard that can't have an else. Commented Dec 9, 2013 at 16:28

2 Answers 2

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 Commented Dec 9, 2013 at 16:36
  • @Izkata: Yes, that's a very good point! Commented Dec 9, 2013 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)? Commented Dec 9, 2013 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) Commented Dec 9, 2013 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. Commented Dec 9, 2013 at 16:11
  • @SomeGuy "hardcoded boundary condition"? Commented Dec 9, 2013 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? Commented Dec 9, 2013 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. Commented Dec 9, 2013 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. Commented Dec 9, 2013 at 16:23

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.