News for hackers. Stuff that matters.
Re:Yoda Conditions by michaelmior on Friday February 17, 2017 @10:55AM (#13669067)
While I don't disagree, curious to hear your reasoning for not doing this.
Re:Yoda Conditions by paol on Friday February 17, 2017 @11:15AM (#13669283)
This trick can only save people who remember to use it. Warnings emitted by tooling save everyone.

Also it's slightly awkward to read, as mentioned by others already.

Re:Yoda Conditions by yellowapple on Friday February 17, 2017 @01:03PM (#13670285)
Warnings can only save people who bother to read them. Yoda conditionals save everyone trying to run or compile the code in question. It goes both ways ;)

Re: awkwardness, it doesn't feel awkward at all to me; given that Erlang and Elixir use assignment for destructuring and pattern-matching, Yoda conditionals feel very natural to me in comparison.

For example, this is valid Elixir code:

    foo = {1, 2}
    
    bar = if {baz, 2} = foo do
            525600
          end
    
    IO.inspect foo  # prints {1,2}
    IO.inspect bar  # prints 525600
    IO.inspect baz  # prints 1
Re:Yoda Conditions by busterarm on Friday February 17, 2017 @12:30PM (#13670035)
The trick saves the person writing the code and anyone modifying it down the road.

You can't force everyone who edits your code to have linters and warnings turned on. Not everyone follows bests practices.

Re:Yoda Conditions by michaelmior on Friday February 17, 2017 @12:35PM (#13670072)
Does it? You also can't force everyone who edits your code to follow your conventions.
Re:Yoda Conditions by digler999 on Friday February 17, 2017 @05:08PM (#13672135)
actually you can, depending on the management structure in an organization :)
Re:Yoda Conditions by mikeash on Friday February 17, 2017 @10:59AM (#13669114)
It's unnatural, confusing to read, and won't save you anyway in cases where you're comparing two variables.

It's OK if your tools absolutely can't diagnose accidental if(a = b), but it should be the last resort.

Re:Yoda Conditions by akerro on Friday February 17, 2017 @11:04AM (#13669159)
In some languages it's better to use it, eg. in Java you can go with:

   mString != null && mString.equals("test")
or

   "test".equals(mString)
They do the same, but the second one adds hidden `defensive programming` to prevent NPE in `equals`. Saves time AND code.

Intended usage of if(a=b) should be written as if((a=b)). Don't know what tool you use at work or at home, but most static code analysers will point you that. Try SonarQube at least.

Re:Yoda Conditions by scj on Friday February 17, 2017 @11:19AM (#13669332)
> It's unnatural, confusing to read ...

I feel as if that can rephrased "Don't do it because it is not done." A left-hand side constant is a convention that can have practical benefits in limited cases. Which at least should merit consideration.

Re:Yoda Conditions by mikeash on Friday February 17, 2017 @11:29AM (#13669424)
"Don't do it because it is not done" is a good reason unless you're working solo and are sure your code will never be seen by others. And even then, it's a good idea to stick to what's done so you don't accumulate bad habits for projects that do have others looking at the code.
Re:Yoda Conditions by pasquinelli on Friday February 17, 2017 @01:38PM (#13670577)
how would you write a yoda conditional with two variables? which is the yoda conditional (a == b), or (b == a)?
Re:Yoda Conditions by mikeash on Friday February 17, 2017 @02:28PM (#13670960)
You can't if they're both mutable, since the whole idea of a yoda conditional is to put an immutable thing on the left side. What I meant was that you can't use them in that case, so it's a technique you can't use with 100% consistency.