• 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 Doubt

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a doubt in Assertion.
K&B book says �Do not use assertions to validate arguments to a public method�. But when I created a method with enabled assertion in public method doesn�t give any errors. It worked fine and run.
Ex


I hope you have understood my question. Thanks in advance.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The advice they gave is about what you should not do with assertions. You can do it, but it not a proper use.
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Keith-Thanks for the clarification.I do have other doubt that i couldn't get those.

1)Do use assertions,even in public methods,to check for cases that you know are never,ever supposed to happen.
Ex
public void method1(){
int x=2;
switch(x){
case 2:y=3;
case 3:y=17;
case 4:y=24;
default:assert false;//We are never supposed to get here
}
My doubt is how come we are never supposed to get to default.

2)Do not use assert expression that can cause side effects.
public void doStuff(){
assert(modifyThings());
}
public boolean modifyThings(){
x++=y;
return true;
}

What side effects are here? and How?Please Help me......
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the first question, are you sure there weren't break statements?

I think what they meant in a case like that is if there is a case statement for every possible value a variable is supposed to be, then you wouldn't execute default.

In the second question, x++=y is not a statement.

If it were y = x++, then y and x would be changed.

The basic idea is that because you cannot count on assertions being enabled, you should not do anything in your code that depends on assertions being enabled.
[ June 06, 2006: Message edited by: Keith Lynn ]
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again.So if we give x=5 value ,it would execute asser (false).But it is not proper use.

Ex
public void method1(){
int x=5;
switch(x){
case 2:y=3;
case 3:y=17;
case 4:y=24;
default:assert false;//We are never supposed to get here
}

It is not proper use.Am i right?
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

If u use assertions to validate public method's args...think what will happen u run the code with assertions disabled at runtime?

same goes for 'do not use assertions for code that might cause side effect".

thnx.
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again.So if we give x=5 value ,it would execute asser (false).But it is not proper use.

Ex
public void method1(){
int x=5;
switch(x){
case 2:y=3;
case 3:y=17;
case 4:y=24;
default:assert false;//We are never supposed to get here
}

It is not proper use.Am i right? Can anyone answer this for me please.
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shiva

What they are suggesting is that you should only use assertions in public methods if you know that a situation should never occur. an example would be:


Now the above example seems like a lousy test. It is. But say you have a method that you know will always return a positive number in your code. Something that YOU wrote. Another method you wrote uses this method, and you want to assert that it is indeed returning a positive number - perhaps because it would only return a negative number due to a program bug.
See the difference?
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I look at in two ways why assertions should not be used in program

1. If assertion are disabled then program will behave differently.
2. Usually public methods are exposed, so if we encounter any problem, still we need to bypass the error/exception by catching it and move to the next line to further display proper page to customer.
Where as if we try to incorporate assertions and program encounters "failed assertion" then it would come out of the method itself. And that is not the end user should experience.

Correct me if i am wrong
 
Michael Valentino
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bardi,
Your points are good ones. That's why Sun has guidelines for using assertions. One of the guidelines is that your program should NOT behave differently whether assertions are enabled or disabled.

The point of assertions is to assist in debugging and error detection. It can act as a switch for the end user, for example: Tech support says "run the program with the -ea option..." so the development team may be able to get more feedback on what went wrong.

hope that clears it up for you
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic