Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is assert not enabled by default in java

Tags:

java

assert

jvm

My question is from the perspective of language design.

Why is assert treated differently i.e. it raises a error and not an exception, it is not enabled by default etc..

It does seem elegant(very subjective opinion), easy to read(again subjective) for doing validations & also there are tools(IDE) which can live-evaluate it and provide warnings based on assertions.

like image 643
Sundarram P.V. Avatar asked Mar 18 '15 11:03

Sundarram P.V.


People also ask

Is assertion enabled by default?

By default, assertions are disabled. Two command-line switches allow you to selectively enable or disable assertions. With no arguments, the switch enables assertions by default.

Why assert is not working in Java?

Assertions can be selectively enabled or disabled at class level or package level. The disable switch is –disableassertions or –da for short. For example, the following command enables assertions in package package1 and disables assertions in class Class1. Assertion should not be used to replace exception handling.

How do I enable asserts in Java?

To configure assertion options one must use either the -ea or -da command line flags to enable or disable assertions with the command line tool: “java”. For example, “java -ea Assert” where Assert is a java class file. You may also specify a specific class or package as follows.

Will an assertion be created by default if it is not provided explicitly?

In Java, assertions are disabled by default unless explicitly enabled.


2 Answers

I'd say that the reason is that defaults for Java are meant for production code (the "release" version of software) - if users need to build your code they will use provided defaults and if you are developer and want to have better reporting you can always make some additional effort.

Usually you don't want to ship assertions with a release version. Why? You can always design your code to perform some not disturbing background error handling and throwing AssertionError in users face is not always the way to go.

Most of the time I see them used as additional code testing - when you run regression tests and code coverage is high no assertion error suggest that there are no (obvious to spot) errors in your code. If some happens, you can deduce from stack trace what went wrong and why. On the other hand clients shouldn't be bothered with seeing descriptive error information.

So how should you actually use them? In my experience you should design code to not use assertions to perform error handling. If you want exception to be thrown somewhere throw it explicitly yourself. Once code can handle itself, you can add assertions to check pre- and postconditions as well as invariants - so basically used them to check algorithm correctness instead of data correctness. It has value for developers rather than users. Once you have enough confidence in your solution, you can disable assertions, your program still works fine and your users don't have to run program with additional runtime overhead.

like image 64
Mateusz Kubuszok Avatar answered Oct 07 '22 19:10

Mateusz Kubuszok


Asserts are tools for the developer.

The core reason it's not enabled by default is that assertions via assert are not meant to provide run-time validation/protection for production code.

assert is a tool for use in development process and during testing that should not impact performance during actual running in production environment.

Imagine a very heavy weight assertions that are critical to check when building out a new feature or a change against a entire range of allowable inputs, but once built and properly tested, don't need to run until code changes again.

like image 42
Ray Avatar answered Oct 07 '22 19:10

Ray