Obfuscation Techniques: The Yoda Conditional

Obfuscation Techniques: The Yoda Conditional Featured Image

As a community, we spend a lot of time debating best practices. Perhaps it’s time we establish actual bad practices. Or as I like to put them, obfuscation techniques. To kick off this new series, I want to start with the Yoda conditional!

Table of Contents

The Standard Conditional

Typically, in imperative programming, we write conditionals in the following form:

if variable operator constant: ...

For example, if you’re a student, you might want to know if you passed a class, so you would setup your conditional as follows:

if my_grade >= PASSING_SCORE:
  print("Yay, I passed!")

Part of the reason I think we write conditionals like this is because they flow in the same way that we speak (e.g., if my grade is as good or better than the passing grade, I’ll pass). Therefore, the logical flow of the conditional makes the most sense to us. However, there is another way to write this condition.

Introducing the Yoda Conditional

Rather than starting our conditional with the variable, we can actually reorder the conditional to begin with the constant as follows:

if constant operator variable: ...

Using our same example of a passing grade, it would be possible to rephrase our conditional as follows:

if PASSING_SCORE <= my_grade:
  print("Yay, I passed!")

The way this is written, it feels like the student’s grade is the constant. In other words, the conditional itself is reversed, hence the name “Yoda” conditional as Yoda speaks in reversed English phrases such as “patience you must have.”

The Power of the Yoda Conditional as an Obfuscation Technique

It may seem like a subtle change, but reversing conditionals is an easy way to make your code less readable, thus achieving obfuscation. By reversing the convention of placing the variable first in the conditional, the reader will have additional cognitive overhead. After all, we all get used to certain conventions and norms, so breaking them will almost certainly make for more challenging code to read.

Ironically, there are also actually software development benefits to adopting Yoda conditionals. For example, by placing the constant (or literal) first, there is no risk of accidentally assigning a variable. Python doesn’t have this issue, but other languages do:

// Variable assignment
if (myLetterGrade = "A")

// Equality conditional
if (myLetterGrade == "A")

// Yoda variable assignment raises error
if ("A" = myLetterGrade)

On top of that, we can avoid null pointer exceptions with method calls if we put our constants first—which can happen in Python:

BOB = Student("Bob")

// Typical conditional--error prone
if student.has_teammate(BOB)

// Yoda conditional--no longer error prone
if BOB.has_teammate(student)

In this case, I think the Yoda conditional isn’t exactly less readable, so the payoff might be worth it. That said, the goal of today is to make code less readable, so I say go for it! Any little bit helps.

At any rate, I know it was a short one today, but I’m on break. Don’t worry! I’ll be expanding this series shortly. At some point, I plan to make another massive obfuscation techniques article like this one, with all of the techniques from this series sprinkled throughout. Should be fun!

Otherwise, here are some related articles you can check out in the meantime:

As usual, you can also take your support a step further by checking out my list of ways to grow the site. Or if you’re good for today, then I’ll see you next time!

Obfuscation Techniques (6 Articles)—Series Navigation

In our field, everyone likes to talk about best practices, but it’s sometimes difficult to make the case for what is and isn’t a best practice. On the other hand, I think it’s very easy to come up with ways to make code worse, but who would want to read about ways to make their code bad? I’ll tell you who! People who want to obfuscate their code, or at least that’s what I tell myself. Regardless, that’s my cover story for putting together this new series, and I figure it’ll be a lot of fun.

Jeremy Grifski

Jeremy grew up in a small town where he enjoyed playing soccer and video games, practicing taekwondo, and trading Pokémon cards. Once out of the nest, he pursued a Bachelors in Computer Engineering with a minor in Game Design. After college, he spent about two years writing software for a major engineering company. Then, he earned a master's in Computer Science and Engineering. Today, he pursues a PhD in Engineering Education in order to ultimately land a teaching gig. In his spare time, Jeremy enjoys spending time with his wife, playing Overwatch and Phantasy Star Online 2, practicing trombone, watching Penguins hockey, and traveling the world.

Recent Posts