skip to Main Content

Given a complex for loop (very long, lots of thing going on), just wondering, what’s the best practises to loop through it?

for ($i = 0; count($array) > $i; $i++) {
  $variable = $array[$i];
  $price = $variable->price;

  OR

  $price = $array[$i]->price;
}

Above is just a sample, the real code of the loop is rather long (lots of things going on, and lots of different keys being accessed).

Just wondering, which is the better practise, or is it solely a preference thing? There will be no updates on the array’s values, just referencing it and unsetting it.

I know that a foreach-loop solves this "issue" with key-value pairs, but I would like to know the best practise for a for-loop. I mainly code in Javascript & PHP, if that matters.

3

Answers


  1. Given your question, it boils down to style and preferences, but there are some best practices that might help optimize performance and readability.

    1. Cache the length of the array in your for loop count($array) is called in every iteration, you might store the value in a variable like so:
    $len = count($array);
    for ($i = 0; $i < $len; $i++) {
        //
    }
    
    1. Use descriptive variable names, e.g. instead of $variable use a name like $product as it would make more sense and it helps the code to be more readable for others who might be reading your code.

    In terms of accesing the value of a variable like $variable = $array[$i]; vs $array[$i]->price; there shouldn’t be any noticeable difference in terms of performance because languages like PHP and Javascript being high level languages have automatic memory management.

    Login or Signup to reply.
  2. It really is a personal preference as there isn’t too much of a difference either way. If you are doing one or two statements either approach is fine. If you are going to do more than a few access of properties you are much better off storing it as a variable and then using the variable. This is to benefit you as the author, but then anyone else that comes along trying to read or maintain the code. My own preference is to store it as a variable or use a different looping expression.

    One other practice to avoid is using count() in the condition of the for loop. That can be expensive if the compiler doesn’t catch it and move it to evaluating only once, meaning it would have to count the list again every iteration. I also always have the $i expression be a less than since the loop is counting up and not down.

    $length = count($array);
    for ($i = 0; $i < $length; $i++) {
        $item = $array[$i];
        $price = $item->price + $item->tax;
        $tax = $item->tax;
        $shipping_cost = $item->shipping_cost;
    }
    
    Login or Signup to reply.
  3. Declaring variable with suitable name makes code more readable.

    But if given that the loop is complex and very long, and we want to save as much memory as possible.
    Then we can skip declaring variables for aesthetic(to get negligible improvement) and compensate lack of readability using detailing comments.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search