samedi 21 septembre 2019

Technical difference on if not then return or if then do

I have a question, might sound simple, but I am trying to understand what or if it makes any technical differences how we write the conditional statements in code. Here I am talking about PHP, but it may apply to other languages as well.

I want to understand if there is any difference between

Check IF NOT then Return

function my_func($var or $val)
{
    if ($var != $val) {
        return;
    }

   // do the stuff ...

}

Check IF then DO

function my_func($var or $val)
{
    if ($var == $val) {
        // do the stuff ...
    }

   return false;

}

What I can assume that retuning in IF NOT will be better in performance as if conditions don't match it will not even go through the code. However, that applies to another approach as well. So which one is better or it depends on the situation?

Aucun commentaire:

Enregistrer un commentaire