This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Appropriate Assertion

 
Aniket Patil
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Santhosh Raje
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic