Best Practices for Assembling IF Condition

3

I've been wondering for some time: would there be any difference in performance between declaring an IF in these two ways?

Or even if there is no difference in the performance question, is one of these ways more correct than the other?

Form 1

if(date('N', strtotime($data)) == 3)

Form 2

$d = date('N', strtotime($data));

if($d == 3)
    
asked by anonymous 20.03.2018 / 01:22

3 answers

3

As discussed in the question Data concatenation or sequencing: which one performs best? , this type of concern with language performance does not make much sense. It is known that PHP was not created to be a performatic language, so micro-optimizations in the code will not change the result and should be avoided when doing so the readability and semantics are impaired. Always prefer code that is easier to read than the one that is supposed to be faster.

You ask if there is a performance difference between the two codes, there are none unless you run it a thousand times and then start to see some difference. The resource consumption of the two solutions is the same. Even though in the second there is a variable and in the first one no PHP will need to store the function return in memory anyway, the difference is that with the variable the value will be accessible to the developer (directly).

Even though there is no performance difference, you ask which one is the best. In my opinion, neither is both, because both are difficult to read and are unclear as to the purpose. By reading the documentation, you can see that the parameter N of date will return a number for the day of the week of a certain date: 1 for Monday, 7 for Sunday. If you are checking for 3, you need to know if a date is a Wednesday. Neither way makes that clear.

The way I would do it:

$isWednesday = (date('N', strtotime($data)) === "3");

if ($isWednesday) {
    ...
}

In this way, I do not need to use the documentation of the date function to know what the code is doing, because the variable name tells me that I am checking for Wednesday.

    
20.03.2018 / 02:41
3

As already mentioned in the comments the second form is less performative (the difference, in this case, is irrelevant) by having a variable being created before,

In the first one the function is executed, an expense X is made of resources (memory, processing ...), then a verification is done, plus one expense Y.

In the second is created a variable, spent Z, and assigned to it the execution of the function, spent X, and then is made the comparison, expense Y.

But if you are going to make many checks with the same value that comes from a function, it is better to save the result to a variable than to perform the same function several times, unless it is clear that at each check, change the value when doing the execution

    
20.03.2018 / 02:17
1

I would do it the first way.

If I used a variable name that I service as documentation of what it is there I could do the second form, even if it was a bit slower (maybe it will not stay). But I would only do this in something that was not really obvious. It would be something like our friend Anderson did, but I find it unnecessary there, but it's just my taste, it might be good for beginner programmers to read the code and understand what they are doing there.

Or I would do a function with the condition that would return true or false and would call the function in the if.

All this is just a way to better demonstrate what you are doing. A comment can be more effective in such cases.

    
20.03.2018 / 12:39