how can i get more than one value from a method

Discussion in 'Java' started by David, Sep 12, 2003.

  1. David

    David Guest

    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??
     
    David, Sep 12, 2003
    #1
    1. Advertising

  2. David

    Erwin Moller Guest

    David wrote:

    > 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
     
    Erwin Moller, Sep 12, 2003
    #2
    1. Advertising

  3. David

    Chris Uppal Guest

    David wrote:
    > 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
     
    Chris Uppal, Sep 12, 2003
    #3
  4. David wrote:

    > 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
     
    John C. Bollinger, Sep 12, 2003
    #4
  5. >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;
    }


    --
    Jonathan Mcdougall
     
    Jonathan Mcdougall, Sep 12, 2003
    #5
  6. David

    Dale King Guest

    "Chris Uppal" <-THIS.org> wrote in message
    news:3f619f18$0$33807$...
    > David wrote:
    > > 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).

    --
    Dale King
     
    Dale King, Sep 12, 2003
    #6
  7. David

    Roedy Green Guest

    On Fri, 12 Sep 2003 13:49:56 -0400, Jonathan Mcdougall
    <> wrote or quoted :

    >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.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Sep 12, 2003
    #7
  8. David

    Tr0mBoNe- Guest

    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".



    Jonathan Mcdougall <> wrote in message news:<>...
    > >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;
    > }
     
    Tr0mBoNe-, Sep 12, 2003
    #8
  9. Do not top-post please. Rearranged.

    >> >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.


    >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
     
    Jonathan Mcdougall, Sep 13, 2003
    #9
  10. David

    Chris Uppal Guest

    Dale King wrote:

    > 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
     
    Chris Uppal, Sep 13, 2003
    #10
  11. David

    Dale King Guest

    "Jonathan Mcdougall" <> wrote in message
    news:...
    > Do not top-post please. Rearranged.
    >
    > >> >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.

    >
    > >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.


    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.

    --
    Dale King
     
    Dale King, Sep 22, 2003
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shufen
    Replies:
    4
    Views:
    872
    Shufen
    Oct 3, 2004
  2. Merciadri Luca
    Replies:
    4
    Views:
    854
  3. Bob Rashkin
    Replies:
    5
    Views:
    102
    Dennis Lee Bieber
    Dec 23, 2013
  4. Steven D'Aprano
    Replies:
    0
    Views:
    146
    Steven D'Aprano
    Dec 23, 2013
  5. Replies:
    3
    Views:
    120
    Gary Herron
    Dec 23, 2013
Loading...

Share This Page