Live data from Hacker News

Yoda conditions

en.wikipedia.org

91–100 of 116 posts

Re: Yoda conditions

#92
post #89

Earlier quoted context omitted.

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.

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?

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

#93

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.

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

#95

Earlier quoted context omitted.

Are they really easier to read though? A lot of the time which variable being tested is obvious, so it makes sense to put the important information - the value - first.

> so it makes sense to put the important information Both sides of the equality check are equally important.

I think he should have written "it makes sense to put the non-obvious information first" as it contrasts better with the first part of the sentence

Re: Yoda conditions

#96

Yoda conditionals are still very popular in the PHP/Wordpress space. I never got used to using them personally.

They're required per the WordPress PHP Coding Standards and flagged by the WordPress rules for PHPLint. Lots of WordPress developers stick to those standards, so that's why you see it everywhere.

Re: Yoda conditions

#98
post #16
post #4

Am I sure pretty any that linter warning a throw can of instead breaking flow the both of reading writing and.

Despite the name, it isn't referring to Yoda's grammar form, it's just reversing things around "and" and "or". If you really have a problem telling that "x == y" is the same as "y == x" I suggest coming to a deeper, more complete understanding of the equality operator as a commutative operator where the order doesn't matter rather than thinking of it as "variable is? value", as so many students clearly pick up accide…

[deleted]

Re: Yoda conditions

#99

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.

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

#100

I know I'm in the minority, but I honestly never really understood why people get so up in arms about this. People keep saying it's so difficult to read, and I just don't understand why. I know that linters and such can catch this most of the time, but a lot of places I've seen don't use them. (I don't know why, that's a whole other discussion.) It just seems to me that it's something that keeps you from wasting time…

You must be young, friend. The holy wars that used to rage over just where to put curly brackets... (And then Python came along and was like, "U wot mate?", and there were had little wars over "syntactically-significant indentation" and tabs vs. spaces, and how many spaces ... And on, and on...)

Oh no, I'm not young. I just find this particular argument makes less sense than all the other types of arguments like this. Bracket placement in some languages is necessary to not have unintended effect. Tabs vs spaces can cause problems with certain ides/editors. I can somewhat understand most justifications as to why people have a certain reason for formatting code in their particular way. You want me to indent 3 spaces instead of 2 in Delphi, because that's the way you've done it, fine. I don't care. But this particular one, I don't get it. I never have. The amount of time that this one particular trick saved me in terms of time and frustration is huge. When you spend 45 minutes tracking down a problem because you assigned 5 (or something) to variable by accident, it almost always leads to a mouse throwing event. When someone comes back and says that seeing 42 == x is too difficult to read even though it can save countless hours, I just don't understand. (not every project has the benefit of having a linter or a compiler that will catch things like this.)
Post reply on HN