• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Assertion usage

 
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have been reading thru the Assertion documentation in Sun's site but not able to catch the following topic
http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html#usage
Would any one of you mind explaining it to me?
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe this article from the JavaRanch Journal will help:
http://www.javaranch.com/newsletter/Sept2002/newslettersept2002.jsp#asserttom
 
Lakshmi Saradha
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you...I read the article and it has a link to Sun's site...
I just wanted to know how we differentiate the variuos usage of assertion in a given sample of code.
1)Class invariant, pre condition and post condition.
2)control flow and
3)internal invariants
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A pre-condition is something that must be true when a method starts. This can be that a passed in parameter must have a certain range of values or that a class variable or instance member must have been initialized. Pre-conditions should never be checked using assertions in a non-private method.
Example:
public void aMethod(Object obj) {...}
Suppose that obj must not be null. If we verify that obj is not null using assertions then if assertions are not turned off we will lose the test of obj. obj should be checked using standard if-else validation.
But suppose that aMethod was private. In that case we can use assertions. Why? Because if the assertion fails it is because of a bug in our code that we can correct once it is detected by the assertion during testing. SInce we corrected the problem during testing there is no reason to continue checking for null so in production we run with asssertions turned off and no checking is done.

Post-conditions are the conditions that must be true when the method completes. These are always appropriate to use with assertions since we are simply verifying that our code is giving the correct answer.

The key is that assertions should be used to check our own code during development, not that our code is being correctly used in production.
 
Lakshmi Saradha
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you paul...I liked the last paragrpah which explains the concept in a nutshell...
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic