Best practices for setting up the IF condition

Asked

Viewed 99 times

3

It has been a while since I have the following doubt: would it have any difference of performance between declaring a IF in these two ways?

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

Form 1

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

Form 2

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

if($d == 3)
  • 2

    In relation to performance there is a difference, the second form has one more variable, that is, one more space in the memory being allocated. Now which form is more beautiful, it varies from person to person. I prefer the first

  • Well placed by @Robertodecampos. I particularly always value the performance.

  • 3

    If you need the value in $d later, the second form is better; if not, the first would be better.

  • @Andreicoelho then I recommend reviewing some concepts. PHP was not meant to perform, so these differences, as asked, are irrelevant to the application, making readability and semantics always a priority. If such a performance difference is relevant to the application, PHP is not the most suitable language.

  • In addition, the resource used in both codes are the same, since PHP needs to store the return of the function in memory to use in the expression validation. The difference is that with the variable it value is accessible by the developer.

3 answers

3

As already stated in the comments the second form is less performatic (the difference, in this case, is irrelevant) by having a variable being created before, exemplifying...

In the first the function is executed, X is spent on resources (memory, processing...), then a check is made, plus a Y expense.

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

But if many checks will be done with the same value that comes from a function it is better to save the result in a variable than to perform the same function several times, unless of course in each check change the value when doing the execution

3


As discussed in the question Concatenation or data sequencing: which performs best?, This kind of performance concern about language doesn’t make much sense. It is known that PHP was not created to be a performative language, so micro-optimizations in the code will not alter the result and should be avoided when making them legibility and semantics are impaired. Always prefer the code that is easier to read than the one that is supposedly faster.

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

Even if there is no difference in performance, you ask which is best. In my opinion, neither of the two are difficult to read and are unclear as to the objective. Reading the documentation, it is possible to notice that the parameter N of date will return a number referring to the day of the week of a given date: 1 for Monday, 7 for Sunday. If you are checking if it is equal to 3, you need to know if a certain date is a Wednesday. None of the ways makes this clear.

The way I would do it is:

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

if ($isWednesday) {
    ...
}

This way, I don’t need to resort to the documentation of the function date to know what the code is doing, because the variable name tells me I’m checking if it’s Wednesday.

  • I understood what you meant in the comment. Actually, it doesn’t make much difference in php, because usually it is read only once, it doesn’t make sense for you to cherish the performance and leave aside the semantics.

1

I’d do it the first way.

If I used a variable name that serves as documentation of what is there I could do the second way, even if I was a little slower (maybe I wouldn’t stay). But I would only do it in something that wasn’t really obvious. It would be something like our friend Anderson did, but there I find unnecessary, but it’s just my taste, it might be good for beginner programmers to read the code and understand what is doing there.

Or I would make a function with the condition that it returns true or false and call the function in if.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.