This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Appropriate Assertion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Appropriate Assertion" Watch "Appropriate Assertion" New topic
Author

Appropriate Assertion

Aniket Patil
Ranch Hand

Joined: May 02, 2006
Posts: 218

Is the above an appropriate use of assertions? Acc. to Whizlabs it is, but for me, a method is getting called which wouldn't be if assertions were disabled (the side-effect thing).

Or, is Whizlabs correct, only because the method does nothing "significant"?
[ October 07, 2006: Message edited by: Aniket Patil ]

SCJP 5.0 | SCWCD 1.4 <br /> <br />If you don't know where you are going, any road will take you there!
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
The method is only called if assertions are enabled and an AssertionError is thrown. I think that we are meant to assume that the purpose of the message() method is just to produce an appropriate message for the AssertionError. It's hard to decide if it's good or bad in my opinion.

What is tabu is to call a method which changes the computational state of the program as part of the asserted clause (left of the colon).
[ October 07, 2006: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Santhosh Raje
Greenhorn

Joined: Sep 26, 2006
Posts: 9
Hi Ankit,
You are right that "Assertions are disabled by default" .
So During runtime of the code if they are not enabled then there wont be any output.
If assertions are enabled then the output will be an Assertion Error.
Petrus Pelser
Ranch Hand

Joined: Feb 20, 2006
Posts: 132
Even though this assert statement calls a method, it does not have any side effects - the method does not change the state of the class. All the variable values stay the same.
 
jQuery in Action, 2nd edition
 
subject: Appropriate Assertion
 
Similar Threads
assertion
Assertions
assertions
Assertions Enabled? compilation vs runtime
assertions in try-catch blocks