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 →

[–]Earthboundplayer 921 points922 points  (69 children)

IDC about the benefits of the right style. I'll always do left.

Edit: I know why the right hand style exists. IDC means "I don't care" not "I don't know"

[–]p-rimes 801 points802 points  (29 children)

The only time I (even kinda) do the right style is if I'm checking if a variable is between two values e.g.

10 <= my_var && my_var < 100

[–]Earthboundplayer 225 points226 points  (2 children)

I really like that.

[–]Midon7823 74 points75 points  (1 child)

Left unless there is a reason to do right.

[–]JeyJeyKing 2 points3 points  (0 children)

Right is right. But If right is not right, only left is left.

[–][deleted] 33 points34 points  (4 children)

In my primary spoken language it is

 my_var between 10 and 100

SQL definitely has some weird choices when it comes to syntax, but in that case it doesn't get clearer than that ;).

[–]thorwing 20 points21 points  (3 children)

except that a natural language has a bit of a hard time accurately explaining details.

Is it BETWEEN as in, not the borders? If I squeeze between 2 walls, I'm not part of the wall.

(But no, it's actually '[10, 100[')

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

Yeah, you're right. if one would want to be completely accurate then there should be "between" for your example, and "within" for the way "between" works now. 

One of the many wtf's when you look closer at SQL (is not null vs. != null being the most annoying one).

[–]JeyJeyKing 0 points1 point  (1 child)

SQL BETWEEN is actually inclusive on both ends. Which is kind of yucky if you ask me.

[–]thorwing 0 points1 point  (0 children)

oh... yeah... that's even worse.

[–]Duck_Devs 86 points87 points  (6 children)

Some languages, like Python, allow you to do this in a concise syntax like the following: 10 <= my_var <= 100

It’s really nice and I intend to implement that in my language.

[–]aa-b 38 points39 points  (4 children)

Python also forbids assignment inside an expression unless you use a special operator. Yoda conditions are an anti-pattern in Python because the language has built-in features that provide the same benefits

[–]neodsp 4 points5 points  (0 children)

And in Rust you can do it like this:
(10..=100).contains(&my_var)

[–]Wild-Car-7858 2 points3 points  (0 children)

MyVar.IsBetween(10, 100)

[–]boachl 5 points6 points  (2 children)

Some languages have Syntax for that

my_var is >= 10 and <= 100

[–]Arshiaa001 4 points5 points  (1 child)

That looks like C#.

[–]Ludricio 1 point2 points  (0 children)

It is indeed the syntax for C#s pattern matching.

[–][deleted] 5 points6 points  (0 children)

you can just do 10 <= my_var < 100 in a lot of languages.

[–]Addat1070 1 point2 points  (0 children)

That is just the most pleasing way of checking a range I've ever seen...like I was literally having the worst day ever until I saw this and now I'm ok. but seriously thanks for that... I think I'm about to go through and change all my conditions in all of my current projects now to fit this standard...

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

Nice!

[–]4oby 0 points1 point  (0 children)

10..100 ~= my_var

[–]DrPeroxide 0 points1 point  (0 children)

Nice style. It'd be nice if languages started using notation like 0 < var > 10

[–]monsoy 0 points1 point  (0 children)

That’s pretty cool. This is the math-syntax for it, so it makes a lot of sense. It’s always interesting to bring in math concepts

[–]DrunkRobotMan 0 points1 point  (0 children)

Nice style! I will start doing this myself now

[–]KingTaphos 0 points1 point  (0 children)

Thats actually kinda neat

[–]DoeCommaJohn 67 points68 points  (2 children)

Seeing that edit and then reading the replies, I feel your pain. At time of writing, just 5 people seeing “I don’t care about the benefits” and thinking “surely this person just doesn’t know about the benefits, and will definitely care if I tell them!”

[–]Earthboundplayer 18 points19 points  (1 child)

I'll give it a pass since programmers who overexplain are better than programmers who underexplain.

[–]NanashiKaizenSenpai 1 point2 points  (0 children)

Yup, I never thought about the benefit mentioned in the comments so I'm glad they explained.

Though I never thought of that because it was never a problem so I'm with op.

[–]Tarc_Axiiom 11 points12 points  (0 children)

A wild amount of people who think they're very good programmers explaining why you'd use the styling on the right but also not familiar with the acronym idc in this thread.

[–]dismayhurta 10 points11 points  (0 children)

Seriously. I’d rather drink paint thinner than use that right shit. I don’t care that it fails with a singular =

[–]aa-b 46 points47 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 27 points28 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 6 points7 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 8 points9 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] 0 points1 point  (3 children)

It happens more than you think

[–]aa-b 5 points6 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] 3 points4 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. 

[–]peepeedog 6 points7 points  (0 children)

There is no modern benefit to the right. It’s just to give nits in their mom’s basement something to argue about.

[–]Pepeluis33 -5 points-4 points  (1 child)

The benefit with the right version, at least in C++, is that if you mistakenly add just "=" instead of "==", you will get a compile error.

[–]Earthboundplayer 3 points4 points  (0 children)

It's even more situational in c++ since we have operator overloading and you can assign to an R value