News for hackers. Stuff that matters.
Re:Yoda conditions by jedisct1 on Thursday August 15, 2019 @08:46AM (#20704660)
All linters and C compilers emit a warning when an assignment is made in a condition.

There are zero reasons to use that ugly and unnatural Yoda notation in 2019.

Re:Yoda conditions by wruza on Thursday August 15, 2019 @09:22AM (#20704920)

  if (systemcall(“some string”, expression_argument(args), SC_MODE_1 | SC_MODE_DEFAULT) != 0)
  if (0 != systemcall(“some string”, expression_argument(args), SC_MODE_1 | SC_MODE_DEFAULT))
One may find it easier to read/navigate flow control in C code that returns status codes, when these codes are stated beforehand. When there are series of long lines and a mix of 0==success and 0==false, it is easy to get lost, at least in my experience.
Re:Yoda conditions by T-hawk on Thursday August 15, 2019 @11:15AM (#20705926)
I'd break this up by lines, every time.

  int result = systemcall(“some string”, expression_argument(args), SC_MODE_1 | SC_MODE_DEFAULT))
  if (result != 0)
Separation of concerns. Each line does one thing. Making a system call and branching on its result are two separate tasks.
Re:Yoda conditions by skissane on Thursday August 15, 2019 @09:21PM (#20711340)
Until you repeat that code pattern 10 times in one function. Then, do you reuse the same result variable? Or have 10 result variables each with a different name? Or put the whole thing in a block to limit that variable to the scope of that block?

In the past, I've done things like this:

  #define TRY(exp) \
     do { \
        int TRY_result = exp; \
        if (TRY_result != 0) \
           return TRY_result; \
     } while (0)
And then instead of the above I can just write:

     TRY(systemcall(“some string”, expression_argument(args), SC_MODE_1 | SC_MODE_DEFAULT)));

(Sometimes, one wants more complex error detection than just comparison to zero, or more complex error handling than just returning the result code. Often, it is possible to build a more complex version of the above TRY macro to meet those specific requirements.)
Re:Yoda conditions by bbmario on Thursday August 15, 2019 @11:45AM (#20706206)
By that logic, shouldn't you be creating a var for the expression_argument() return?
Re:Yoda conditions by T-hawk on Thursday August 15, 2019 @11:47AM (#20706225)
Indeed I probably would, just didn't want to distract from the central point in that post.
Re:Yoda conditions by wruza on Thursday August 15, 2019 @03:11PM (#20708605)
I would agree with your style, unless all monitors and our vision were built to be more ultrawide-aware than “ultrahigh”. Irregardless of resolution, we can have at most 50-70 lines per eyesight and I doubt that constraint cannot be left unconsidered without losing readability. But it is just my guess from observations, not a real science.

Did you compare writing in different ways and reading it later? This specific style confuses me the most, because I have to mentally connect an assignment with a flow control, because there is no guarantee that a following if() will not use a different variable or it was not shadowed, misnamed, etc. And given that, at once I see x2-3 lines less than usual.

Re:Yoda conditions by admax88q on Thursday August 15, 2019 @09:36AM (#20705037)
Yeah but that's also just a ridiculously long line which will be hard to read regardless ;)
Re:Yoda conditions by wruza on Thursday August 15, 2019 @09:50AM (#20705170)
You either grow it to the right, or in a vertical direction. If the latter, conditions also disperse across that axis. What matters more in a specific case is up to the writer to decide who tries to remain both readable and concise. Highlighting important details in a homogeneous text is part of “the art”.
Re:Yoda conditions by johnfactorial on Thursday August 15, 2019 @09:55AM (#20705207)
Furthermore, when we have a problem, if our solution is to have human beings simply remember to do it a different way, we then have two problems.
Re:Yoda conditions by muench on Thursday August 15, 2019 @01:24PM (#20707391)
I find this quote very powerful. I'm thinking I could re-use it. Is it yours or if it's sourced from somewhere could you share the source?
Re:Yoda conditions by NoodleIncident on Thursday August 15, 2019 @01:32PM (#20707485)
It seems like an intentional reference to this quote (Jamie Zawinski):

"Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems."

Re:Yoda conditions by johnfactorial on Wednesday August 21, 2019 @04:50PM (#20761668)
LOL That quote is exactly what I was cribbing from. :)
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.
Re:Yoda conditions by emilfihlman on Thursday August 15, 2019 @01:39PM (#20707559)
Pretty harsh. Imho it's not ugly nor unnatural, just different.

It also reduces time in debugging as you catch it immediately.

Re:Yoda conditions by weberc2 on Thursday August 15, 2019 @09:56AM (#20705216)
Which is great if your project treats warnings as errors. Not sure how common this is though since every time I compile a C program it’s just pages and pages of warnings.
Re:Yoda conditions by emilfihlman on Saturday August 17, 2019 @06:34PM (#20726859)
Exactly. All code I write must pass -Wall -Werror -Wextra and there's always a debugging phase once you have written the code since there are syntax errors and whatnot, a lot.
Re:Yoda conditions by roland35 on Thursday August 15, 2019 @09:37AM (#20705061)
This is true, but sadly warnings can be ignored! At my last job we enforced -Wall so we had no warnings on pull requests to prevent this though.
Re:Yoda conditions by ur-whale on Thursday August 15, 2019 @02:15PM (#20708027)
A useful aspect of yoda notation is that it often makes an equality test easier to read because the constant is shorter than the expression it is tested against, and the == sign comes right away, not buried far away in the line.

It's my favourite way of writing if statements.

Re:Yoda conditions by codegladiator on Thursday August 15, 2019 @08:48AM (#20704682)
Not all programming languages !
Re:Yoda conditions by saw-lau on Thursday August 15, 2019 @09:33AM (#20705010)
I'm so glad somebody else thinks this! I've always hated that style of notation.
Re:Yoda conditions by na85 on Thursday August 15, 2019 @08:51AM (#20704698)
Do compilers for other languages emit the same warning? Because there are fewer and fewer reasons to use C in 2019 as well.
Re:Yoda conditions by shultays on Thursday August 15, 2019 @08:54AM (#20704716)
Not all languages allows chained assignment (a=b=c) like c++ does, which is why we have this problem (and probably does not automatically cast them to bool either)
Re:Yoda conditions by adrianmonk on Thursday August 15, 2019 @11:20AM (#20705962)
Chained assignment isn't the reason. The similarity of the "=" and "==" operators is.

Specifically, it's both (1) easy to make a typo where you meant "==" but typed "=" and (2) not easy to visually distinguish the two.

Re:Yoda conditions by shultays on Thursday August 15, 2019 @05:02PM (#20709633)
It is part of the reason. In c++ a=b returns a reference to a to allow chain assignments. And also in c++, most stuff casts to bool automatically when you use them as an if condition. When you have both and make the typo, it gives you a relatively silent bug instead of a compile error. Yea sure, the reason is a typo and easily mistakable operators but the actual reason is the code compiles fine

Java for example expects a bool for for if(). if(a=b) is only valid when they are, which is relatively rare. So it is mostly a compile error.

Re:Yoda conditions by adrianmonk on Friday August 16, 2019 @08:26PM (#20720986)
Yeah, valid point. Those other things are factors.

If C didn't allow assignments to be used as expressions, then it wouldn't be an issue.

Chained assignment isn't the only way to use an assignment within an expression, so I think it's still more accurate not to say chained assignment as the cause, but chained assignment might have been a big part of the motion for making assignments expressions instead of just statements.