views:

187

answers:

5

Hi folks,

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

FYI, I am calculating the number of 5 and 6 results ("hits") in multiple throws of 6-sided dice. My arbitrary int value is the dicePool which represents the number of multiple throws.

As I understand it, there are two options:

  1. Convert the dicePool into an array and for-each the array:

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

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

I apologise for the poor presentation of the code above (for some reason the code button on the Ask Question page is not doing what it should).

My view is that option 1 is clumsy code and involves unnecessary creation of an array, even though the for-each loop is more efficient than the traditional for loop in Option 2.

Thanks in advance for any suggestions you might have.

+1  A: 

I wouldn't write the first one. It's not necessary to use the latest syntax in every setting.

Your instinct is a good one: if it feels and looks clumsy, it probably is.

Go with #2 and sleep at night.

duffymo
Thanks for your your answer and supporting comment about instinct.
Arvanem
+4  A: 

Why would you need to allocate an array to loop over a variable that can be safely incremented and used without any need of allocation?

It sounds unecessarily inefficient. You can need to allocate an array if you need to swap the order of ints but this is not the case. I would go for option 2 for sure.

The foreach is useful when you want to iterate on a collection but creating a collection just to iterate over it when you don't need it is just without sense..

Jack
+1 for your clear concise exposure of my lack of sense! :)
Arvanem
+2  A: 

(2) is the obvious choice because there's no point in creating the array, based on your description. If there is, of course things change.

cletus
Thank you for your answer which is quite correct.
Arvanem
+11  A: 

At this point, speed really isn't important (insert premature-optimization comment ;). What matters is how quickly you can understand what the code does.

In my opinion, you should use the second method, as it has been used for years and is clear and concise.

The first method allocations an array of size dicePool and iterates through the integer objects that have to get boxed and unboxed with their values not even being set when all you are trying to do is perform a task a certain number of times. Not only is this inefficient for the computer, its highly inefficient for the human as its implementation gets far away from what its trying to actually accomplish. When in doubt, go with simplicity.

CrazyJugglerDrummer
+1 and accepting your answer for focus on practical consequences for human and non-humans alike.
Arvanem
Good point. I would also add in a premature-optimization-comment-given-how-popular-and-inefficient-groovy-is.
Yar
+2  A: 

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

Iterating over a set is very likely less efficient than a simple loop and counter.

It might help if you gave more context about the problem, specifically whether there's more to this question than choosing one syntax over the other. I am having trouble thinking of a problem to which #1 would be a better solution.

Jacob O'Reilly
Thank you Jacob for your answer. You can rest easy, there is not more than meets the eye here. The context is nothing more than simulating dice rolls for a boardgame RPG called Shadowrun.
Arvanem