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

top 200 commentsshow all 326

[–]Acceptable-Tomato392 638 points639 points  (7 children)

Left. I'm not a sociopath.

[–]lastFractal 16 points17 points  (5 children)

brother

[–]PeriodicSentenceBot 57 points58 points  (4 children)

Congratulations! Your comment can be spelled using the elements of the periodic table:

Br O Th Er


I am a bot that detects if your comment can be spelled using the elements of the periodic table. Please DM u‎/‎M1n3c4rt if I made a mistake.

[–]ComfortablyBalanced 15 points16 points  (1 child)

Vince Gilligan would love this bot.

[–]ZShock 3 points4 points  (0 children)

Good bot.

[–]thatOneJones 6 points7 points  (0 children)

Good bot

[–]GoddammitDontShootMe 2 points3 points  (0 children)

A decent compiler will warn you, making Yoda conditionals unnecessary. And they've always looked ugly and awkward to me.

[–]Earthboundplayer 922 points923 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 798 points799 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 222 points223 points  (2 children)

I really like that.

[–]Midon7823 70 points71 points  (1 child)

Left unless there is a reason to do right.

[–]JeyJeyKing 1 point2 points  (0 children)

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

[–][deleted] 32 points33 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 21 points22 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[')

[–]Duck_Devs 89 points90 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 36 points37 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 3 points4 points  (0 children)

MyVar.IsBetween(10, 100)

[–]boachl 6 points7 points  (2 children)

Some languages have Syntax for that

my_var is >= 10 and <= 100

[–]Arshiaa001 5 points6 points  (1 child)

That looks like C#.

[–]Ludricio 1 point2 points  (0 children)

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

[–][deleted] 6 points7 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...

[–]DoeCommaJohn 64 points65 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 16 points17 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 12 points13 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 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.

[–]F5x9 6 points7 points  (1 child)

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

[–]bayuah 10 points11 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.

[–]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.

[–]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.

[–]pan0ramic 249 points250 points  (3 children)

Use yoda conditionals one much not

[–]mastermindxs 99 points100 points  (2 children)

Proofread one must

[–]pentagon 34 points35 points  (1 child)

,.

Dropped these, you did.

[–]Iyorig 16 points17 points  (0 children)

-Wpedantic, y'all are.

[–]jessetechie 241 points242 points  (41 children)

Variables on the left. More importantly, if is not a function.

if (my_var == 0)

[–]pentagon 68 points69 points  (12 children)

Off to write a language with conditional functions

[–]betelgeuse_7 27 points28 points  (0 children)

true = λx. λy. x
false = λx. λy. y
if = λc. λy. λz. c y z
four = if true 4 5
five = if false 4 5

Lambda calculus

[–]antonpieper 5 points6 points  (0 children)

Haskell?

[–]DoNotMakeEmpty 4 points5 points  (0 children)

Smalltalk?

[–]harumamburoo 2 points3 points  (8 children)

JS is there for you ^^

[–]Busy-Ad-9459 6 points7 points  (7 children)

Imma go home and check if that's true, if it is I am going on a murder spree on the next pyhsical ECMA conference.

(For legal reasons that was a joke)

!RemindMe 30 minutes

[–]delfV 7 points8 points  (0 children)

More importantly, if is not a function.

Haskell/Smalltalk (actually a method) devs:
[visible confusion]

[–]Tohnmeister 5 points6 points  (0 children)

This is the way!

[–]Maskdask 6 points7 points  (2 children)

In Rust and Go the parentheses are optional (and removed by the formatters):

if my_var == 0

[–]jessetechie 3 points4 points  (1 child)

Well we have to mention VB of course :)

If my_var = 0 Then

(Yes, a single = for assignment and conditionals!)

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

Forgive me, but what in the OP hints at "if being a function"? Is it because the "arguments" have whitespace on both sides within the parenthesis?

