Following is an example code , checkUserGuess
method that belongs to a Board
class.
public String checkUserGuess(String aGuess)
{
// Some processing
return result_of_a_guess;
}
I have a SimpleGuessingGame
class consumes this method and is satisfied with the processing that the method does. It does not use the returned value.
Another class ComplexGuessingGame
consumes this method and also uses the value returned by the method for further processing.
So we have two cases , one where the return value is used and other where its ignored. Is this a common occurrence or does this point to bad design ?
When you need to do something like this, chances are that the method does two things that are of value to a caller:
Since some users need only #1, while other users need both #1 and #2, it may be a good idea to split the method in two parts, like this:
public void validatekUserGuess(String aGuess) {
// Some processing
}
public String checkUserGuess(String aGuess) {
validatekUserGuess(aGuess);
// Some additional processing
return result_of_a_guess;
}
Now the users that wish to ignore the return value would not be required to "pay" with CPU and memory for computing a value that they are going to discard anyway.
There is nothing inherently wrong with using a return value in one call, and not using it in another.
Imagine in one case you want to attempt to turn a light on, and in another you want to make sure it was actually turned on.
public boolean turnOn(Light l);
case 1:
turnOn(new Light());
log.debug("Attempted to turn on light");
case 2:
boolean turnedOn = turnOn(new Light());
if (turnedOn) {
log.debug("Light is turned on");
} else {
log.debug("Not able to turn light on");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With