I have 2 alternatives to implement a calculation method, and I am wondering what would be the better approach.
The method needs some int and double parameters and (in some cases) a special flag to do some different calculation.
In the first example, I could call the method with 'calculateFoo(1, 2.0d)' to have the boolean flag == FALSE.
In the second example, I always have to set the boolean flag (even if I do not need it)
Method 1: (here I'm using the '...' as an 'method overloading' parameter)
public SomeObject calculateFoo(int pIntValue, double pDoubleValue, boolean... pDoSpecialStuff) {
if (pDoSpecialStuff.length == 0 || pDoSpecialStuff[0] == false) {
// method was called without boolean flag or boolean flag was set to FALSE
// do some calculation stuff
// ...
} else {
// method was called with boolean flag == TRUE
// do some other calculation stuff
// ...
}
return SomeObject;
}
Method 2: (this is the 'common' approach)
public SomeObject calculateFoo(int pIntValue, double pDoubleValue, boolean pDoSpecialStuff) {
if (pDoSpecialStuff == false) {
// method was called with boolean flag == FALSE
// do some calculation stuff
// ...
} else {
// method was called with boolean flag == TRUE
// do some other calculation stuff
// ...
}
return SomeObject;
}