3

A while loop can be written as follows in C# (and many other languages):

int someValue;
int someTerminatingValue;

while ((someValue = GetSomeValue()) != someTerminatingValue)
{
    // Do something interesting
}

Is there a name for this pattern of assigning the value inside the while loop's expression?

NB I'm not asking if it is a good idea or a bad idea, just if there's a name for this pattern. Whether it's good or bad, people will see it so they need to know what it is. If there isn't a name for it then that's fine, I'm just asking if there is one.

  • 5
    It is not a pattern, it is a consequence of evaluating an assignment expression with the value being assigned (instead of, say, void). – SJuan76 Jul 12 '15 at 17:34
  • 3
    I guess some would argue this is an anti-pattern, because it violates the command-query separation principle and makes the code intention a bit less clear, not to mention the "one-liner" assignment/test makes it a bit harder to spot an error in case of debugging. – heltonbiker Jul 12 '15 at 18:00
  • 1
    You might want to take a look: stackoverflow.com/q/2576571/401828 – heltonbiker Jul 12 '15 at 18:04
  • 1
    @SJuan76 It's definitely a pattern, even if it doesn't have a name or is in any books purely because it is repeated in many code bases. If it repeats then it's a pattern. I'm not implying that it is a good pattern, just that it is a pattern. – BanksySan Jul 12 '15 at 18:15
  • 2
    In the years I've been writing code, I haven't really bumped into this approach, and frankly I'm pleased with that- I would quickly change this if I saw it. This is frankly far too dangerous, without benefit (saves a single line of code..), and more importantly breaks the fluent reading of the code. while (condition) is simply more clear in it's intent than what you have there. Just don't do it, and your question becomes moot. No it doesn't have a name, for it is not common, and there's good reason. – Jimmy Hoffa Jul 12 '15 at 22:45
5

There is no name for this specific idiom.

And it's a good idea™ to write idiomatic code, instead of trying to dumb it down further or writing code which is too clever by half.

Especially if any alternatives are long-winded, tedious, repetitive or otherwise bad.

  • I think this code is idiomatic in C, but in C# I'd rather use an enumerator instead of a function that either returns a value or a special terminator. – CodesInChaos Jul 13 '15 at 8:14
  • @CodesInChaos: See System.Console.Read – Deduplicator Jul 13 '15 at 11:32
  • I know that a few APIs following this pattern exist in C#, but usually foreach together with an enumerable is the cleaner choice. It's certainly rare enough to not count as a C# idiom. – CodesInChaos Jul 13 '15 at 11:45
2

Idiom

At this scale, reoccurring motifs are called idioms rather than design patterns. The design patterns work on a larger scale than just a single line of code or a single loop.

Clever code

This would also qualify as clever code anti-pattern. Clever code is not advisable, because it will be harder to read for your teammates and for yourself 6 moths later.

Better alternatives

Loop initialization, loop "propulsion", loop exit conditions are separated.

    int someValue;
    int someTerminatingValue;

    for (someValue = GetSomeValue();         // initialize the loop variable
         someValue = GetSomeValue();         // move the loop onward
         someValue != someTerminatingValue)  // exit condition
    {
        // Do something interesting
    }

But this is still clever code. McConnell call this "a while loop abusively crammed into a for loop header" (see Code Complete 2, p.374). he also proposes a better alternative.

    someValue = GetSomeValue();                 // initialize the loop variable
    while (someValue != someTerminatingValue)   // exit condition
    {
        // Do something interesting
        someValue = GetSomeValue();             // move the loop onward
    }

Related:

enter image description here (source)

  • 1
    Actually, it's idiomatic good code, thus certainly not an anti-pattern nor too clever for it's own good. – Deduplicator Jul 12 '15 at 18:50
  • @Deduplicator This answer gives some reasons to support its point, but your comment doesn't. Why is is to be considered good code instead of the clever anti-pattern? – heltonbiker Jul 12 '15 at 18:52
  • 1
    This does not answer the question as asked. – user22815 Jul 12 '15 at 18:54
  • @heltonbiker: Actually, it asserts without any support that it's "too clever". But what would be an equally idiomatic alternative? Being idiomatic makes it far better understood, increasing clarity. – Deduplicator Jul 12 '15 at 18:55
  • 2
    Having seen your update by chance, I already discounted your second example. And the first is not better in any but one way: The duplicated code is in consecutive lines, so it's more obvious when the copies get out-of-sync. It buys that one advantage over your other terrible example by abusing the for-loop though, so it ain't any better. – Deduplicator Jul 12 '15 at 22:21

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.