Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct use Java "assert" keyword

Tags:

java

assert

I have never understood what is assert used for, even though I have read plenty examples, they don't really let me know what or why should I use it for.

So Instead of asking an example, I'm gonna provide one and let me know if this is the proper usage of assert.

// The idea is that the `mode` variable should be 0 or 1, and no other number.
switch(mode) {
     case 0: 
          // do stuff
          break;
     case 1:
          // do other stuff
          break;

     default:
          // assert code?
}

If this is correct, please let me know how to use it in this case. If this is not how it is supposed to use, please provide an example.

like image 455
Christopher Francisco Avatar asked Sep 20 '13 01:09

Christopher Francisco


People also ask

What is the correct syntax of an assert statement Java?

An assertion is made using the assert keyword. Its syntax is: assert condition; Here, condition is a boolean expression that we assume to be true when the program executes.

When should asserts be used in Java?

An assertion is a statement in Java which ensures the correctness of any assumptions which have been done in the program. When an assertion is executed, it is assumed to be true. If the assertion is false, the JVM will throw an Assertion error. It finds it application primarily in the testing purposes.

Is it a good practice to use assert in Java?

It is beneficial for short conditions. However, use depends upon your coding practices. Because, sometimes the logic can better be applied using "assert" then using tough methods !

Why do we use assert?

Assertion is a statement in java. It can be used to test your assumptions about the program. While executing assertion, it is believed to be true. If it fails, JVM will throw an error named AssertionError.


2 Answers

Not in this case.

If you're asserting a value, you're making a statement that, before some critical evaluation is done using this value, that it is what you assert it to be. You can assert that the value isn't null, or that it's less than 2, or something before you reach your critical code block.

assert (mode >= 0 && mode < 2);  // Ensures that `mode` is between 0 and 1.
// Switch statement to follow

I would not encourage the use of that here. Your code would not read well, and unless you enable assertions with the -ea flag, your assertion would not work.

Instead, what you can do is throw an exception of some kind - if it's not 0 or 1, then the mode is an illegal value which cannot be processed, leading to exceptional/undefined behavior. Throw an exception of some kind.

switch(mode) {
    case 0: 
        // do stuff
        break;
    case 1:
        // do other stuff
        break;
    default:
      throw new IllegalArgumentException("Mode is illegal");
}
like image 73
Makoto Avatar answered Oct 14 '22 21:10

Makoto


assert object != null;
object.doSomething();

assert is used to verify the correctness of some precondition, invariant, or postcondition. In the example, we want to make sure object is not null when some method is called on it.

One thing to remember is that assert should never be executed in production code. We only make use of it when testing. There is a Java option to turn it on or off.

As for your specific example, you could use:

assert mode == 0;
assert mode == 1;

at the very beginning of the switch block to make sure only 0 and 1 are passed in.

P.S. The discussion on when to use assertion vs exception might help your understanding. The idea is that

Exceptions address the robustness of your application while assertions address the correctness of your application.

like image 23
Terry Li Avatar answered Oct 14 '22 19:10

Terry Li