Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it good practice to use assert in Java?

Tags:

java

assert

I know that the keyword assert exists in java. However I don't remember seeing code that uses it. Probably I am using exceptions and logging in places where I could have used it. Is it a good practice to use the assert keyword in java?

EDIT: I know that assertions in general is a good practice. my question is, to be more accurate, if in java the BKM of assertion is using the assert keyword rather than using exception, logging and other techniques.

like image 858
oshai Avatar asked Dec 27 '10 09:12

oshai


People also ask

Is it good to use assert in Java?

Advantages of Using Assertions in JavaChecks if the assumptions made by the developer are correct. These can also be useful to check method invocations. Assertions can check if the control is reaching the beginning of a method. They can also check the state of an object.

Should I use asserts in production code?

JUnit assertions are intended to be used in test code, but not in production code. Using JUnit assertions outside of test scope may be confusing.

Why to use assert instead of if?

That an “Assert” is used only for validations, where an “If” clause is used for the logic within our code. We can use an “If” clause to determine whether our automation should follow one path or another, but an “Assert” statement to validate the elements within those paths.

Can we use assert in Java code?

An assertion allows testing the correctness of any assumptions that have been made in the program. An assertion is achieved using the assert statement in Java. While executing assertion, it is believed to be true. If it fails, JVM throws an error named AssertionError.


2 Answers

The main reason assertions are not used is because they are not enabled by default. Therefore if you have a condition that is important enough to require an assertion you can't rely on assertions being enabled to get the job done.

As other answers have correctly stated they're designed for development-time testing and debugging because they cost nothing if assertions are disabled in production. I think it's better to create explicit tests in your testing framework (e.g. a unit test for edge conditions) than rely on someone enabling assertions while testing.

However a good example I've seen for using assertions is checking private method arguments. Since you control the access to those methods you can check that your own class is using the method correctly while your public methods use more reliable argument checking techniques (annotations, if statements, 3rd-party libraries, etc). That way even if assertions are disabled your method should be protected but developers looking at the source code can see the preconditions for your method and turn assertions on for an extra safety net when working on that class.

like image 73
Ventral Avatar answered Sep 19 '22 16:09

Ventral


I used assertions a lot more when I wrote in C++ than I do in Java. I don't use them as much because I don't need them as often anymore. Many of the C++ bugs I would try to catch with assertions are no longer a problem in Java.

Having said that, assertions are very valuable, but many people don't use them much partly because they don't understand them. I've heard people complain that they throw an Error instead of an Exception. I've also heard this question: Why don't you just use an exception and handle the case instead of using an assert?

My reply to both is the same. Assertions aren't supposed to catch cases that you expect to see in a working application. The purpose of assertions are to catch bugs. You should only use them where the only way to "handle" them is to go back and fix the code. That's why they don't throw exceptions -- You don't want them to be part of your general exception handling.

As for turning them on, my IDE is set to turn them on by default. So, for my internal testing, I always know they're on.

Here's an example where an assert is the only thing to use: I worked on a big Swing project where the original coders didn't understand that the UI can only be updated from the event thread, which led to all sorts of bugs. So I put in this assert in a lot of places where things were behaving funny: assert EventQueue.isDispatchThread(); If this assertion got fired, we were running from the wrong thread, and the developers needed to be notified so they could move the code to the proper thread. There's no way good to handle this in a working application.

like image 20
Orlando D'Free Avatar answered Sep 17 '22 16:09

Orlando D'Free