walk with params... the whole way?

hello.

consider the following:

abstractpublicclass Test{

void doSomething()

void doSomething(double x, Object o)

}

with doSomething is "equal" except it for SOME Tests/derivates need args, for others not...

there is also some intercompatibilty eg, its okay to call doSemoething(DEFAULT_X, null) if needed...

now a smart design is maybe:

interface SomethingWithParamDoer{

void doSomething(double x, Object o);

};

abstractpublicclass Test{

void doSomething(){

doSomething(DEFAULT_X,null);

}

void doSomething(double x, Object o){

doSomething();

}

publicstaticvoid main(String[] args){

doWithManyManyTests

if (testinstanceof SomethingWithParamDoer)

test.doSomething(x, o);

else

test.doSomething();

}

}

although its horrible because no one guarantees for overloading of one of the doSomething so happy deadlocking...

on the other hand, its much cleaner to only use the parameterized version of

doSomething and fill with defaults if not needed. but maybe that hides which one needs params, and which not so the caller is uncertain about the situations params are useful or not.

also the empty params would spoil cpu cycles?

-perf. is an issue here.

the above example would get far more complicated at my app as i maybe use two or three params, that are abitrary useful, independent of each other.

its that... most calls are obviuos , eg. use much real non-default params but some special cases need default fallback, and severall objects would not have any use to some of the params, but "to the most"...

so what would you do?

thanks & greets

Paul

[2827 byte] By [paulgeisa] at [2007-10-2 3:52:48]
# 1

Not really sure I understand the question.

But a simple solution for paramter passing is the following.

class TestParameters

{

public double x = 0.1; // 0.1 is the default value.

Object o = null; // Null is default value.

}

void doSomething(TestParameters parms)

There is only one doSomething() method. And if you want you can derive other classes from the Parameters class as well.

jschella at 2007-7-15 23:09:12 > top of Java-index,Other Topics,Patterns & OO Design...
# 2

i'm even not sure i understand my problem exactly :-)

it's the olde performance vs. convinience tradeoff i think...

your suggestion seem the most convenient solution to me...

on the other hand it maybe lacks performance due to massive instantiation TestParameters just for parameter passing. The structure in question is my app's main workhorse.

i am quite uncertain what to do as i hate substancial refactoring due to bad chosen architecture in the beginning ..

thank you much anyway

Paul

paulgeisa at 2007-7-15 23:09:12 > top of Java-index,Other Topics,Patterns & OO Design...
# 3

As to performance short time temporary object creation in current generations of the Sun VM is very fast.

I even believe I saw a link somewhere where it demonstrated that it was faster than object creation in C++.

Also keep in mind that calling a method in the Sun VM creates a stack frame which I believe involves the creation of real java objects as well.

As always you should actually profile the application to find real problem areas.

jschella at 2007-7-15 23:09:12 > top of Java-index,Other Topics,Patterns & OO Design...