argh Java

Jul. 19th, 2007 04:10 pm
gusl: (Default)
[personal profile] gusl
The following piece of code is redundant, in a bad way. The first half prints adjacency-evaluation data. The second half, which is almost identical, prints coefficient-evaluation data. The only difference between them is the set of "System.out" statements inside the inner loop.

The right thing to do should be to refactor it, by pushing the outer loop into a new method, and passing to this method the set of properties to be recorded.

But how can I pass properties in Java? Do I need to turn them into Strings and use reflection?

Here's the code:

		System.out.println("************************************************************************");
		System.out.println("*** comparing runs for this setting: adjacency (errOmission,errCommission,loss) ***");
		System.out.println("************************************************************************");
		//for each run
		for (List run : evalTable){
			String s = "#" + i + ":" + __;

			//for each method, show omission, commission and loss
			for(EvaluationResult eval : run){
				s += eval.adj.errorsOfOmission + _;
				s += eval.adj.errorsOfCommission + _;
				s += eval.adj.loss() + __;
			}			
			//s+=winner;
			
			System.out.println(s);			
			i++;
		}

		
		System.out.println("************************************************************************");
		System.out.println("*** comparing runs for this setting: all coefficients (total_loss) ***");
		System.out.println("************************************************************************");
		
		i=0; //run-number

		//for each run
		for (List run : evalTable){
			System.out.print("#" + i + ":" + __);

			//for each method, show omission, commission and loss
			for(EvaluationResult eval : run){
				System.out.print(eval.coeffAll.loss() + _);
			}
			//s+=winner;
			
			System.out.println("");			
			i++;
		}
		

(no subject)

Date: 2007-07-19 11:25 pm (UTC)
From: [identity profile] dankamongmen.livejournal.com
i'm no java idiom guru by any means, but wouldn't the classic solution be to write a base class which performs the core behavior, and then subclass it, implementing a variety of inherited abstract interfaces (which can, of course, be consts if you like to do it that way, or aggregative functions otherwise)....

(no subject)

Date: 2007-07-19 11:35 pm (UTC)
From: [identity profile] gustavolacerda.livejournal.com
My goal is to make this code more modular.

I could push the code into an abstract class, and use a different set of properties in each subclass. But that would be just as redundant as what I have now.

The outer and inner loops should appear exactly once in my code.

(no subject)

Date: 2007-07-20 12:44 am (UTC)
From: [identity profile] bhudson.livejournal.com
If the code is really that short, I doubt you're going to save anything. Otherwise, sounds to me like you want to pass a lambda that takes an EvaluationResult, and call it in the inner loop.

(no subject)

Date: 2007-07-20 02:20 am (UTC)
From: [identity profile] simrob.livejournal.com
Yes, I thought this too. But that doesn't mean that there's not a similarly-simple way to do it in Java, just that we're used to using functional programming...

(no subject)

Date: 2007-07-20 02:29 am (UTC)
From: [identity profile] bhudson.livejournal.com
Java doesn't actually have closures, sadly, but you can get close by passing small objects around and using dynamic dispatch.

(no subject)

Date: 2007-07-20 06:59 am (UTC)
From: [identity profile] gustavolacerda.livejournal.com
Actually, there is a third instance of the same nested loop, that I didn't show here.

(no subject)

Date: 2007-07-20 01:48 pm (UTC)
From: [identity profile] bhudson.livejournal.com
Right, but each one is only 5 lines long, so you can't possibly save a lot.

(no subject)

Date: 2007-07-20 07:19 am (UTC)
From: [identity profile] pbrane.livejournal.com
If your EvaluationResult class implemented Map, then you could do it nicely, esp. if you gave it a new fancy toString(List propsToPrint) method.

February 2020

S M T W T F S
      1
2345678
9101112131415
16171819202122
23242526272829

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags