For a long time I have wondered what is more optimal in a function php
to make return
a value.
I have the function that receives a parameter string
and that compares it within if
, my doubt arises in whether it is more optimal that within each it if
has a return
so that when it enters the if
immediately it returns the value and no longer verifies the others or it is better that it has the return
after of all conditions.
Just as it could also make use of elseif
so that when it enters one it condición
no longer enters the others, but here it would also apply to put the return
inside the condition or put it at the end of all the conditions.
The example functions are small (2 conditions) but the ones I am going to use in a project have many conditions and hence the need to find the optimal method.
Feature #1:
private function obtener_assets($parametro)
{
if ($parametro == 'compras'){
$data = array(
'assets/js/scripts/pages/compras.js',
);
}
if ($parametro == 'pagos'){
$data = array(
'assets/js/scripts/pages/pagos.js',
);
}
return $data;
}
Feature #2:
private function obtener_assets($parametro)
{
if ($parametro == 'compras'){
$data = array(
'assets/js/scripts/pages/compras.js',
);
return $data;
}
if ($parametro == 'pagos'){
$data = array(
'assets/js/scripts/pages/pagos.js',
);
return $data;
}
}
Feature #3:
private function obtener_assets($parametro)
{
if ($parametro == 'compras'){
$data = array(
'assets/js/scripts/pages/compras.js',
);
}
elseif ($parametro == 'pagos'){
$data = array(
'assets/js/scripts/pages/pagos.js',
);
}
return $data;
}
Feature #4:
private function obtener_assets($parametro)
{
if ($parametro == 'compras'){
$data = array(
'assets/js/scripts/pages/compras.js',
);
return $data;
}
elseif ($parametro == 'pagos'){
$data = array(
'assets/js/scripts/pages/pagos.js',
);
return $data;
}
}
I would opt for a much simpler and more portable solution which consists of the following:
By doing this, when there are new assets, you just add them to the array and you don't have to modify
if
or blocksswitch ... case
that could be endless.This would be the code:
We do some tests:
Departure:
fiddler
Here you can see one
DEMOSTRACIÓN
of the code and do tests with it.I would venture to give you another additional option to those you expose. It is similar (practically) to a nesting of
if
(like the one you expose in Function:#4 ) as far as optimization is concerned, but it has the added attraction of being much more efficient at a visual level:Feature #5:
They also
switch
have the property that they are very easy to add new conditions, so they are very easy to adapt when the number of these grows.#1 and #3 have an error, in case it
$parametro
does not have any of the values, the function will try to return a variable that never existed (because the declaration was inside the conditionals), remember that you must shield your code no matter how absurd that is, you do not know the limits of the end user.The other proposals are valid, this is the taste of each developer and also essential cases, if the function will not do anything beyond returning an armed variable, it is excellent, but, if after the conditionals there are more lines to execute, these will never they will be reflected because you are "cutting the code"
Now if the conditionals are many, then use a
switch
as recommended by @PHPMyguel since not all the conditions are evaluated if it is not a direct deal (It is strange because python does not use switch)If it is a condition between 2 options then a ternary conditional, obviously at the processing level it is the fastest
All options are valid as long as you know how and when to use them and also a bit of "Good taste".