For vs. For-Each Construct

Posted on Tuesday, September 30, 2008
1 commentAdd comments

In his blog Automated QA vs. Manual QA: CodeRuler Revisted, Arthur Shum states that I 'identified a false error' and that use of the for construct was necessary to provide access to the indices for the implementation to function as desired. I stand by my observation that use of the for statements I cited in Peer Review: Adhering to Java Programming Standards can be replaced with for-each constructs and still maintain the desired results:

Case 1 – Iterating over myKnights (Line 113):

for construct:
   1:  for (int i = 0; i < numOfKnights; i++) {
2: if (i < numOfKnights * .2) {
3: team1.add(myKnights[i]);
4: }
5: else {
6: team2.add(myKnights[i]);
7: }
8: }

for-each construct:
   1:  for (IKnight k : myKnights) {
2: // Assign one-fifth of knights to team 1
3: if (team1.size() < numOfKnights * .2) {
4: team1.add(k);
5: }
6: // Assign remaining knights to team 2
7: else {
8: team2.add(k);
9: }
10: }

Case 2 – Iterating over myPeasants (Line 189):
This example still requires knowledge of the peasantIndex variable, but it has been implemented as a counter-index variable for use in only 2 lines of code outside of the declaration/incrementation statements. I have commented out the code I replace in order to use the for-each construct:

   1:  int peasantIndex = 0;
2: // for (int peasantIndex = 0; peasantIndex < myPeasants.length; peasantIndex++) {
3: for (IPeasant p : myPeasants) {
4: directions.clear();
5: int curX = p.getX(); // myPeasants[peasantIndex].getX();
6: int curY = p.getY(); // myPeasants[peasantIndex].getY();
7:  
8: for (int i = 1; i <= 8; i++) {
9: possibleNextPosition = World.getPositionAfterMove(curX, curY, i);
10: if ((possibleNextPosition != null)
11: && (World.getLandOwner(possibleNextPosition.x, possibleNextPosition.y) != this)
12: && (World.getObjectAt(possibleNextPosition.x, possibleNextPosition.y) == null)) {
13: directions.add(i);
14: }
15: }
16: if (directions.size() > 0) {
17: move(p, directions.get(rand.nextInt(directions.size())));
18: // move(myPeasants[peasantIndex], directions.get(rand.nextInt(directions.size())));
19: }
20: else if (enemyPeasants.length > 0) {
21: // Splits up the ruler's peasants to move toward different enemy peasants in search of land.
22: // Uses modulo so that it does not matter if there are less ore more enemy peasants.
23: int x = enemyPeasants[peasantIndex % enemyPeasants.length].getX();
24: int y = enemyPeasants[peasantIndex % enemyPeasants.length].getY();
25: move(p, p.getDirectionTo(x, y));
26: // move(myPeasants[peasantIndex], myPeasants[peasantIndex].getDirectionTo(x, y));
27: }
28: else if (enemyCastles.length > 0) {
29: int r = rand.nextInt(enemyCastles.length);
30: int x = enemyCastles[r].getX();
31: int y = enemyCastles[r].getY();
32: move(p, p.getDirectionTo(x, y));
33: // move(myPeasants[peasantIndex], myPeasants[peasantIndex].getDirectionTo(x, y));
34: }
35: else {
36: move(p, rand.nextInt(8) + 1);
37: // move(myPeasants[peasantIndex], rand.nextInt(8) + 1);
38: }
39: peasantIndex++;
40: }

Comments (1)

atshum  

I stand corrected. Thanks for taking the time to show me.

Subscribe to: Post Comments (Atom)