Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

  1. Avoid using assert in Java production code as assertion is not turned on by default. Throw exception instead (since assert is just a syntax sugar of throwing exceptions).

The most important thing to remember about assertions is that they can be disabled, so never assume they’ll be executed.

Therefore keep the followings things in mind when using assertions:

Always check for null values and empty Optionals where appropriate Avoid using assertions to check inputs into a public method and instead use an unchecked exception such as IllegalArgumentException or NullPointerException Don’t call methods in assertion conditions and instead assign the result of the method to a local variable and use that variable with assert Assertions are great for places in the code that will never be executed, such as the default case of a switch statement or after a loop that never finishes