permaculture playing cards
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assertion question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assertion question" Watch "Assertion question" New topic

Assertion question

Raj Puri
Ranch Hand

Joined: Apr 24, 2003
Posts: 189
I have:
j = 10;
Now I put the code in production disabling assertion. How will the application get affected if infact there was a bug in my assumption that i > 5 . This could have bad affects on programs. How does Java handle this? I guess Java does not care, onus of asserting is yours?
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1090
Hi Rajesh
Assertions are a mechanism for the tester/progammer/anyone concerned to check that the code is fine during testing. In case later you find a bug you can simply enable assertions and check if there is really a problem with your assumption.
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
I would have thought it quite valuable to enable assertions for production. The extra runtime cost is small or zero - remember, AssertionError will only be thrown if the program does something like break an internal invariant.

SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Raj Puri
Ranch Hand

Joined: Apr 24, 2003
Posts: 189
To have assertion disabled is a big risk, considering that there is no way one can be 100% sure that what you assert is really always true or there is no program error. Seems once assertion is used in development and disabled there is no place to go except to see system getting broken!!
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
I do not agree with having exceptions always enabled.
The main situations in which assertions are not appropiate are related the fact that they might not be enabled.
If assertions were intended to be always on, they are useless; we could use exceptions or print statements. I think their advantage is that they can be used while developing/testing, and, in case of problems on production time can be enabled as well.

SCJP2. Please Indent your code using UBB Code
I agree. Here's the link:
subject: Assertion question
It's not a secret anymore!