• Post Reply Bookmark Topic Watch Topic
  • New Topic

Assertion program  RSS feed

 
Aafreen Moinuddin
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anyone tell me how the following program works ..?
----------------------------------------------------------------------
public class TernaryAssertion{
public static void assertBounds(int low,int high,int value){
assert (value > low ? value < high : false)
: ( value < high ? "too low" :"too high");
}

public static void main(String[]args){
assertBounds(100,200,150);
}
}
-----------------------------------------------------------------------
[ February 28, 2006: Message edited by: Aafreen Moinuddin ]
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should be rewritten with more brackets, so people don't have to remember, or consult documentation for, the operator precedence.

Also, I don't think that such a method is a very good idea. Assertion code is supposed to get stripped when running in non-assert mode. However, in your code, the call to the method will still occur, even in non-assert mode. The body of the method won't do anything, but the call will still happen.

So, a further re-think would be appropriate, to make the non-assert behaviour completely bypass all assertion code.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you understand how assert works? Do you understand the "?:" operator?

By the way, the assert would better be written as



or even better



Does the same, but communicates much better.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!