1
In certain parts of my system, some selection filters occur, an example, is when selecting the area of general sector collaborators, I can select:
Employees (all)
Active employees (status = true)
Inactive contributors (status = false)
I can do it two ways.
Via parameter in the method:
public static function get_collaborators(string $filter = 'all')
{
if($filter == 'all'){
$sql = 'SELECT * FROM data_collaborators';
} elseif($filter == 'active'){
$sql = 'SELECT * FROM data_collaborators WHERE status = 1';
} elseif($filter == 'inactive'){
$sql = 'SELECT * FROM data_collaborators WHERE status = 0';
}
$rs = \MySQL\MySQLSelector::select_by_query_string($sql);
return $rs['data'];
}
Or, Via separate methods:
public static function get_all_collaborators()
{
$sql = 'SELECT * FROM data_collaborators';
$rs = \MySQL\MySQLSelector::select_by_query_string($sql);
return $rs['data'];
}
public static function get_active_collaborators()
{
$sql = 'SELECT * FROM data_collaborators WHERE status = "1"';
$rs = \MySQL\MySQLSelector::select_by_query_string($sql);
return $rs['data'];
}
public static function get_inactive_collaborators()
{
$sql = 'SELECT * FROM data_collaborators WHERE status = "0"';
$rs = \MySQL\MySQLSelector::select_by_query_string($sql);
return $rs['data'];
}
From what I understand, the first mode (with parameters) would perform less because it has checks, so several separate methods would be better.
The question is:
Is there a difference in execution from one to the other ? (even if it is insignificant) If yes, which would be better ? If not, which follows the 'correct pattern' followed by the majority ?
If there is another way, what would it be ?
For sure the best is to use a single method that receives parameters, the difference of performance is imperceptible so what ends up weighing more and the question of the organization then use only one method with parameters.
– user83428
When using PHP, it doesn’t make much sense to worry about tiny performance issues, because PHP itself isn’t designed for performance. In this case, you should choose the form that maintains the semantics and atomicity of your code. In my opinion, being the objective of the three queries to search a list of collaborators, a method with parameters is the best to do.
– Woss
@Andersoncarloswoss said it all.
– user83428
@Andersoncarloswoss I’m trying to gain a little bit of performance in virtually all the most basic resources, to have a slightly higher margin in functions that consume the most, because I have a very big drop in the operational areas with a lot of data, but the difference is so minimal that the interval between one and the other call can be the performance of the machine, really, I think it won’t make much difference.
– AnthraxisBR