ASCII silly question, Get a silly ANSI.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by A Alqtn:
Here is an example:
public class A {
private int bonus;
public boolean setBonus(int bonus) {
if( bonus > 0 ) {
this.bonus = bonus;
return true;
}
return false;
}
}
public class B {
public static void main(String [ ] args) {
A a = new A();
int bonus = read from the user the bonus value;
if( a.setBonus( bonus ) == false )
System.out.println("invalid bonus value);
}
}
%
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
"I'm not back." - Bill Harding, Twister
Originally posted by Michael Duffy:
Now clients don't have to check a value to see if the set succeeds, and they'll be told if the operation fails.
Exceptions can be expensive, and there's an argument that says they shouldn't be used for normal program flow. But you've documented the proper use of your class in javadocs, so any user who wants to use your class will be fully-informed as to its proper use. I think it's a better alternative.
Originally posted by Layne Lund:
IllegalArgumentException is a runtime exception. Generally, runtime exceptions indicate a programmer error and not a system error. This means that such an exception should NOT occur in a production environment. Persumably, the exception is thrown to facilitate testing so that such programming errors can be easily found and fixed.
I think in most cases this would be an acceptable solution as long as the whole system is thoroughly tested to ensure that such exceptions are not thrown after the program has been distributed for production. I also agree with Ilja that the way to handle this type of situation might not be entirely universal and depends on the exact situation. Don't get stuck into one solution so much that you are not flexible enough to modify it when there are reasons to do so.
Layne
Originally posted by A Alqtn:
Layne,
When reading a value from a text field, a runtime error might happen and it is not a programmer error. Such a exception can happen a production environment. What I mean is that not all run time errors represent progrmmaer error.
Alqtn
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Originally posted by Mike Noel:
A slightly more complex example would be where the bonus has to be >= 0 and <= 10% of another field, (maybe "gross"). Now the client has to know a bit of the business logic that the class is implementing. You can imagine even more complicated cases. Following the "check for validity first" path can mean that the client ends up replicating a lot of the class's own logic. IMHO this defeats the purpose of setters.
I suggest that the class provide a method that can test for valid values or throw an exception. Making a checked exception seems like a reasonable idea for a class that's a bit complex. The client can wrap all the setter calls in a single try block and create appropriate catch code to deal with invalid input.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Originally posted by Ilja Preuss:
I think there are better solutions to this, such as the Builder pattern, or a Collecting Parameter. Code doesn't *have* to be complex, it's always a choice we make.
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus