Why Some Programmers Code Like Yoda

preview_player
Показать описание
Yoda notation is one of the most controversial practices in computer science. Some people will die for yoda notation, while others will die trying to stop it. In this video, we discuss what Yoda Conditionals, better known as Yoda Notation, is, the pros and cons of using it, and why so many people are fighting it to this day.

🏫 COURSES 🏫

🔥🔥🔥 SOCIALS 🔥🔥🔥
Рекомендации по теме
Комментарии
Автор

You forgot to mentioned that it's also awkward/unnatural to read.

All modern compiles throw warnings if '=' is used in 'if' statements. So, I'd say that there is no real advantage of using Yoda notation anymore.

We don't live in the 90s anymore 😉.

jarosawwierzbicki
Автор

Let’s be real, if you have the awareness to think about the order, you aren’t gonna forget a double equal sign

Renni-kgvf
Автор

Classic programming "improvements": shooting yourself in the foot by making code harder to read to prevent an error with occurrance rate of 0.001%

oancemr
Автор

Programming in a high level language is literally about readability since inception. Yoda Notation is a solution looking for a problem. It makes code less readable and no one would make this tradeoff for the minor use case where it might help prevent a small logic error that should be correctable in 30 sec.

chbrules
Автор

to everyone who says that the compiler will warn you about this, thats assuming you pay attention to warnings in the first place. Tons of codebases have all kinds of detritus coming out of the compiler that gets ignored. Or a 10+ minute build where you get up and go for coffee, you aren't going to see the compiler warning.

mcpr
Автор

if a junior programmer is unknowingly writing an assignment instead of boolean comparison, they definitely won’t be writing code like this to prevent the error in the first place

cameronball
Автор

Imagine you're speeding in a car. You've been pulled by a police officer and he's saying: "100mph is less than the speed of your car". Are you comfortable with that?

gencurrent
Автор

I'm pretty sure any decent IDE will yell at you if you do that. Or most languages don't even allow it.

matrix
Автор

I've been programming for 20 years, I can count the number of times I've mistakenly put a single equals sign instead of two on one hand. This is a solution for a problem that really doesn't exist.

Especially nowadays where IDE's will give you warnings about this exact kind of thing if you let it.

hiTocopter
Автор

Yoda Programming is popular in java. Since you often have to call functions in these if statements and calling functions on null causes a runtime exception, by putting your constant on the left side, you eliminate the chance of null-pointer exceptions.

cid-chan-
Автор

It's a cool idea, but I think it allows for someone to become more reliant on the compiler to spot the error and increases the chance that you won't notice "if (varA = varB)" because you come to expect an error for accidental assignment.

NovaHorizon
Автор

In Java, “str”.equals(str) is also better than str.equals(“str”) because it avoids NullPointerException

CKth
Автор

I do have one instance where this can be helpful: checking if a value falls within a range. Using Yoda notation for the first comparison makes the read order match the mathematical notation:

if (2 < x && x < 8) {…}

nununoisy
Автор

Strict syntax rules in languages such as Java, Scala, and Rust can prevent certain errors, while Yoda code may appear unnatural. Also using linters, static analyzers, and tests can help to identify and prevent such issues in code. It's worth noting that in the past, in languages such as Fortran, it was possible to assign values to constants, which would lead to unpredictable behavior with Yoda code.

andrei_bo
Автор

How i code:
- Attempts to code
- nothing's working
- go to internet
- realize I'm an idiot
- fix code with the answer to a question someone asked on Reddit / quora 10 years ago
- code works until problem happens again.


[I live in endless pain :)]

alexmuse
Автор

In all my years in software I've never ever encountered this error. Just add good test cases and you're good

Nominal_GDP
Автор

Fun fact. In safety critical applications, like automotive and control systems, "yoda-notation" is mandatory, and in fact it is part of certain safety-focused coding standards like MISRA-C

M.A.Kaminski
Автор

In most modern languages the compiler usually doesn't allow assignment operations in a conditional statement, i.e. inside round brackets of an if..then statement

EdwardChan.
Автор

The last I heard, you can tell gcc that you intended to use an assignment by wrapping it in extra parentheses: if ((i = 42)). It also visually signals to others that it was your intention to write that mess - it was no accident.

uplink-on-yt
Автор

I've only found Yoda notation good for one thing: comparing a constant with a nullable value. If you do `myvar.equals("string")` and `myvar` is null, you get a NullPointerException, but you don't if you do it the other way around.

abadhaiku