15

Code style arguments are stupid and waste of time, thus I never have them.

But fuck I hate yoda conditions, most stupid shit someone could come up with. It’s not even about the style, it’s about the way how one must change their thinking to evaluate condition from the opposite side...

Comments
  • 2
    Same for me. It's so awkward to read. Really slows me down.. and enrages me everytime.
  • 1
    Honestly I like it because you can avoid checking if some vars are null. Of course it is far from necessary.

    Still, I don't get why the php community has a fetish for that
  • 7
    Seems to me that the use of Yoda conditions rose from a lack of good IDEs or linters. If you reverse the comparison you won't get fucked by missing an equals sign, making the consition an assignment.

    Don't see the need today, don't like it, but I won't hate on it knowing where it came from.
  • 10
    The reason is that it prevents the error where someone types = instead of == in a comparison, which is still valid code in many languages, but it doesn't do what's intended. If there is a constant at the left side, then this will become an obvious syntax error.

    That said, I also hate this because a good compiler or static analyser should warn about that anyway unless some specific silencer like double parantheses is used.
  • -1
    Is there even a rationale or an otherwise sane explanation behind the use of Yoda notation?
  • 5
    @kamen How about just reading the two comments before yours?
  • 1
    PHP community seems obsessed with yoda style, even when that brings no bennefits like not comparing for equality which is a bloody pain in the ass to read (to compare if value is less or equal: var <= 5 vs 5 >= var) what any sane person would read as - is 5 greater or equal to var.
    Linters can fix this for you, i.e. convert to Yoda style if that is a must, so you can still use human readable style. But it won't prevent you from making a mistake, a good ide or editor will warn as well as linters (e.g. as pre-commit hook). AFAIK, that's what symfony uses so you get Yoda style everywhere, even when comparing constant to function result (CONST == func()) which is unnecessary.
Add Comment