• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

when to make a method-and-loop

 
fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on java-4b - not that that matters. on my last revision, i had a hunk of code that repeated 4 times. the only difference was a parameter in a call to a method.

so, rather than repeat the same block of code 4 times, i wrapped it in a loop. Granted it was a non-traditional loop, counting down and decrementing by something other than 1.

the nit-pick said i should break it out into the 4 hunks.

I guess my question is how do you decide when to do it which way? Theoretically, there could be a java-4c that would ask you to parse a 60 digit number. My for-loop approach would have been (relatively) simple to scale up (other parts - not so much).

I always thought that any time you use the same hunk of code more than once, it should be a method.

am i wrong?
 
Katrina Owen
Sheriff
Posts: 1367
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are right on the spot: If there is a hunk, it should probably go in a method.

My guess is that for Java 4c, having a method deal with the hunk, and then using a loop to call the method would be a pretty clean way to go. With just four calls, the loop would probably just be in the way.

Please note the "guess" part above

Katrina
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that there may be two different things to consider: (a) should you factor the repeated hunk of code out into a method, and (b) should you call the method with a loop, or just repeat the call? I tend to think that anytime you repeat any remotely complex code even once, you should probably make it a method. But assuming you do that, the choice then is, should you do this?

Or this?

Now personally I'd do the former. But the repetition in the second version really isn't very bad. And it may be a little clearer for novice programmers who don't notice the fact that the loop is actually counting backwards. A Cattle Drive nitpicker may well favor the latter approach for this reason. Alternately you could draw attention to the backwards loop with a comment:

or

Personally I like the last best - it's just enough of a comment to draw attention to the unusual part, and it's still easy to modify later if you want to change it to loop ten times or a thousand. The nitpicker may well have other ideas though; I don't speak for any Cattle Drive policies.
 
Marilyn de Queiroz
Sheriff
Posts: 9067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that if you can start a for loop at zero and increment by one up to a known end point, a for loop is ideal. If you don't start at zero, don't increment by one, or don't have a known end point, You have to stop and ask yourself whether a for loop is the right approach. If you have a loop that is very short, I often wonder if the program would be simpler without a loop at all ....

If you're already calling a method, maybe some of the stuff in the for loop could be handled inside the method, or maybe another method would be useful. If you put as much into a method as you can and then just call that method a few times, it often is cleaner.

As far as Java 4C is concerned, I think I would cross that bridge when I came to it. If you did everything while thinking of the future, your program could easily grow ten fold. Focus on the here and now!
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There may also be other refactorings that become possible with one of the forms above; these subsequent refactorings may give you a better reason to prefer one version to another. For example, consider this:

and this:

Which of these is simpler? I like the second, assuming that the array was not needed for anything else, and provided we're reasonably confident that the number of names will never grow to hundred. The array of names was just extra infrastructure that wasn't really necessary in this case. It's not much infrastructure I grant you, but it's something. Sometimes it's cleaner looking to just see the values in-lined, with a minimum of repetition. I don't know if that applies for what the nitpicker's talking about, but it's something to consider.
[ October 30, 2007: Message edited by: Jim Yingst ]
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know if that applies for what the nitpicker's talking about...



Bingo!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic