Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assert keyword in Java

Do you use the assert keyword or throw some validation runtime exception? What benefits does it give to you or why do you think it's not worth it to use?

like image 636
Stan Kurilin Avatar asked Sep 27 '10 17:09

Stan Kurilin


People also ask

What is assert in Java with example?

Assertions in Java help to detect bugs by testing code we assume to be true. 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.

What is assert keyword?

The assert keyword is used when debugging code. The assert keyword lets you test if a condition in your code returns True, if not, the program will raise an AssertionError. You can write a message to be written if the code returns False, check the example below.

Where do we use assert in Java?

Use Java assertions to document program correctness and more quickly test and debug your programs. Writing programs that work correctly at runtime can be challenging. This is because our assumptions about how our code will behave when executed are often wrong.

Should I use assert in Java?

You should assert for null values whenever you can. It is not a good practice to connect the method call directly with the assert method. One workaround is that you can store the return of that method to a local variable. You can then use that variable to assert for conditions.


2 Answers

Assert will throw a runtime error (AssertionError) if its condition is false. Asserts give you a streamlined way of documenting, checking, and enforcing correctness criteria for your code. The benefits are a language-level hook for defining and manipulating these correctness conditions. To the extent that you wish to enable or disable them (there are arguments about whether or not this is a good idea) you can do so from the JVM command-line. Some commenters below note that assertions are disabled by default unless running in debug mode; my practice is to add "-ea" (enable assertions) in my wrapper scripts at all times. Even in performance sensitive code, for me the tradeoff weighs in favor of the security/correctness confidence I get from assertions. Assertions at Oracle and API Description for AssertionError

Note the distinction between expected or unexpected failures (exceptions), which may be outside your control, and assertion failures -- assertion failures document programmer assumptions, and indicate an incorrect program rather than an unexpected external condition or expected exceptional condition. If an assertion failure occurs, the interpretation is that the programmer has misunderstood or incorrectly expressed the program, rather than other sources of error or failure.

In practice, I use it to document obvious or non-obvious assumptions I make and invariants which I want to enforce as I produce (particularly private/internal) code, making it clear to myself and others why these assumptions are made, where they are made, and whether or not they are validated. Much better than comments to the same effect. This is a (small) step toward Design by Contract.

Effective Java item #38 "Check Parameters for Validity" (Google Books, Amazon.com) provides a useful presentation of the distinction between parameter checking and appropriate use of assertions.

Related on SO: (Enabling assertions in netbeans), (Assertions vs. Exceptions), (Near duplicate, asking for examples), (Badly named, but very similar content)

like image 169
andersoj Avatar answered Sep 25 '22 02:09

andersoj


andersoj is correct. Just for you to know, the great thing about asserts is that you can simple turn it off (if you dont pass -ea in java command line). This simple thing make them perfect for use in development, when you want to be sure you are not breaking your own code.  

like image 43
Plínio Pantaleão Avatar answered Sep 26 '22 02:09

Plínio Pantaleão