Condition Yoda, good reason exists to use?

13

Question mainly in PHP, in the weirdPress documentation there is a which explicitly says that it is interesting to use the famous Yoda conditions, although it does not give a good reason.

  

When making logical comparisons involving variables, always place the   variable on the right-hand side and put constants, literals, or   function on the left side. (Free Translation)

Specifically I've always found it ugly and not easy reading this type of coding: if(10 == $teste){ // do anything } or if("string" == $teste){ // do anything }

This question came to me after a colleague of mine recommended to install a plugin in brackets called brackets php code quality tools which theoretically points out errors in coding ... and in lines of type if($teste == 10){ // do anything } it generates warnings, it recommends using the blessed Yoda conditions.

The first time I saw this type of coding came to mind is to facilitate processing, first type the processor only takes the constant and then accesses the location of the variable in memory, but stopping to think I do not see any difference!

I tested PHP and JavaScript and saw no difference in performance with the naked eye ...

Follow tests:

PHP

echo "Normal<br/>";
$a = "a";
for($x = 0; $x <= 5; $x++){
    $time_start = microtime(true);
    if($a == "a"){
        //
    }
    $execution_time = microtime(true) - $time_start;
    echo $execution_time.'s<br/>';
}
echo "-------------<br/>";
echo "Yoda Condition<br/>";
$a = "a";
for($x = 0; $x <= 5; $x++){
    $time_start = microtime(true); 
    if("a" == $a){
        //
    }
    $execution_time = microtime(true) - $time_start;
    echo $execution_time.'s<br/>';
}

JavaScript

IsthereanygoodreasontouseYodaConditionsinanyprogramminglanguage?Orisitjusttothetasteofthecustomer?Isthereanyperformancedifference(insomelanguage)?

    
asked by anonymous 03.03.2017 / 06:10

3 answers

12

The only advantage of yoda conditions is change error type , logic (assignment instead of comparison) to one of syntax. You can not assign something directly to a number or any other type.

Basically yoda conditions make the code fail faster in case of errors while the performace is still the same comparison; soon does not affect anything.

Is it worth sacrificing readability to detect this kind of error sooner? It is a simple mistake that sometimes can cause a little headache, in these situations it is best to pause and cool the head.

<?php
$usuario = 'teste';
if('admin' = $nome){
    echo 'administrador do sistema';
}
  

PHP Parse error: syntax error, unexpected '='

Example - ideone

    
03.03.2017 / 11:08
6

In Java something similar is used for comparisons involving a literal string (between quotation marks):

if ("Mario".equals(nome)) {

The utility is to save a zero reference test. The programmer may even forget to do this test, which can cause NullPointerException if the variable nome is null and try to call equals() , but if he is accustomed to reverse the condition this exception will be avoided. >     

04.03.2017 / 17:21
4

The advantage of Yoda conditions is to expose possible bugs for typos by changing == by = .

In the code below, the condition will always return true , causing unexpected behavior in the application - which could take some time to discover.

<?php
$usuario = 'teste';
if($usuario = 'admin'){
    echo 'administrador do sistema';
}
?>

Using the yoda condition, you would receive a syntax error while executing this code, noticing and correcting the problem immediately.

<?php
$usuario = 'teste';
if('admin' = $usuario){
    echo 'administrador do sistema';
}
?>

So you have a performance gain not in code execution, but in preventing bugs.

    
03.03.2017 / 13:34