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)?