在循环数组的编程实践中,将值声明为变量还是直接访问值?

huangapple go评论56阅读模式
英文:

Programming practice in a loop of array, declaring the value as variable or accessing the value directly?

问题

在处理一个复杂的for循环(非常长,有很多事情要做)时,只是想知道循环的最佳实践是什么?

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

  或者

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

以上只是一个示例,实际循环的代码相当长(有很多事情要做,访问了很多不同的键)。

只是想知道哪种是更好的实践,还是完全是个人偏好的问题?数组的值不会被更新,只会被引用和取消引用。

我知道使用foreach循环可以解决这个问题,因为它处理键值对,但我想了解for循环的最佳实践。我主要在JavaScript和PHP中编程,如果有关系的话。

英文:

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.

答案1

得分: 2

以下是已经翻译好的部分:

  1. 缓存数组的长度 在您的 for 循环中,count($array) 在每次迭代中都被调用,您可以将该值存储在变量中,如下所示:
$len = count($array);
for ($i = 0; $i < $len; $i++) {
    //
}
  1. 使用描述性的变量名称,例如,不要使用 $variable 这样的名称,而是使用像 $product 这样的名称,因为这更有意义,有助于其他人阅读您的代码时更容易理解。

至于访问变量值,例如 $variable = $array[$i];$array[$i]->price; 之间在性能方面不应该有明显的差异,因为像PHP和JavaScript这样的高级语言具有自动内存管理。

英文:

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 &lt; $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]-&gt;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.

答案2

得分: 1

这真的是个人偏好,因为两种方法没有太大的区别。如果你只是执行一两个语句,任何一种方法都可以。如果你要访问多个属性,最好将其存储为变量,然后使用该变量。这有助于你作为作者,也有助于其他人阅读或维护代码。我个人偏好将其存储为变量或使用不同的循环表达式。

另一个要避免的做法是在for循环的条件中使用count()。如果编译器没有捕捉到它并将其移动到只评估一次,那么这可能会很昂贵,这意味着它每次迭代都必须重新计算列表。我也总是让$i的表达式小于,因为循环是递增而不是递减。

$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;
}
英文:

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 &lt; $length; $i++) {
    $item = $array[$i];
    $price = $item-&gt;price + $item-&gt;tax;
    $tax = $item-&gt;tax;
    $shipping_cost = $item-&gt;shipping_cost;
}

答案3

得分: 0

声明适当命名的变量可以使代码更易读。但如果考虑到循环复杂且非常长,我们想要尽量节省内存。那么我们可以跳过声明变量以提高美观度(以获得微不足道的改进),并通过详细的注释来弥补可读性不足。

英文:

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.

huangapple
  • 本文由 发表于 2023年6月13日 10:08:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76461306.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定