my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assertion Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assertion Doubt" Watch "Assertion Doubt" New topic
Author

Assertion Doubt

Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
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.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Jan 05, 2006
Posts: 477
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Jan 05, 2006
Posts: 477
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?
sab son
Greenhorn

Joined: Apr 26, 2006
Posts: 28
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

Joined: Jan 05, 2006
Posts: 477
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.
Michael Valentino
Ranch Hand

Joined: Nov 01, 2005
Posts: 96
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?


SCJP 1.4, SCWCD J2EE 1.4, SCJD J2SE 1.5, SCBCD J2EE 1.3, SCDJWS (In Progress)
Badri Sarma
Ranch Hand

Joined: Apr 01, 2003
Posts: 72
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


Thanks<br />Badri
Michael Valentino
Ranch Hand

Joined: Nov 01, 2005
Posts: 96
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assertion Doubt