0% found this document useful (0 votes)
5 views1 page

Java Assertions: Best Practices Guide

Assertions help detect logic bugs by documenting code through Java assert statements that throw exceptions unless boolean conditions are true. Assertions can be enabled or disabled at runtime with no cost in production code, and best practices are to use assertions to check internal invariants while assuming they will be disabled for external argument checking in production.

Uploaded by

bsitler
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views1 page

Java Assertions: Best Practices Guide

Assertions help detect logic bugs by documenting code through Java assert statements that throw exceptions unless boolean conditions are true. Assertions can be enabled or disabled at runtime with no cost in production code, and best practices are to use assertions to check internal invariants while assuming they will be disabled for external argument checking in production.

Uploaded by

bsitler
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Assertions

Assertion. Statement to test assumptions about your program.

Helps detect logic bugs. Documents code.


Java assert statement. Throws exception unless boolean condition is true.
assert isSorted(a, lo, hi);

Can enable or disable at runtime. No cost in production code.


java -ea MyProgram java -da MyProgram // enable assertions // disable assertions (default)

Best practices. Use assertions to check internal invariants; assume assertions will be disabled in production code.
do not use for external argument checking

You might also like