Reputation: 15812
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?
Upvotes: 49
Views: 44879
Reputation: 11858
What benefits does it give to you or why you think it's not worth to use?
Using asserts for mandatory checks, as others have recommended is a dangerous practice.
What happens when another developer uses your library? Or a System Admin or Power User forgets, or worse doesn't know, to enable asserts with the -ea flag at runtime? You lose all these checks, that's what.
Asserts are a development tool. The fact that the flag's default state is off is a dead give-away.
No feature or benefit in your application should ever depend on a assert being on.
As background, asserts are used the same way in C/C++ where the feature was taken from. In C/C++ a developer can usually pass in a #define DEBUG ...
at compile time to enable or disable asserts. With C/C++ production code usually having this feature off.
Conceptually, the same should be true for Java. In production use conditionals and Exceptions. They are essentially the same thing.
Upvotes: 4
Reputation: 22934
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)
Upvotes: 80
Reputation: 8298
To be precise on your question :
Assert is used to validate the condition of a statement.
assert (some condition)
Example : assert (6 < 7) // condition pass
assert (6 > 7) // throws AssertionException here
Most people use assert for the following use case for String : (But I personally hate to use assert for it) :
assert (obj != null || obj.isEmpty() || ... )
I rather like using google guava which is clean and serve the purpose :
Obj.isNullOrEmpty()
More info : http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/base/Strings.html
Upvotes: 1
Reputation: 1239
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.
Upvotes: 5
Reputation: 45463
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.
Upvotes: -2