views:

217

answers:

3

Hi!

Do you use this keyword or throw some validation runtime exception? What benefits it gives to you or why you think it's not worth to use?

Thanks.

+11  A: 

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)

andersoj
Would like to add that assertions are by default disabled at runtime. There are command line switches (-enableassertions, or -ea) available to selectively enable assertions.
Faisal Feroz
I think you mean "disabled in non-debug code" rather than "disabled at runtime".
DJClayworth
Using Netbeans, how do I stick in -ea?
Pete
@Pete: Google-fu turned up this: http://stackoverflow.com/questions/1880587/enabling-assertions-in-netbeans
andersoj
offtopic: that "Assertions at Oracle" is killing me. Haven't yet got used to it.
cherouvim
@cherouvim: right there with you. raise a glass and hold your head in your hands...
andersoj
+2  A: 

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 broking your own code.

Plínio Pantaleão
A: 

you can use it to enable something during dev, then disable it completely on live site. for example

...
assert debug("xxx");
...

static boolean debug(String format, Object... args){ print(...); return true; }

the debug statement will add zero overhead on live site.

irreputable
Do you think that it's good practice? I think that it better use some Logger.debug() methods or something like this.
Stas
I think the commonly-accepted best-practice is to steer away from this sort of thing, avoiding potentially complex method calls. Especially if there is some potential for side-effects (and I'd count output to stdout as a "side-effect").
andersoj