Search code examples
javaassertdesign-by-contractfast-fail

Do you have any tips to effectively use Java Assert?


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?


Solution

  • 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.