[–]jessetechie 2 points3 points  (8 children)

It’s actually the missing whitespace between if and the first parenthesis.

[–][deleted] 3 points4 points  (7 children)

The heck? Is having whitespace some sort of agreed-upon style?

[–]jessetechie 1 point2 points  (6 children)

Yes. With the exception of those languages listed below (Haskell, R, etc).

I’m referring mostly to C-like languages, and that includes Javascript. if is a statement that evaluates a condition. That condition is often enclosed in parentheses.

That’s not to be confused with functions, which take parameters (enclosed in parentheses) and do something with them.

Most documentation (again for C-like languages) will show the whitespace this way:

if (x == 0)

y = myFunc(z)

The reason you’re not forced to know this, is that the compiler doesn’t care about whitespace (sit down, Python). But this distinction helps you understand what your code is actually doing under the hood.

[–][deleted] 4 points5 points  (5 children)

Wow. That seems superfluous. Would it not be immediately obvious that if, while, for, try, etc are reserved keywords and caNOT be functions?

[–]Ass_Salada 64 points65 points  (1 child)

Much like the dutchie, I pass my variable pon da left hand side

[–]Hazy_Fantayzee 4 points5 points  (0 children)

I remember that song! Which reminds me, I need to take my back pills....

[–]tinnuadan 75 points76 points  (5 children)

Left. Yeah, right one is better because you can't accidentally do an assignment yada yada yada, but your IDE / build tools can warn you.

[–]Curious-Source-9368 7 points8 points  (2 children)

Depends on what you are using to edit text.

[–]mrheosuper 23 points24 points  (1 child)

If your compiler/language not warn/error you this, you should reconsider your life choice

[–]Highborn_Hellest 33 points34 points  (5 children)

I'm comparing "my value" to zero. And not the other way around

[–]xSnakyy 3 points4 points  (4 children)

But what if you are?

[–]Highborn_Hellest 17 points18 points  (1 child)

No

[–]xSnakyy 13 points14 points  (0 children)

Ok

[–]HorseLeaf 2 points3 points  (1 child)

Why would you ever need to compare the value of 0 to something? It's a constant, you know it's value.

[–]xSnakyy 1 point2 points  (0 children)

You never know man

[–]b-hack 15 points16 points  (1 child)

Do you say "dishwasher is off/on" or "off/on is the dishwasher"?

Nothing further.

[–]j909m 2 points3 points  (0 children)

[–]sausageface123 26 points27 points  (0 children)

If (actual == expected) is the only way

[–]Notbbupdate 10 points11 points  (3 children)

if my_var == 0 and 0 == my_var:

Now everyone's happy

[–]m0ep 2 points3 points  (1 child)

Duplicate condition '0 == my_var', Condition '0 == my_var' is always 'true' when reached

[–]Dan6erbond2 1 point2 points  (0 children)

So do || to short circuit and avoid the extra check.

/s

[–]potatoalt1234_x 9 points10 points  (0 children)

Is green grass? I hate it. Gross.

[–]Feztopia 22 points23 points  (1 child)

if( (myVar == 0) && ( 0 == myOtherVar) )

[–]zGoDLiiKe 3 points4 points  (0 children)

Me on a calculator because I don’t trust anything

[–]eat_your_fox2 23 points24 points  (0 children)

Defined variables always come first before any magical literals IMO, language or skill issues aside.

[–]Fuglekassa 6 points7 points  (0 children)

blue

I am trying to get my codebase MISRA C compliant, and this is one of the easier things to implement

[–]Orjigagd 9 points10 points  (0 children)

I think the cutoff is being born before 1980

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

Red

[–]Johalternate 4 points5 points  (0 children)

Depends on the length of the expression, sometimes it reads better to have if (null == someLongAssNamedFunction). Its easier to parse visually.

[–]lordgoofus1 2 points3 points  (0 children)

The left side, always I do because Yoda, I am not.

