Re: use of assert in Java [vs. exceptions]
Giovanni Azua wrote:
I don't agree that the type of distribution source, jar, war or ear is what
drives the decision to label public methods as "exposed". I explain at the
end.
....
What do all non "exposed" public methods have in common? that any Java
application or component built on top of them can proof fulfillment of pre
condition and correctness via unit testing while assertions are enabled i.e.
any pre condition violation is a programming error whereas the case of
"exposed" public methods the possible pre conditions violations can not be
unit tested/prevented and require input validation.
Once you release a class with public or protected methods, those methods are
exposed. That's a fact. It is true as you say that the type of distribution
is not relevant; the mere fact that a method is public or protected is what
exposes it.
Again to conclude, my only point here is that the statement "Do not use
assertions to check the parameters of a public method" [2] is too
categorical and not the absolute truth.
Actually, you have said nothing to contradict its truth, other than "it's not
the absolute truth". Your suggestion to use assertions to aid in testing then
to disable them supports the dictum not to use assertions to check the
parameters of a public method, at least not in production contexts. You also
recommend in such cases not to use exceptions or other so-called "defensive"
mechanisms. If anything, you extend Sun's advice rather than contradict it;
you go beyond "don't use assertions to check the parameters of a public
method" to "don't use anything (in production) to check the parameters of a
public method".
Sun's point is true on the face of it - if you disable assertions, then they
cannot check anything. so anyone relying on them for so-called "defensive"
checks will be disappointed. That's all they're saying.
--
Lew