Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is it correct to call a loop that you do not know the limit at compile time "non-trivial"?

If you know that you will be using a value from a class that will be in a range e.g. between 0-1000, is that still non-trivial?

e.g. Where ISomeInterface.MAX_LIMIT is always between 1-1000:

public bool someMethod(Type someType, int someId, int someVariable)
    bool returnFlag = false;
    int maxLimit = (Activator.CreateInstance(someType) as ISomeInterface).MAX_LIMIT;

    for (int i = 0; i < maxLimit; i++) //is this a non-trivial loop?
        if (_someSerivce.SomeOtherMethod(someType, someId, someVariable, i))
            returnFlag= true;
    return returnFlag;

Edit: Was uncertain as to whether this was a meaningful piece of terminology.

I originally read it in one of Jon Skeet's pieces from yoda.arachsys: (see the section headed Rules Of Thumb).

Do you use this as technical terminology at all?

share|improve this question
I've never heard it described that way. It makes sense, but it's not common terminology, at least in my environment. – JohnB Aug 12 '11 at 10:57
@JohnB, I was afraid of that, updated question – StuperUser Aug 12 '11 at 11:00
up vote 4 down vote accepted

When I've used the term it's usually been in relation to compiler optimizations ... in that context a trivial loop was one that the compiler could fully unroll without creating adverse conditions (ie increasing the block count, removing the ability to inline other code etc) ...

So we generally meant loops with a small number of instructions or few number of iterations (the later being the more common meaning).

With that said, it's definitely a non-standard term without a solid technical definition.

share|improve this answer

It's not a standard term. Personally, I'd consider a loop trivial if the test condition (2nd part) is a simple comparison of a value that changes only due to the counting expression (3rd part of for loop).

Here, the test condition is i < maxLimit (simple comparison on i) and the counting expression is i++ (same variable i). So, yes, trivial.

share|improve this answer
+1 for "It's not a standard term." – StuperUser Aug 12 '11 at 11:17
I think this is the (informal) definition for non-trivial too, as encountered in literature, though I can't find any sources mentioning it. As I see it, non-trivial has everything to do with readability, but not necessarily something to do with performance. They tend to walk hand in hand when it comes to loops, however. – user29079 Aug 12 '11 at 14:26

I can't be sure, but trivial or non-trivial for loops have nothing to do with the range, but rather the increment. A for loop is "trivial" if it is executed at increments of 1, i.e., i++ in your case. The same loop is "non-trivial" if it is not executed at increments of 1, such as executing at every even iteration, etc.

share|improve this answer
Hmmm... would call that loop simple, rather than trivial. – StuperUser Aug 12 '11 at 11:17

A loop is trivial if executing it takes up a trivial part of your program's execution time.

share|improve this answer
Hmmm... this makes the term even less clear and does not seem to be about the loop. If you change the contents of a loop with a method that takes a long time, then the structure of the code that executes the loop hasn't changed, but the loop has changed from trivial to non-trivial. – StuperUser Aug 12 '11 at 11:14
@StuperUser: That's because the underlying code structure is irrelevant- whether it's a loop or a function call or just a segment of a function. What matters is if it is trivial or not. – DeadMG Aug 12 '11 at 22:57
for( int i = 0, k = 1, l=things.size(); i < k && l >= 0 ; --l )
   if( things[i] == things[k] )

This is a non-trivial loop. It's harder to understand than most common ways to write it.

That said, here it's obviously obfuscated. I don't have real world example at hand.

share|improve this answer
Obfuscated and real world aren't necessarily mutually exclusive :) Though I believe you are right, it is non-trivial since it is hard to read. – user29079 Aug 12 '11 at 14:28

First of all, in a broader sense, I would colloquially define non-trivial code by code whose implementation and execution has a significant impact on the nature of the project as a whole. This is obviously somewhat situational, making the term an imprecise one. Context is a big factor in relation to the meaning of non-trivial when talking about a specific piece of code, whether it is a loop or any other structure. There are a few possible scenarios:

  • Performance
  • Design
  • Maintainability
  • Portability

In each of these cases, a code block can be non-trivial, and it means something different in every case.

For your example, you seem to be talking about performance. In the context of performance, I believe a good metric for non-trivial would be its Big-O performance. In your example, the loop is O(n), which in many scenarios is a perfectly acceptable performance, and is the Big-O of a large percentage of for loops.

In the context of performance, one might also say code is non-trivial if its execution time is very large with respect to the total execution time of the whole program.

share|improve this answer

It's not a well-defined piece of technical jargon or a common term of art. Skeet is just using it in the ordinary English language sense: "non-trivial" means "not meaningless."

In that article, "in a non-trivial loop" means "in a loop that takes a meaningful length of time."

share|improve this answer
Jon Skeet. Not John. – cularis Aug 12 '11 at 12:56

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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