to use an "if empty" statement, or not
Posted: Fri Mar 07, 2008 9:34 am
A friend and I have been debating on an issue and it's really hard to come up with a valid argument for or against it.
The situation:
===================================================================================================================
(Without any checks)
===================================================================================================================
(With Checks)
===================================================================================================================
The debate:
We have 2 checks now. One just let's the for loop determine if it needs to execute. The second determines if there is truly a reason for the for loop to execute, it will execute only then.
The argument is, only when the value is truely empty, will the if statement make sense. When it's not empty, the empty function will add more time to the overall performance of the application due to having to check if is empty.
Here is also another bit of information: if you run the empty function on a blank array, it evaulates a LOT quicker than if you evaluate on an array with 1200+ records.
So, now the question is this: which way would you use, and why?
I personally prefer checking to see if the array has value, and only then executing a for loop. This is because there is a count, and then a for loop function call, and checking if it has value before executing it prevents the uncessary run.
The counter argument is if the array does have value, that it will run even slower due to the initial if statement.
The counter to the counter is that, in my opinion, you should be checking to see if that block of code is valid to be run at all, meaning if you do take the .0002 seconds to evaluate it's in all reality perfectly valid due to not knowing one way or another if the operations are absolutely needed or not.
What say you?
The situation:
===================================================================================================================
(Without any checks)
Code: Select all
<?php
$array = array();
$array = dynamic_function(); // This could return 1200 array values, no values, 1 value, etc.
$x = count($array);
for($i = 0; $i < $x; $i++) {
}
?>
(With Checks)
Code: Select all
<?php
$array = array();
$array = dynamic_function(); // This could return 1200 array values, no values, 1 value, etc.
if(!empty($array)) {
$x = count($array);
for($i = 0; $i < $x; $i++) {
}
}
?>
The debate:
We have 2 checks now. One just let's the for loop determine if it needs to execute. The second determines if there is truly a reason for the for loop to execute, it will execute only then.
The argument is, only when the value is truely empty, will the if statement make sense. When it's not empty, the empty function will add more time to the overall performance of the application due to having to check if is empty.
Here is also another bit of information: if you run the empty function on a blank array, it evaulates a LOT quicker than if you evaluate on an array with 1200+ records.
So, now the question is this: which way would you use, and why?
I personally prefer checking to see if the array has value, and only then executing a for loop. This is because there is a count, and then a for loop function call, and checking if it has value before executing it prevents the uncessary run.
The counter argument is if the array does have value, that it will run even slower due to the initial if statement.
The counter to the counter is that, in my opinion, you should be checking to see if that block of code is valid to be run at all, meaning if you do take the .0002 seconds to evaluate it's in all reality perfectly valid due to not knowing one way or another if the operations are absolutely needed or not.
What say you?