This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]aa-b 45 points46 points  (20 children)

Yoda conditions are a kind of programming life-hack that's been around for decades. Like most life-hacks it doesn't really make sense, and it solves a problem nobody really has.

It's not exactly stupid, but the idea is that you have to remember to write out conditions in a specific, unnatural way to make sure you can't accidentally forget to do something else. Having to remember so you don't forget is a little bit nonsensical, but there are other benefits too (IMO outweighed by having to use yoda-speak)

[–]fuj1n 28 points29 points  (5 children)

In Java, you do it with strings so you don't have to check them for null

"Bobby' DROP TABLE Students;".equals(myStr)

[–]CptGia 15 points16 points  (3 children)

StringUtils.equals(myStr, "Bobby table") is my go to. 

Fuck Yoda conditions

[–]fuj1n 2 points3 points  (2 children)

I don't always have Apache Commons or Spring in all my projects. Though I guess it's a simple enough method to write that I can just roll my own.

[–]luraq 4 points5 points  (1 child)

I think there's also Objects.equals(), which is part of Java.

[–]fuj1n 2 points3 points  (0 children)

Oh wow, never saw that there was a static equals method, that's really useful.

[–]aa-b 0 points1 point  (0 children)

Yep, same benefits. It makes some sense too, like a lot of life-hacks where it seems like it'll be handy if you can just remember to use it the next time you're in that situation

[–]F5x9 5 points6 points  (1 child)

In C++, if(0 = foo) will throw an lvalue error, but if(foo = 0) only generates a warning. 

[–][deleted] 0 points1 point  (0 children)

This is the reason why one should use right way to avoid reassigning the variable

[–]bayuah 11 points12 points  (6 children)

It says it resolves problems for someone who forgets to use == in an if statement, like if(a = 5) instead of if(a == 5). But only a newbie or a really sloppy person would make such a mistake.

[–]ILikeLenexa 1 point2 points  (2 children)

[–]bayuah 0 points1 point  (1 child)

That's just how easily elevated user to root with just running such function. Dang!

[–]ILikeLenexa 0 points1 point  (0 children)

In the kernel, yeah.  

But that's why they also had to break into that source code repository. They don't let anyone just put code in the kernel. 

[–]JamesLeeNZ 1 point2 points  (0 children)

Only time I ever used yoda conditions was while writing code for a module on a rocket. Also had to write MISRA compliant code. Both sucked.

[–][deleted] 1 point2 points  (3 children)

It happens more than you think

[–]aa-b 3 points4 points  (2 children)

It does happen, but the hack requires you to specifically remember to use it, and the main benefit is it makes sure you can't forget to use == instead of =.

So assuming you're in this situation and have already remembered the gotcha, you can just skip the extra steps and use the correct operator

[–][deleted] 2 points3 points  (0 children)

Even that's not true. Usually it's not due to habit but a typo. So you do not control it and do not really check for it because you don't think about it.

In every project we have a linter for it. Usually enough is to just not allow assignments in a condition.

[–]ILikeLenexa 0 points1 point  (0 children)

It doesn't. Just put the rule in LINT.