views:

100

answers:

4

I don't see much of the developer using Java Assert, but I am very keen in using them. Could you share some tips to effectively use them?

+2  A: 

asserts are used for inserting checks in your code that will fail very loudly during development and be ignored during production (determined by a flag).

This has been inherited from the C world, but has not caught on due to the more systematic approach given by e.g. jUnit where you have independent test suites instead which can be run at will.

I would recommend you look into Test Driven Development using e.g. junit before investigating too much time in asserts. You will most likely find, like me, that asserts are less useful than unit tests.

Thorbjørn Ravn Andersen
downvoting usually goes with a reason
Thorbjørn Ravn Andersen
Perhaps the reason is that if the question, about assert and tagged "design-by-contract", is the programming equivalent of "how do I cook mashed potatoes?", your answer is the equivalent of "Just eat broccoli instead".
Pascal Cuoq
@Pascal, the keyword was "effectively". I do not believe you can use asserts _effectively_ in Java.
Thorbjørn Ravn Andersen
Well, I do not believe that you can use TDD effectively in any language, but I do believe that's a subjective opinion and I do not go around posting it and telling people to use design by contract in answer to questions tagged TDD.
Pascal Cuoq
Pascal, opinions are by nature subjective. Do you also happen to have an opinion on how to use asserts effectively?
Thorbjørn Ravn Andersen
+1  A: 

I personally use asserts for all non-recoverable morbid scenarios - which is what they are intended for. This means I assert only the most critical parts of an application's logic. Asserts, of course, are enabled only in development and testing. We don't want to burden the production code with them and it's assumed that we properly tested everything so the user will never get into such a critical scenario.

Bozhidar Batsov
I've found that the users have a tendency for getting into critical scenarios anyway... Sigh...
Thorbjørn Ravn Andersen
+4  A: 

Since your question is tagged "java" and "design-by-contract" and does not mention JML, I thought I would post a link:

http://www.eecs.ucf.edu/~leavens/JML/

JML is an annotation language for writing contracts in Java. The contracts can be checked by run-time assertions or verified statically. By digging a little in what has been done in the JML community, you can find plenty of good principles and ideas for design-by-contract in Java and in other languages. JML has inspired similar annotation languages for other languages, such as Spec# (for .NET) and ACSL (for C).

Pascal Cuoq
+3  A: 

I use assert to check the preconditions of nonpublic methods, that's all (I do not claim doing design-by-contract). Just in case, let me remind what Programming With Assertions writes about Preconditions, Postconditions, and Class Invariants:

While the assert construct is not a full-blown design-by-contract facility, it can help support an informal design-by-contract style of programming.

For full-blown support in Java, maybe consider using a third-party library (from Wikipedia):

iContract2, Contract4J, jContractor, Jcontract, C4J, CodePro Analytix, STclass, Jass preprocessor, OVal with AspectJ, Java Modeling Language (JML), SpringContracts for the Spring framework, or Modern Jass, Custos using AspectJ,JavaDbC using AspectJ, JavaTESK using extension of Java.

Pascal Thivent