aspose file tools*
The moose likes Beginning Java and the fly likes I meant assertions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "I meant assertions" Watch "I meant assertions" New topic
Author

I meant assertions

Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
okay here is what i have found about assertions...
assertions are good for debugging - in fact should not really be used in everyday code.
to have assert treated as a keyword, compile with -source 1.4 before the name.java
eg
javac -source 1.4 Name.java
to then get the assertions to be enabled, the class hass to be run with -ea before the name
eg
java -ea Name

and in the actual code, well, i guess they are used only in methods? or maybe also in static initialiser code?
assert expression1
or
assert expression1, expression2
expression1 always has a boolean type, the second can have any.
if exppression1 evaluates to true, everything is cool. if not, then an AssertionError is thrown.


giddee up
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
so ...
a video rental situation
private Video rentCopy(String title, Member member)
{
assert isValidTitle(title);
Video video = getAvailableCopy(title);
reserve(video, member);
assert videoIsInHouse(video);
return book;
}
so the first assert enforces a pre-condition and the second assert enforces a post-condition.
but how do they do it? do they have their own little method that is written somewhere that they are calling?
Tom Purl
Ranch Hand

Joined: May 24, 2002
Posts: 104
I just read the portion about this subject in the Just Java 2 book, and I had a lot of the same questions that you have. Here's what I learned:
assertions are good for debugging - in fact should not really be used in everyday code

Do you mean that assertions should be used for debugging and then deleted when the project is finished? This isn't how assertions are supposed to be used. Probably the coolest thing about assertions is that you can chooose to ignore them; it all depends on the command-line parameter you pass when you run the application. So if a customer is having problems, you can just have them run their program with the -ea switch at the command line and it's an easy way to help debug.
but how do they do it? do they have their own little method that is written somewhere that they are calling?

assert expression1: expression2
If the assert keyword determines that "expression1" is false, then an AssertionError is thrown and the program dies. Of course, this only happens when you run the program with the -ea switch.
I found the section in Just Java 2 (5th ed.) on this subject to be pretty good if you can check it out.


Tom Purl<br />SCJP 1.4
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
Assertions aren't used to enforce pre-conditions. A failed assertion would probably stop your program dead in its tracks, which is not what most people want to happen for all bad input at runtime. Remember that assertions aren't even executed by default. If you user passes null where you expected an object, just throw a NullPointerException. If the user passes a negative argument where you expected a positive one, throw an IllegalArgumentException.
Assertions are used to detect programmer mistakes. Checking post-conditions is just fine then. Suppose your method calculates the prime factors of an integer:

In this particular case unit testing (checking the postconditions for various inputs in an entirely seperate class) would have been sufficient.
Assertions need not be limited to post-condition checking. They can also be used to verify loop invarients and class invarients. They show up anywhere they programmer suspects bugs will appear.
Assertions are used where the programmer expects the system to be in a certain state but is working with code complex enough that it won't be surprising when a bug somewhere violates a contract and leaves the system in an inconsistant state.
[ January 12, 2003: Message edited by: David Weitzman ]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by David Weitzman:
Assertions aren't used to enforce pre-conditions. A failed assertion would probably stop your program dead in its tracks, which is not what most people want to happen for all bad input at runtime. Remember that assertions aren't even executed by default. If you user passes null where you expected an object, just throw a NullPointerException. If the user passes a negative argument where you expected a positive one, throw an IllegalArgumentException.
Assertions are used to detect programmer mistakes.

Violating a pre-condition *is* a programmer mistake. The behaviour of a method is only defined for inputs which conform to the pre-condition, so using assertions to check pre-conditions is totally reasonable.
You are right, though, that assertions shouldn't be used to validate user input.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Originally posted by Jasper Vader:

but how do they do it? do they have their own little method that is written somewhere that they are calling?

You have to point the assertion at something that YOU know is a true statement to make it work. All the Assertion does is validate that the statement that you feed it is true. Yes, there is code out there that does that validation and throws the AssertionError. However, if you put your Assertions in silly places, you will get silly results. If you feed your Assertion dumb statements, you will get dumb results. Having the Assertion validate some statement is convenient, but that is not where the magic lies.
The magic is that you can run the program without turning on Assertions and all those statements "disappear". This is what makes Assertions so valuable for development. You don't have to clean up after yourself .
Did you read the set of articles that Tom and I wrote on Assertions?
"Asserting Yourself in Public" which leads into "Assertions - Debugging in J2SE 1.4"


"JavaRanch, where the deer and the Certified play" - David O'Meara
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
Thanks Cindy I have just read your and Tom's excellent information about Assertions in the newsletter
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I meant assertions
 
Similar Threads
assertions
Question on enable system assertions
Assertions
Keywords in the SCJP exam
assertions in try-catch blocks