aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assertions.. Please Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assertions.. Please Help" Watch "Assertions.. Please Help" New topic
Author

Assertions.. Please Help

Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
Hi guys..

Can any kind hearted person give me a simple example of using "assertions"?
I have not been able to find a example for it and could not understand the concept.

Thank you very much guys



(Topic quietened)
[ June 07, 2005: Message edited by: Barry Gaunt ]
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673
Programming with Assertions
Philip Heller
author
Ranch Hand

Joined: Oct 24, 2000
Posts: 119
Assertions are commonly used to check preconditions in non-public methods, and to check postconditions in all kinds of methods.

Suppose you were writing a medical application, with a private method that computes the appropriate dosage of a medicine, given a patient's age and weight. If those args are <= zero, it's meaningless to proceed. So you have precoditions that age and weight must be positive. If they aren't, it's time to quit execution immediately and devote your attention to finding the bug that caused the weird call. (Since the method is private, you know the problem lies somewhere in your class.) So your method would start like this:


Presumably any precondition-violation bugs are eliminated before shipping the program to your users. Your users run the code with assertions disabled, so (almost) no processor time is wasted in precodition checks that by now always pass.

Eventually your program is used in hospitals throughout the world and you become immensely rich. Aren't exceptions great?!


Consultant to SCJP team.<br />Co-designer of SCJD exam.<br />Co-author of "Complete Java 2 Certification Study Guide".<br />Author of "Ground-Up Java".
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
Originally posted by Philip Heller:

Presumably any precondition-violation bugs are eliminated before shipping the program to your users. Your users run the code with assertions disabled, so (almost) no processor time is wasted in precodition checks that by now always pass.



but we can always use exception handling as a suplement for assertion...
but it will check for condtion breaking i.e processor time is wasted in precondition checks if using exception..

is this is the only reason for using asserstion... as we can always use exception handling.. ?

do comments..


Thanks and Regards, Amit Taneja
Philip Heller
author
Ranch Hand

Joined: Oct 24, 2000
Posts: 119
Amit asks: "is this is the only reason for using asserstion... as we can always use exception handling.. ?"

Pretty much, yes. But the benefit is huge. Think of assertions an an exception mechanism that can be disabled in the field. You can always do this in every method:




That will work, but look at the overhead. The only way to disable the mechanism in the field (where your customers figure they paid for the software, so there better not be any pre- or postcondition violations) is to edit out all the checks and throws. Then you have to redo your method declarations, because they no longer throw AssertionError. Then you have to recompile your code, and run your regression tests again.

It's really about economics. One economy is the processor-cycle economy. That's the one we pregrammers tend to focus on, especially when preparing for the SCJP exam. In my own code, the pre- and postcondition checks usually don't take long to run, but now and then they do. With the assertion mechanism, those checks (corresponding to preconditionViolation() and postconditionViolation() above) never happen in the field.

The other economy is the development cost economy. In a big company, the big boss would prefer not to pay someone to delete all those condition checks, and someone else to run the tests. In a small company like my own (it couldn't possibly be smaller, if you get my drift), I would rather go out dancing or work on my next novel than stay up late doing all that engineering.
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
Thanks a lot Philip.. Assertions was one of the topics confused me.. Thanks for clearing me out..

Thanks you all guys..
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
but incase we take user input in the s/w which are to be checked by pvt method and -then we must use exception handling or we must use check when user is taking input.
using event handling or some javascript ?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assertions.. Please Help