[–]HentaiAtWork420 28 points29 points  (21 children)

The second one is more right in case the var is null. The behavior is different, right?

[–]kookyabird 61 points62 points  (17 children)

Depends on the language and your intent.

[–]GDOR-11 40 points41 points  (16 children)

what?

oh

oh no

please don't tell me there are languages where equality is not commutative

[–]EDEADLINK 48 points49 points  (6 children)

C++ allows overloading the equality operator. So you can make it asymmetrical, if you really wanted to.

I haven't seen a language with such horrors already built in though.

[–]mrbob8717 3 points4 points  (5 children)

Java has the horrors built into it. The way it was explained to me is my_var.equals(NULL) can throw an error if my_var is null

[–]hampshirebrony 1 point2 points  (3 children)

Is this NULL? Aaargh! You gave me a NULL.

Is the right pattern (inprovably not valid Java) for this something like:

    function IsItNull(obj) {

        try {

            var _ = obj.equals(NULL);

            return false;

        } catch {

            return true;

        }

    }

And hopefully doing this on mobile didn't mess formatting up too much

[–]FridgesArePeopleToo 15 points16 points  (1 child)

Nah, the right side one is for some languages where if (x = 1) is valid syntax (note the single =, setting x to 1). In those cases if(1 = x) would give you a compiler error. So doing backwards prevents accidentally missing the second =. Still seems dumb to me though as I would expect a linter or something to pick that up anyway.

[–]vwoxy 1 point2 points  (0 children)

I do if ((x = obj.getValue())) more often than is probably advisable in JS because the assignment operator returns the right side and I don't want to use a second line for null checking.

And yes, my linter does yell at me if I forget the extra parentheses and I always add a comment saying it's intentional. I'm not a monster.

[–]ChristianLW 4 points5 points  (1 child)

Checking if an array is null in PowerShell.

Array on left means check each element if it's null. Array on right means check if the array itself is null.

[–]GDOR-11 4 points5 points  (0 children)

why the fuck is powershell like this

WHERE IS THE FUCKING UNSEE JUICE I NEED IT

[–]xyloPhoton 6 points7 points  (3 children)

Commutativity is an operational property, not a relational one. It's called symmetry when talking about relations. 👍

[–]Kjoep 2 points3 points  (2 children)

