【发布时间】:2016-04-01 14:26:47
【问题描述】:
我想知道在计算机科学中是否有一个最佳实践,即当两者拉向不同方向时,是否支持代码行数或指令数?
示例 1:下面的代码行数较少,但 if 是针对每个 g 评估的,而它们不依赖于 g。
For g = 0 To UBound(myRegions)
If Cells(2, j) = "" And Cells(myRowToBeAllocated, 4 + j) <> 0 Then
Cells(2, g) = Worksheet("Test").Cells(k, q)
ElseIf Cells(myRowToBeAllocated, 4 + j) <> 0 Then
Cells(2, g) = Worksheet("Test").Cells(2 * k, q / 2)
End If
Next g
示例 2:下面的代码有更多的行,但不是为每个 g 评估 ifs,因此下面导致计算执行的指令更少。
If Cells(2, j) = "" And Cells(myRowToBeAllocated, 4 + j) <> 0 Then
For g = 0 To UBound(myRegions)
Cells(2, g) = Worksheet("Test").Cells(k, q)
Next g
ElseIf Cells(myRowToBeAllocated, 4 + j) <> 0 Then
For g = 0 To UBound(myRegions)
Cells(2, g) = Worksheet("Test").Cells(2 * k, q / 2)
Next g
End If
问题是:对于哪一个最好,是否有最佳实践?
【问题讨论】: