• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Assertions.. Please Help

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Programming with Assertions
 
author
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?!
 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
I'm doing laundry! Look how clean this tiny ad is:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic