DEV Community


Discussion on: Why using Yoda conditions you should probably not be

maxart2501 profile image
Massimo Artizzu


That's all we need. Linters can easily catch problems like this one. And many others, of course.

So let's forget about Yoda conditions, like we forgot about the Hungarian notation and other hard-to-read conventions.

palle profile image

Or languages designed with code safety in mind, which prohibit these kinds of expressions.

For example in Swift (and other type safe programming languages), the following expression results in a compile time error:

if name = "Luke Skywalker" { // error: use of '=' in a boolean context, did you mean '=='?
    // ...
greg0ire profile image
Grégoire Paris Author

Woah! Neat!

greg0ire profile image
Grégoire Paris Author • Edited

Yoda conditions are a lot like the Hungarian notation: it's a "clever" trick that help you catch mistakes for every gazillion time you use it. Just not worth it.