File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assertion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assertion" Watch "Assertion" New topic
Author

Assertion

Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
Do not use assertions to validate arguments to a public method


Do use assertions to validate arguments to a private method


Please explain the difference between them


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
RameshKumar MuthuKumar
Greenhorn

Joined: May 25, 2005
Posts: 8
Dear Vishnu,
It is not appropriate to use assertions to check the parameters passed to public methods. Because your public methods may be used in programs written by other people you cannot be certain that they will have assertions enabled and thus the normal running of the program may be faulty.
However it is appropriate to use assertions for checking the parameters to private methods as these will generally only be called by code written by people who have access to the source of those methods.

i got this from http://www.jchq.net/certkey/0205certkey.htm

regards
Ramesh
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
Thanks for the link. I wrote a sample code to understand the concept

Create two different classes namely General and CheckAssert.

and compile
javac -source 1.4 CheckAssert.java
javac General.java

and execute
java -ea General
o/p
Exception in thread "main" java.lang.AssertionError
at CheckAssert.call(CheckAssert.java:4)
at General.main(General.java:5)


java General
o/p
0


Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
Sun suggestions that you NOT use assertions to validate public method arguments. Memorize the answer but ignore the suggestion.

The argument, although unstated, is that you have no control over who will call your public methods. Generally speaking that is not the case. In fact, most of the time "public" means accessible across packages; it does not mean you are publicly releasing your API to the world at large.

Asserts are a powerful facility when combined with unit testing. They make for extremely robust code and, like exceptions, the isolate housekeeping code from business logic. So, if that's good for protected or package or private methods why not public? Answer: it is equally valid for all three; ignore Sun's asinine suggestion and keep in mind they have been wrong about some glaring things in the past; this is another.

Having said that, if you are publishing an API, you will have to forego the benefits of assertions and throw runtime exceptions (e.g., IllegalArgumentException) since callers might want the option of catching an exception versus shutting down the JVM when they use your API incorrectly,.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608

Asserts are a powerful facility when combined with unit testing. They make for extremely robust code and, like exceptions, the isolate housekeeping code from business logic. So, if that's good for protected or package or private methods why not public? Answer: it is equally valid for all three; ignore Sun's asinine suggestion and keep in mind they have been wrong about some glaring things in the past; this is another.

This depends on your perspective. The way the assert keyword has been implemented is harmful to unit testing and its use considerably degrades the robustness of software, therefore, the assert keyword should be avoided at all costs. Objective reasoning intentionally omitted just like the marketers do (only they are so good at disguising it), which causes the reiteration of such guff in places like public forums.

Having said that, if you are publishing an API, you will have to forego the benefits of assertions and throw runtime exceptions (e.g., IllegalArgumentException) since callers might want the option of catching an exception versus shutting down the JVM when they use your API incorrectly,.

Shutting down the VM? Since when did an AssertionError cause the VM to abort? Better not put any asserts in your J2EE applications if it will bring down the whole container.


Tony Morris
Java Q&A (FAQ, Trivia)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assertion
 
Similar Threads
Assertions to validate arguments; Inappropriate to validate PUBLIC arguments
Assertion issue
Assertions
Assertions
I dont understand why 'assert' is allowed on private methods but not on public methods