how can i get more than one value from a method

D

David

i want to invoke a method to analyze a byte array, and return several values
in different type, including int, String, and boolean, how can i achieve
this, at first, i plan to use a Vector, but the element is only Object, can
not be int or boolean, if i have to change these primary type into Object,
it's so boring. is there any better solutions??
 
E

Erwin Moller

David said:
i want to invoke a method to analyze a byte array, and return several
values in different type, including int, String, and boolean, how can i
achieve
this, at first, i plan to use a Vector, but the element is only Object,
can not be int or boolean, if i have to change these primary type into
Object, it's so boring. is there any better solutions??

Yes, encapsulate all the data in a (simple) Object.
Make getter and setter methods for all relevant data (properties, members,
pick your word)

Return the Object instead.

It is easy and it is clean.

Regards,
Erwin
 
C

Chris Uppal

David said:
i want to invoke a method to analyze a byte array, and return several
values in different type, including int, String, and boolean, how can
i achieve this, at first, i plan to use a Vector, but the element is
only Object, can not be int or boolean, if i have to change these
primary type into Object, it's so boring. is there any better
solutions??

If you *really* have to derive several values from one pass of analysis (which
is plausible, though it doesn't happen too often), then you might consider
making an AnalysisResults class, and returning that.

Indeed, its quite probable that a better solution would be to put the
responsibility for doing the analysis onto the new class, so you would end up
writing code like:

// do the analysis
Analyzer analyzer = new Analyzer().
byte[] bytes = ...;
analyser.analyze(bytes).

// retrieve the various results
int size = analyzer.getSize();
double stddev = analyser.getStandardDeviation();

-- chris
 
J

John C. Bollinger

David said:
i want to invoke a method to analyze a byte array, and return several values
in different type, including int, String, and boolean, how can i achieve
this, at first, i plan to use a Vector, but the element is only Object, can
not be int or boolean, if i have to change these primary type into Object,
it's so boring. is there any better solutions??

Define a class suitable for containing the return values you want to
provide, and return instances of this class.


John Bollinger
(e-mail address removed)
 
J

Jonathan Mcdougall

i want to invoke a method to analyze a byte array, and return several values
in different type, including int, String, and boolean,

this usually means a design flaw. If your function returns three
things, it is maybe because it _does_ three things. Try to break down
your function into three smaller ones.
how can i achieve
this, at first, i plan to use a Vector, but the element is only Object, can
not be int or boolean, if i have to change these primary type into Object,
it's so boring. is there any better solutions??

A simple class

class Results
{
public int my_int;
public String my_string;
public boolean my_bool;
}
 
D

Dale King

Chris Uppal said:
David said:
i want to invoke a method to analyze a byte array, and return several
values in different type, including int, String, and boolean, how can
i achieve this, at first, i plan to use a Vector, but the element is
only Object, can not be int or boolean, if i have to change these
primary type into Object, it's so boring. is there any better
solutions??

If you *really* have to derive several values from one pass of analysis (which
is plausible, though it doesn't happen too often), then you might consider
making an AnalysisResults class, and returning that.

Indeed, its quite probable that a better solution would be to put the
responsibility for doing the analysis onto the new class, so you would end up
writing code like:

// do the analysis
Analyzer analyzer = new Analyzer().
byte[] bytes = ...;
analyser.analyze(bytes).

// retrieve the various results
int size = analyzer.getSize();
double stddev = analyser.getStandardDeviation();


This is definitely the approach I would suggest but it might even make more
sense to use the constructor instead of an analyze method:

// do the analysis
byte[] bytes = ...;
Analyzer analyzer = new Analyzer(bytes).

// retrieve the various results
int size = analyzer.getSize();
double stddev = analyser.getStandardDeviation();

It might be advisable to use lazy evaluation so that you don't do the
analysis in the constructor and do the analysis as needed by the get methods
(caching it for the next call).
 
R

Roedy Green

this usually means a design flaw. If your function returns three
things, it is maybe because it _does_ three things. Try to break down
your function into three smaller ones.

How about this:

I want to write a general Arraylist splitter that splits the objects
in into two categories e.g. sheep and goats, "process now" and
"process later"

You want to pass this thing a Criterion delegate with a method named
boolean take ( Object ). [Is there a traditional name for this
class/method?]


It invokes it in each object and puts the passes in one result and the
fails in another.

It would be wasteful to scan the list twice once to get the sheep and
then to get the goats.

What the method could do is return Arraylist[2] with the sheep in one
slot and the goats in the other.

If you generalise this to N categories, then it clearer you are not
really returning two results, but rather an array of results.
 
T

Tr0mBoNe-

This issue is one faugt by all programmers, wether or not to jam
everything into as little of place as you can, or spread everything
out and make it easier to work with.

now, you can create an object in a sub-class that has those as
parameters, and you can then generate accessor methods for that
object, or seperate that into 3 methods that each return the value
they are supposed to. Both of these are good, because they are both
scalable solutions (easy to expand) and easier to read.

remember, you are not the only one who has to read your code, so
please:
"Be kind, Please Unwind your methods".
 
J

Jonathan Mcdougall

Do not top-post please. Rearranged.
This issue is one faugt by all programmers, wether or not to jam
everything into as little of place as you can, or spread everything
out and make it easier to work with.

It is not an issue, it has been a long time since we noticed smaller
functions were easier to maintain and understand.
now, you can create an object in a sub-class that has those as
parameters, and you can then generate accessor methods for that

I do not see the need for accessors where the only goal is to make a
structure holding three values. This would be polymorphic extremism.
object, or seperate that into 3 methods that each return the value
they are supposed to. Both of these are good, because they are both
scalable solutions (easy to expand)

the first solution is not quite scalable. Adding another feature to
the function would mean adding the field to the class and setting that
field (by a ctor or whatever). It could break earlier code.
and easier to read.

I do not find that easy to read since a programmer new to that code
would need to have a look to the class to understand what he is doing.

IMHO,

int f()
{
return 1;
}

String g()
{
return "";
}

boolean h()
{
return true;
}


void bar()
{
int i = f();
String s = g();
boolean b = h();
}


is much easier to read than


Helper fgh()
{
Helper h = new Helper();
h.i = 1;
h.s = "";
h.b = true;

return h;
}

void bar()
{
Helper h = fgh();

int i = h.i;
String s = h.s;
boolean b = h.s;
}

class Helper
{
public int i;
public String s;
public boolean b;
}


Adding a ctor could help, but it could break other code using that
class. Using a helper class confuses the use of the function fgh()
since we don't really know what it does anymore. We only know that it
returns a Helper.


Jonathan
 
C

Chris Uppal

Dale said:
This is definitely the approach I would suggest but it might even
make more sense to use the constructor instead of an analyze method:

// do the analysis
byte[] bytes = ...;
Analyzer analyzer = new Analyzer(bytes).

// retrieve the various results
int size = analyzer.getSize();
double stddev = analyser.getStandardDeviation();

I agree entirely.

It makes it much harder to misread the intention of the class, and also neatly
answers the question of whether you can/should use the same Analyzer to perform
more than one analysis (the answer being "no", of course ;-)

-- chris
 
D

Dale King

Jonathan Mcdougall said:
Do not top-post please. Rearranged.



It is not an issue, it has been a long time since we noticed smaller
functions were easier to maintain and understand.


I do not see the need for accessors where the only goal is to make a
structure holding three values. This would be polymorphic extremism.

Not really. The idea of the methods is to isolate clients from the
implementation. Whether you believe it or not using the raw field does tie
you to the implementation.

An example would be that one of these fields we might want to switch to lazy
evaluation and only really eveluate the value if and when the client asks
for it. If you use a field you cannot switch to lazy evaluation without
changing every client of the object to use an accessor. If we start with an
accessor, we can make the change transparently.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top