[newbie] Incapsulating a function

  • Thread starter Thread starter Giulio Petrucci
  • Start date Start date
G

Giulio Petrucci

Hi everybody,

I have some different functions taking a double[] as their input data and
returning a double value as their output data. Which implementation do you
think is better?

--first:

class Op1 {
private double[]d;

public Op1(double[] data) {
d=data;
}

public double[] GetResutl() {
double[] r;
//COMPUTING...
return r;
}

}

--second:

class Op1 {

public Op1() {
}


public double[] GetResutl(double[] data) {
double[] r;
//COMPUTING...
return r;
}

}


Thanks in advance,
Kind regards,
Giulio
 
Hi,

the GetResult() method in both cases return an array of double , not a
unique double value as you state in your text.

Unless you user your data in more than one operation I would prefer to do
not keep a reference to it inside the class. In so the method can be
declared as static
 
The question you need to ask in order to determine which construction
to use is whether the array of doubles forms part of "what an Op1 is".
So, what is the definition of "an Op1"?

If the private double[] is the only member of Op1, then you're
effectively saying that "Op1 is a wrapper around an array of double,"
in which case one assumes that Op1 provides some functionality in
addition to what you get with an array of double, or is used instead of
an array of double in case someday you decide to change it to an
List<double> or something like that. Anyway, one possibility is that
Op1 is something that you use all over your program to represent a
particular collection of doubles that has some particular meaning.

However, if Op1 exists only to allow you to do the mathematical
operation, and has no particular meaning beyond that, then the second
case is the better construction, and you would probably want to make
GetResult "static" if it doesn't use anything from the state of Op1 (or
if Op1 has no fields and so has no state) in its calculations.
 
Back
Top