Calling the method n times: should I use a for-each loop or traditional for a loop?

Given the need for a loop to an arbitrary int value, is it better to use a program to convert the value to an array and for each array, or just use a traditional loop?

FYI, I calculate the number of 5 and 6 results (β€œhits”) in a few rolls of 6-sided dice. My arbitrary int value is dicePool, which represents the number of multiple throws.

As I understand it, there are two options:

  • Convert dicePool to an array and for each array:

    public int calcHits(int dicePool) { int[] dp = new int[dicePool]; for (Integer a : dp) { // call throwDice method } } 
  • Use traditional for loop:

     public int calcHits(int dicePool) { for (int i = 0; i < dicePool; i++) { // call throwDice method } } 

My opinion is that parameter 1 is an awkward code and involves unnecessarily creating an array, even if the for-each loop is more efficient than the traditional one for the loop in Option 2.

+4
source share
5 answers

At this point, speed is not important (add a comment on premature optimization;). The important thing is how quickly you can understand what the code is doing , which should call the dicePool method once.

The first method allocates an array of size dicePool and dicePool over its values, which causes the dicePool loop body to run once (I will pretend that you had int in mind instead of Integer to avoid unrelated autoboxing problem). This is potentially inefficient for the computer on which the code is running, but more importantly, it is inefficient for the person reading the code because it is conceptually remote from what you wanted to execute. In particular, you make the reader think of the new array that you just created, and the value of the variable a , which will be 0 for each iteration of the loop, although none of them are related to your final goal.

Any Java programmer looking at the second method will understand that you execute the body of the dicePool cycle once from i "counting" to dicePool . Although the last part is not particularly important, the beginning is exactly what you wanted to do. Using this common Java idiom minimizes the unrelated things the reader should think about, so this is the best choice.

If in doubt, go with simplicity.: D

+12
source

Why do you need to allocate an array to iterate over a variable that can be safely increased and used without the need for allocation?

It sounds uselessly inefficient. You may need to allocate an array if you need to change the order of ints , but it is not. I would choose option 2 .

foreach is useful when you want to iterate over a collection, but create a collection to iterate over it when you don't need it, just without meaning.

+4
source

(2) is an obvious choice because it makes no sense to create an array based on your description. If there is, of course, things are changing.

+2
source

What makes you think the for-each loop is more efficient?

Iterating over a set is very inefficient than a simple loop and counter.

This can help if you give more context about the problem, in particular, is there still this question than choosing one syntax over another. I am having trouble thinking about a problem for which # 1 would be the best solution.

+2
source

I would not write the first one. There is no need to use the latest syntax in each setting.

Your instinct is good: if it feels and looks awkward, it probably is.

Go with No. 2 and sleep at night.

+1
source

Source: https://habr.com/ru/post/1307941/


All Articles