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
- Introducing the Yoda Conditional
- The Power of the Yoda Conditional as an Obfuscation Technique
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:
- Abusing Python’s Operator Overloading Feature
- 5 Absurd Ways to Add Two Numbers in Python
- How to Obfuscate Code in Python: A Thought Experiment
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!
Recent Posts
While creating some of the other early articles in this series, I had a realization: something even more fundamental than loops and if statements is the condition. As a result, I figured we could...
Today, we're expanding our concept map with the concept of loops in Python! Unless you're a complete beginner, you probably know a thing or two about loops, but maybe I can teach you something new.