• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assertions.. Please Help

 
Maduranga Liyanage
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 703
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Programming with Assertions
 
Philip Heller
author
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?!
 
amit taneja
Ranch Hand
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
Philip Heller
author
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 813
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic