I'm cutting my Java teeth on an application that performs a medical calculation to estimate a person's kidney function. I can get the relevant values and calculate the result but I'd like to add range-checking (so for instance, if the object is passed a patient's age of 150 my object's calulating method will not return an erroneous result). I'm a purist at heart - I'd like to know the *best* way to do the range checking - should I learn about exceptions at this point or is another approach preferred?
I'm learning from a combination of HeadFirst Java and AgileJava at this point; I think they are both excellent.
Unfortunately, I don't think there is a cut-and-dry answer to your question. For one thing, it depends on who answers it. I'm certain that you will get a variety of opinions in this thread.
Personally, I would like to start by seeing your code so far. There may be an elegant way to add the range-checking without introducing exception handling.
On the other hand, this might be a good opportunity to get your feet wet. It sounds like simple enough program that you should be able to use it to learn about how exceptions work. In the end, I think experience is the best guide to indicate when exceptions are a good idea and when they are not. So I think it will help if you use exceptions. If they end up not working very well, then at least you may learn how to NOT use exceptions.
I agree with Layne. For simple, direct solutions, your choices are:
a) throw an exception b) return an invaid number (-1) c) return a null object
Any way you slice it, either the calling method or the called method has to do extra work for out-of-range input, so it's best to start with there you think the responsibility is best implemented.
For user input, I favor handling range-checking in the calling method. If the called method is already designed to handle it, then go with that.
Thanks for the input. I'll post the code (already 92 lines without the tests) ASAP; I look forward to seeing how you folks would approach it.
I can already see clearly how to handle the problem via returning null objects or (-1) results so I'm leaning towards exceptions as I have the most to learn from this approach
Fail early. If you have a finite set of values, declare them as an enumeration, such that you have type-safety (fail before it even turns into executable bytecode). If you don't, then I don't know what you're talking about; I am guessing.
I would absolutely use Exceptions all over the place. At its simplest level, just use the java.lang.Exception class. Or, to get complex, develop your own exception class which subclasses exceptions. In any cae, declare your methods to throw exceptions that need some sort of validation on them, especially your getters and setters.
i.e.: public void setAge(int age) throws Exception { if (age < 0) { throw new Exception("Age cannot be less than 0"); }
this.age = age; }
Now, onto gender... I would use static variables in your class: public static final int MALE = 1; public static final int FEMALE = 2;
public void setGender(int gender) throws Exception { if (gender != thi.MALE && gender != this.FEMALE) { throw new Exception("Gender is invalid."); }
this.gender = gender; }
And then when using this class, one could just do: patient.setGender(Patient.MALE);
I would absolutely use Exceptions all over the place. At its simplest level, just use the java.lang.Exception class. Or, to get complex, develop your own exception class which subclasses exceptions. In any cae, declare your methods to throw exceptions that need some sort of validation on them, especially your getters and setters.
i.e.: public void setAge(int age) throws Exception { if (age < 0) { throw new Exception("Age cannot be less than 0"); }
this.age = age; }
Now, onto gender... I would use static variables in your class: public static final int MALE = 1; public static final int FEMALE = 2;
public void setGender(int gender) throws Exception { if (gender != thi.MALE && gender != this.FEMALE) { throw new Exception("Gender is invalid."); }
this.gender = gender; }
And then when using this class, one could just do: patient.setGender(Patient.MALE);
Originally posted by James Hejmanowski: Sorry. I haven't figured out how to paste text into this forum and preserve the whitespace/formatting
There are buttons below the message text area that help you do this. Click on the one labeled CODE and past your code in between the tags that are inserted into your message. You should also check out the help link for UBB Code tags. The link is to the left of the message text area.
Layne
Post by:autobot
Roses are red, violets are blue. Some poems rhyme and some don't. And some poems are a tiny ad.
a bit of art, as a gift, that will fit in a stocking