News for hackers. Stuff that matters.
Re:Yoda conditions by beatgammit on Thursday August 15, 2019 @09:53AM (#20705189)
I think this is reasonable readable:

    while ((status = systemcall(...)) != SUCCESS) {
        do something with status;
    }
Leave a comment there explaining the assignment.

Likewise:

    if ((status = systemcall(...)) != SUCCESS) goto error;
Or something like that. I don't understand the hate, just make sure it's obvious what you're doing.
Re:Yoda conditions by stochastic_monk on Thursday August 15, 2019 @10:12AM (#20705358)
It’s traditional to build these kinds of loops in C, e.g.:

    int c;
    while((c = getopt(argc, argv, argstr)) >= 0) {...}
I’d say it’s acceptable, and Python is adding syntax so it can mix assignments and checks.
Re:Yoda conditions by 08-15 on Thursday August 15, 2019 @12:10PM (#20706480)
> Leave a comment there explaining the assignment.

Please don't. This is a perfectly understandable idiom in C. Don't explain the language in your comments; that's the job of a text book, which should be read by anyone before they read your code.

Re:Yoda conditions by marcosdumay on Thursday August 15, 2019 @10:19AM (#20705417)
That's perfectly fine and arguably even better than the alternatives. The `a = b = c = 0;` format is also good.

But enabling it on the language level brings a large amount of risk and complexity just for what amounts to a micro-optimization.

Re:Yoda conditions by Sahhaese on Thursday August 15, 2019 @10:03AM (#20705267)
This would achieve the same but is far more readable:

   while(status != SUCCESS) {
      status = syscall(...);
      // do something with status
   }
Re:Yoda conditions by taneq on Thursday August 15, 2019 @10:11AM (#20705342)
Only if status is initialised to something other than SUCCESS.
Re:Yoda conditions by blktiger on Thursday August 15, 2019 @10:53AM (#20705717)
So use a do-while instead.
Re:Yoda conditions by Izkata on Thursday August 15, 2019 @11:50AM (#20706262)
And this introduces another bug: it'll run the loop body even if syscall fails the first time it's run.
Re:Yoda conditions by dpzmick on Thursday August 15, 2019 @10:16PM (#20711575)
I've been going with this style for things like reading files (with retries) or any sort of loop that feels awkward.

  while (1) {
    int ret = ...;
    if (ret == ...) break;
    if (ret == some_other_condition) break;
    // additional termination conditions....
    // do exactly one thing
  }
Re:Yoda conditions by Izkata on Thursday August 15, 2019 @11:49AM (#20706245)
Your transformation has introduced a bug: the loop body will run once after success turns false.