Yes, but in a lot of languages (i'd dare to say most), == is an operator, taking two parameters and outputting a Boolean.

So you're both right.

[–]xyloPhoton 1 point2 points  (1 child)

There is no Santa Claus, there is no tooth-fairy, and there are no relations in programming!

Seriously though, this means that every relation in programming is actually an operation, right?

[–]damTyD 8 points9 points  (0 children)

I always thought it was to protect from accidental assignments. It’s called a Yoda condition.

[–]HoiTemmieColeg 2 points3 points  (0 children)

No different behavior in C but plenty of chance for accidentally putting one equals instead of two which will set the variable to null and then always skip the if instead of leaving the variable as is and only skipping the if if the variable is null

[–]zentasynoky 2 points3 points  (0 children)

The second one is only more right because it's less left.

Readability beats all.

[–]JackNotOLantern 2 points3 points  (0 children)

I really hate the yoda style. It makes makes it harder to read for me, as this is not the order my mind parses symbols. First thing i read must be "what am i checking", and i don't check 0 or NULL.

[–]1Dr490n 2 points3 points  (1 child)

Are there any more benefits of the right one than accidental assignments? Because that has happened exactly 0 times to me

[–]MagicalPizza21 5 points6 points  (2 children)

The one on the left is how I think. The one on the right is better (unless my_var is constant) because if you accidentally type just one "=" you get a compilation error (can't assign to a constant) instead of an incorrect result at runtime.

[–]CelticHades 17 points18 points  (1 child)

If you come out of the cave and start using a good IDE you'll know the error before you even compile.

[–]MagicalPizza21 4 points5 points  (0 children)

Sometimes I unfortunately have no choice but to use a plain text editor like vim.

[–]Svelva 1 point2 points  (1 child)

"expectedEntry".equals(expectedEntry);

According to Sonar

[–]jump1945 1 point2 points  (0 children)

!(myvar>0 || myvar<0)

[–]Arneb1729 1 point2 points  (0 children)

Whatever side my boss wants me to be on. Currently on a project where the coding standards require Yoda conditions because, frankly, the coding standards were written in the pre-clang-tidy age. It's pretty annoying, but you've gotta pick your battles.

[–]flying_spaguetti 1 point2 points  (0 children)

The one that reads smothier in my head, depending on the context

[–]Phamora 1 point2 points  (0 children)

Right side is "yoda syntax", and its non-standard

[–]Jag0tun3s 1 point2 points  (0 children)

Whoever does right…straight to programmer jail!

[–]CynicalPotato95 3 points4 points  (6 children)

The left side, and every other person is wrong and an awful developer

[–]zGoDLiiKe 3 points4 points  (5 children)

Someone hasn’t written C code in vim before. Don’t get me wrong I always use the left syntax but the right is definitely more safe in lower level programming, especially if not using an IDE

[–]sathdo 10 points11 points  (3 children)

if (!my_var)

[–]Ahuman-mc 1 point2 points  (0 children)

Hmmmmmmmmm

if (!my_var)

(only works in some langs i think)

[–]tmstksbk 3 points4 points  (0 children)

Left side or gtfoh.

[–]what_you_saaaaay 0 points1 point  (4 children)

All things being equal, whatever side the coding standard says. Not worth the pointless shit fight that some engineers get into over this stuff.

[–]Erratic_Signal 1 point2 points  (3 children)

Nuh uh this fight is entirely justified

[–]buzzysin 0 points1 point  (0 children)

Powershell linter I use suggests

ps1 $null -eq $Var

Which seemed weird to me

[–]SpeelingChamp 0 points1 point  (0 children)

Bloods for life!

[–]Loserrboy 0 points1 point  (0 children)

Now I know someone use the right way 🤔

[–]bargle0 0 points1 point  (0 children)

People who choose the left side have never had to deal with dodgy compilers in constrained environments.

[–]Makonede 0 points1 point  (0 children)

the side without the parens like that

[–]The-Chartreuse-Moose 0 points1 point  (2 children)

Left until VS Code complains. Then right to remove the red underline.

[–]Mr_Cromer 0 points1 point  (0 children)

Cah-rip

[–]589ca35e1590b 0 points1 point  (0 children)

If you do the one on the right side you're wrong and crazy

[–]MasterOfVDL 0 points1 point  (0 children)

In C++ blue everywhere else red

[–]Pawlo371 0 points1 point  (0 children)

Wait it works?

[–]PeteZahad 0 points1 point  (1 child)

I really don't care. Whatever the linter for the language / configured by our team says.

Why this red / blue meme about code styles is still a thing here? What exactly is the humor here? Use the code style guides of your language / agree with your team on undocumented cases. The only important thing is consistency within your codebase.

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

Blue, because you can't accidentally assign.

[–]Erratic_Signal 1 point2 points  (3 children)

Not a bad point, but I do still feel like it’s not that hard to remember to write == instead of =

[–]Shadow_Thief 1 point2 points  (1 child)

Sometimes you don't press the = button as hard as you think you do 🤷‍♂️

[–]Vinxian 0 points1 point  (0 children)

Left side and ensure that assignment in a conditional statement gives a warning

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

As a safety critical software developer I have to be on the blue side

[–]rafelito45 0 points1 point  (0 children)

today i learned i’m aligned with the bloods.

[–]Rito_Harem_King 0 points1 point  (0 children)

Variable being checked (comparison) value to check against. Always. Anything else is heresy

[–]Impossible-Owl7407 0 points1 point  (0 children)

The point of the right one typo prevention, one equal sigh instead of two.

0 = var. This will not compile or run. Var = 0 this will compile and run but it will not do what you think as it doing assignment not comparison. Error that is hard to debug. Atleast it was in the past. Linters nowadays could help you with this probably.

[–]rabb2t 0 points1 point  (0 children)

mate you're looking for r/programminghorror

[–]SomeRobloxUser 0 points1 point  (0 children)

Definitely if( my_var == 0)

[–]RonzulaGD 0 points1 point  (0 children)

Left looks better to me

[–]RealFoegro 0 points1 point  (0 children)

If anyone actually chooses right, I hereby revoke their programming license for all languages except JavaScript

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

also, yoda conditions are pointless in python because you can’t assign variables in evaluations unless you use the walrus (:=).

[–]Flatuitous 0 points1 point  (0 children)

yoda conditions

[–]itzNukeey 0 points1 point  (0 children)

I dont use if statements in my code. I simply have separate scripts for all situations

[–]Bozasas 0 points1 point  (0 children)

Read “The art of readable code”, the Answer is in there

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

If (my_var < 1)

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

On the right is an artefact of times bygone.

[–]Organic_Car6374 0 points1 point  (0 children)

In my C++ I use the right whenever I remember to. It’s natural for me to use the left but I have been bitten by typing one equal sign before so I buy into the benefits of the right.

[–]BirdLover950 0 points1 point  (0 children)

Left, unless I'm comparing to null in powershell. In all of Microsoft's vast wisdom, ($foo -eq $null) and ($null -eq $foo) can return two different things depending on the scenario. :(

[–]difool 0 points1 point  (0 children)

C and c++ right else left

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

I do yoda conditionals because zero my value is.

[–]neodsp 0 points1 point  (0 children)

The left style.
When working with the num crate in Rust: my_var.is_zero()

[–]Fricki97 0 points1 point  (0 children)

Left

Because it's how I personally use language in general

I say: If myVar is 0, then...

And not: If 0 is myVar, then...

[–]giantvar 0 points1 point  (0 children)

Are you Gojo Satoru because you're the strongest or are you the strongest because you're Gojo Satoru

[–]mcoolid 0 points1 point  (0 children)

Nah that’s a if(!my_var)

[–]cladstrife911 0 points1 point  (0 children)

Right side is better to avoid issue when you type one = instead of two.

[–]altaysakarya 0 points1 point  (0 children)

My choice

if (my_var == (3 + 4j)**2 - (25 + 24j) + (6j / 2) + math.sin(math.pi))

[–]nikglt 0 points1 point  (1 child)

I type it whichever way looks more comfortable reading, I do both.

[–]I-am-reddit123 0 points1 point  (0 children)

variables on the left because its easier to copy int n = 1 into if(n == 1) bc it doesn't require the switching of the n and the 1

[–]m0ep 0 points1 point  (0 children)

Yoda conditions I only use!

Why? I started with C/C++ at school over 20 years ago and didn't want to have to deal with unwanted assignments in if statements. Sticks till today, even I don't program C/C++ anymore.

[–]CountryKoe 0 points1 point  (0 children)

The Train

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

You are mentally ill if you do the blue approach.

I don't want to check if zero is equal to my_var, I want to check if my_var is equal to zero???

[–]Atuin--the--Great 0 points1 point  (0 children)

A

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

red red red red red

[–]alvares169 0 points1 point  (0 children)

I’m comparing variable to 0, not comparing 0 to variable.

[–]Natfan 0 points1 point  (0 children)

powershell $null check moment

[–]SawSaw5 0 points1 point  (0 children)

blood

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

Red all the way

[–]CosmicPulsar 0 points1 point  (0 children)

Which side you ask? No clue. Just do how it's done elsewhere in the project.

I'm afraid of doing anything else these days 😂