Yoda Conditions (From the office)
Tags: php, ifstatement, yodacondition, cleancode
Published: 2022-07-28 10:02:51

---
title: Yoda Conditions (From the office)
published: true
description: are Yoda conditions worth it?
tags: php,ifstatement,yodacondition,cleancode
cover_image: https://static.wikia.nocookie.net/starwars/images/e/eb/PurpleGlow_HRA2.png

---

I am Nico, 

I am a senior engineer for a ticket broker based in London.

From the office! is a daily (almost) posting routine that I keep to journal about the tech things I have been doing in my day-to-day office life.

Condition statements
--------------------

Most often than not, while writing code, you need to perform different actions for different conditions. 

The way programming languages do it is by providing conditional statements.

In PHP, we have the following conditional statements:

The **if** statement executes some code if one condition is true.

```php
if (condition) {
     code to be executed if the condition is true;
}
```

The **if...else** statement has two blocks of code and executes the first if the condition is true, and the other code if that condition is false.

The **if...else if...else** statement executes different blocks of code for more than two conditions.

```php
if (condition) {
  code to be executed if this condition is true;
} else if (condition) {
  code to be executed if the first condition is false and this condition is true;
} else {
  code to be executed if all conditions are false;
}
```

The **switch** statement selects one of many blocks of code to be executed and provides a default option too.

Nothing too fancy here, if you have been through the basics of web development you know this stuff already.

The problem
-----------

You, as a developer, want your code to be reliable and working 100% of the time.

THAT IS QUITE A GOAL!

One of the most frequent ways to make the previous sentence incorrect is by introducing bugs into your code.

Well, stop it and just do not write buggy code, right?

The problem is that we often do not know when a bug in inside our code.

![bug](http://www.quickmeme.com/img/44/4496d0d71b9ef73aa5a8bcbe1e4df8a1148f6c5a4b1f782f5f02f4f58a68a9af.jpg
)

I am sure you have seen this meme already




Let's test you now

```php
if ($wheelsNumber == 2) {
    echo 'it is a motorbike';
}
if ($wheelsNumber = 4) {
    echo 'it is a car';
}
```

Can you tell me what the bug is in this code?

Introducing the Yoda condition...

The Yoda condition explained
----------------------------

The reason Yoda condition is a thing, is that the code above ALWAYS returns '*it is a car'.*

Even when we are dealing with a motorbike.

Why?

The way PHP works is by working out what is inside the condition's brackets and then evaluating the *if* keyword.

In the second condition, we are not actually evaluating a condition but we assigned the value of 4 to the variable named *$wheelsNumber.*

After the assignment is done the condition always returns true.

Solution
--------

```php
if (2 == $wheelsNumber) {
    echo 'it is a motorbike';
}
if (4 = $wheelsNumber) {
    echo 'it is a car';
}
```


The outcome of this code will be a syntax error.

Now 4 is considered by PHP as a constant, and, you know, constants do not change.

The good
--------

**Catching errors**

As I wrote above, I believe that your job is to make your code reliable all the time, this means preventing bugs at any cost.

Using Yoda condition is a simple way to avoid a bug, it takes 1 second to be written and it might save you an entire night in front of your screen to understand where the issue is coming from.



The bad
-------

**Bad Readability**

"I understand the old C programmer's trick of inverting equality statements.  

It prevents the inadvertent omission of a = from being silent.  

But I don't like it. It doesn't read right. I prefer:

`if (commentData == null)`

CommentData is the subject of that sentence, and null is the direct object. Inverting them doesn't jive well with the way we think. I dislike any statement in code that causes the reader to do a double-take. Code that protects the author at the expense of the reader is flawed code."

The above paragraph comes from Uncle Bob, famous programmer and author of "The Clean Code series".

He has a point, writing a condition differently might take a couple of seconds for the reader to understand what were you doing.

Are those couple of seconds worth the danger of having a bug inside the code?

Don't even try to tell me that.

**Not a universal standard**

That might be a bigger issue to me:

Here are the platforms that actively encourage the use of Yoda:

-   WordPress ([PHP](https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/#yoda-conditions) and [JavaScript](https://make.wordpress.org/core/handbook/best-practices/coding-standards/javascript/#yoda-conditions))
-   [Symfony](http://symfony.com/doc/current/contributing/code/standards.html#structure)

Here who don't:

-   [jQuery](https://jquery.com/)
-   [Zend](http://framework.zend.com/)
-   [Laravel](https://laravel.com/)
-   [React](http://facebook.github.io/react/)
-   [Underscore](http://underscorejs.org/)
-   [AngularJS](https://angularjs.org/)
-   [Backbone.js](http://backbonejs.org/)
-   [React](http://facebook.github.io/react/)
-   [Closure](https://developers.google.com/closure/?hl=en)
-   [Ruby on Rails (RoR)](http://rubyonrails.org/)
-   [CakePHP](http://cakephp.org/)
-   and many more including C#, VB, C++, Python, and more.

Don't get fooled by the two parts length, though.

Estimates say that in 2022 WordPress is still used by 43.2% of all

websites on the internet.

**Do Unit tests instead**

A better way to prevent bugs from getting and staying into your code would be by writing automated tests.

Let me rephrase it for you:

YOU MUST WRITE AUTOMATED TESTS!

There are amazing components nowadays that allow you to test in complete confidence.

See PHPUnit, Mockery, CodeCeption, Behat etc.

There is no excuse of not doing it, it is just too important

What is your take?
------------------

What do you think about the Yoda condition?

Is it worth adding it into your daily practice or it is just a waste of time for the readers of your code?

Write your thought in the comment below.

Conclusion
----------

I have been a web developer for almost a decade now.

Working with world-class businesses and award-winning marketing agencies situated in the heart of London.

Also, I write articles and tutorials on my blog and online communities and help businesses build their presence online.

[Click here to read more than 100+ of my blog post](https://anastasionico.uk/blog)