File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Cattle Drive and the fly likes when to make a method-and-loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "when to make a method-and-loop" Watch "when to make a method-and-loop" New topic

when to make a method-and-loop

fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

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?

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Katrina Owen

Joined: Nov 03, 2006
Posts: 1367
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

Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
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:


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.

"I'm not back." - Bill Harding, Twister
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9059
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!

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
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

Joined: Jan 19, 2001
Posts: 4012
I don't know if that applies for what the nitpicker's talking about...

I agree. Here's the link:
subject: when to make a method-and-loop
It's not a secret anymore!