Mike gave an excellent answer, which, unfortunately, is rarely defended in the Java literature. I'm sorry that I do not have enough reputation to support this. Instead, I will give my own answer, which I hope will add additional support to Mike's views.
The vast majority of the Java literature spreads the dogma that you should not use assert to check the public arguments of a method. In other words, they say that assert should not be used to validate prerequisites for public methods and that instead you should use explicit if (!precond) throw SomeException(); . The Java standard library is full of examples of this policy.
Arguments confirming this are as follows:
- Responsibility for compliance with the prerequisites is a function call (customer code), and not your code (vendor code).
- Validation of claims is optional, so it is best to validate validation.
Well, that seems like a very protective attitude towards me. The clients of your code are programmers, just like you. Satisfying preconditions is the responsibility of the customers, of course, and if the customers do not, this is a mistake; customer error, not yours. Of course, you expect your customers to test their programs with claims included, right? But once they are convinced of the correctness of their program, why should you still impose your useless exceptions?
Now let's look at it from the point of view of the client. You are using String.charAt . The documentation for this method reports that
public char charAt (int index)
Returns the char value at the specified index. The index ranges from 0 to length () - 1. [...]
The precondition is clearly indicated. But later he adds
Throws: IndexOutOfBoundsException - if the index index is negative or not less than the length of this row.
So, if you know for sure that your index is within the bounds, you still put your call inside try ... catch ? Note that the documentation does not really tell you that an exception will not be thrown if you respect the precondition, but of course, what do you expect, right?
So, you are sure that the index is within the bounds, you might even have argued that you didn’t waste time and space on a useless try , which will never catch anything, but String will still spend time checking that you behave well.
As i see him
- Claims should be used to verify conditions that are entirely under the control of the programmer, whether it be a client or a provider.
- Explicit conditional instructions with exceptions (or another error signaling device, such as an error return code) should be used to verify conditions that depend on uncontrolled external factors, such as user input, unpredictable operating system restrictions, etc.
This is what I teach my students. I do not tell them. This is not a dogma. I let them know that this is contrary to what they will read in most books, but I urge them to decide for themselves.
This question does not apply to Java; it is a question of programming methodology. The approach I follow is akin to Design by Contract . Some languages have special syntax to support this style, for example, the ability to explicitly declare preconditions, postconditions, and object invariants, and they are explicitly included as part of the public specification (and documentation) of the code.
Joao Rodriguez