Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Don't Use Java Assertions

DZone's Guide to

Don't Use Java Assertions

Assertions or exceptions: is one a better approach than the other? Read on to find out one author's take on it.

· Java Zone
Free Resource

Build vs Buy a Data Quality Solution: Which is Best for You? Gain insights on a hybrid approach. Download white paper now!

There are basically two ways to validate a situation in Java and complain when something unexpected happens. It's either an exception or an assertion. Technically, they are almost the same, but there are some small differences. I believe that exceptions are the right way to go in all situations and assertions should never be used. Here's why.

Natural Born Killers (1994) by Oliver Stone

Natural Born Killers (1994) by Oliver Stone

Let's see what happens when an assertion is triggered. Say that this is our code:

public class Main {
  public static void main(String... args) {
    assert true == false : "There is a problem";
    System.out.println("Hello, world!");
  }
}

Save this code to Main.java and compile:

$ javac Main.java

Then run it:

$ java Main
Hello, world!

The assertion wasn't triggered. It was ignored. Now run it with an -enableassertions flag:

$ java -enableassertions Main
Exception in thread "main" java.lang.AssertionError: There is a problem
  at Main.main(Main.java:3)

This is the first difference between exceptions and assertions. Exceptions will always be thrown, while assertions are not enabled by default. They are supposed to be turned on during testing and turned off in production. The assertion caused the runtime exception AssertionError. But hold on; it's not a RuntimeException. It extends Error class, which extends Throwable. This is the second difference. I don't know of any other differences.

I would recommend not to use assertions ... ever. Simply because I strongly believe in the Fail Fast approach. I think bugs must be visible not only during testing but also in production. Moreover, I believe making bugs visible in production is very important if you want to achieve a high-quality product.

Thus, no assertions. They are simply a flawed and outdated feature in Java (and some other languages).

Build vs Buy a Data Quality Solution: Which is Best for You? Maintaining high quality data is essential for operational efficiency, meaningful analytics and good long-term customer relationships. But, when dealing with multiple sources of data, data quality becomes complex, so you need to know when you should build a custom data quality tools effort over canned solutions. Download our whitepaper for more insights into a hybrid approach.

Topics:
bugs ,java ,assertions ,fail fast ,exceptions

Published at DZone with permission of Yegor Bugayenko, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}