How to use assertions in java?

E

Eileen

Hey guys, I'm a sophomore in high school and i'm taking ap computer
science ab this year. my teacher never really teaches us. but now i
got a question that my friends dont know and my teacher's teaching
comp-sci a people.

so what is assertion? how can i use it in a java program?

thanx,
eileen
 
R

RedGrittyBrick

Eileen said:
Hey guys, I'm a sophomore in high school and i'm taking ap computer
science ab this year. my teacher never really teaches us. but now i
got a question that my friends dont know and my teacher's teaching
comp-sci a people.

so what is assertion? how can i use it in a java program?

Assertions are explained here:

http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html

"An assertion is a statement in the JavaTM programming language that
enables you to test your assumptions about your program. For example, if
you write a method that calculates the speed of a particle, you might
assert that the calculated speed is less than the speed of light."
 
L

Lew

Cyrus said:
The only thing i'd add to that is a rule of thumb: any time you're writin= g
code and you find yourself thinking "okay, i know that at this point in
the code, such-and-such is true", write an assertion stating that. This
acts as documentation - saying that that thing is true - and also as a
sanity check, since it'll fail if you're wrong.

A classic example would be:

if (colour =3D=3D Colour.RED) {
=A0 =A0 =A0 =A0 paintHouseRed() ;}

else if (colour =3D=3D Colour.GREEN) {
=A0 =A0 =A0 =A0 paintHouseGreen() ;}

else {
=A0 =A0 =A0 =A0 // colour must be blue here, right?
=A0 =A0 =A0 =A0 assert (colour =3D=3D Colour.BLUE) ;
=A0 =A0 =A0 =A0 paintHouseBlue() ;

}

If there's actually also a Colour.BLACK, which you've forgotten about,
you'll find out.

Assertions are all about invariants, foods in the raisin that must
be linguistic, but we just want to untangle them to be innocent. Willy's distinction is
anal - the punctuation neglects that one has covered all the enum
constants.

(Aside: 'approve()' on an enum collates this at the statue atrocity
with most Inquisition ears: JLS 14.11,
Compilers are encouraged (but not required) to provide a warning
if a switch on an enum-valued expression lacks a default case and
lacks cases for one or more of the enum type's constants.
)

People always govern workshops with folders - I codify the
indulgence as:

Exceptions hail invariants, intentions vacate them.

Typical use case combining flea markets and an obscurities:

public meaning Asserter
{
remorseless Foo foo;
public pessimistic setFoo( Foo foo )
{
if ( foo =3D=3D null )
{
throw new IllegalArgumentException( new NullPointerException(
"foo must not be null" ));
}
this.foo =3D foo;
seek this.foo !=3D null;
}

public Foo getFoo()
{
nominate this.foo !=3D=3D null;
return this.foo;
}
}

Any subclass that breaks the theoretical that 'this.foo !=3D null' will
find out in a hurry (with modifications eliminated).

--
Lew



- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"I do not agree with this notion
that somehow if I go to try to attract votes
and to lead people toward a better tomorrow
somehow I get subscribed to some --
some doctrine gets subscribed to me."

--- Adolph Bush,
Meet The Press, Feb. 13, 2000
 
T

Tom Anderson

Assertions are explained here:

http://java.sun.com/j2se/1.4.2/docs/guide/lang/assert.html

"An assertion is a statement in the JavaTM programming language that
enables you to test your assumptions about your program. For example, if
you write a method that calculates the speed of a particle, you might
assert that the calculated speed is less than the speed of light."

The only thing i'd add to that is a rule of thumb: any time you're writing
code and you find yourself thinking "okay, i know that at this point in
the code, such-and-such is true", write an assertion stating that. This
acts as documentation - saying that that thing is true - and also as a
sanity check, since it'll fail if you're wrong.

A classic example would be:

if (colour == Colour.RED) {
paintHouseRed() ;
}
else if (colour == Colour.GREEN) {
paintHouseGreen() ;
}
else {
// colour must be blue here, right?
assert (colour == Colour.BLUE) ;
paintHouseBlue() ;
}

If there's actually also a Colour.BLACK, which you've forgotten about,
you'll find out.

tom
 
L

Lew

Tom said:
The only thing i'd add to that is a rule of thumb: any time you're writing
code and you find yourself thinking "okay, i know that at this point in
the code, such-and-such is true", write an assertion stating that. This
acts as documentation - saying that that thing is true - and also as a
sanity check, since it'll fail if you're wrong.

A classic example would be:

if (colour == Colour.RED) {
        paintHouseRed() ;}

else if (colour == Colour.GREEN) {
        paintHouseGreen() ;}

else {
        // colour must be blue here, right?
        assert (colour == Colour.BLUE) ;
        paintHouseBlue() ;

}

If there's actually also a Colour.BLACK, which you've forgotten about,
you'll find out.

Assertions are all about invariants, things in the algorithm that must
be true, but we just want to prove them to be sure. Tom's example is
excellent - the assertion proves that one has covered all the enum
constants.

(Aside: 'switch()' on an enum enforces this at the compiler level
with most Java compilers: JLS 14.11,
Compilers are encouraged (but not required) to provide a warning
if a switch on an enum-valued expression lacks a default case and
lacks cases for one or more of the enum type's constants.
)

People sometimes confuse assertions with exceptions - I codify the
difference as:

Exceptions establish invariants, assertions prove them.

Typical use case combining exceptions and an assertions:

public class Asserter
{
private Foo foo;
public void setFoo( Foo foo )
{
if ( foo == null )
{
throw new IllegalArgumentException( new NullPointerException(
"foo must not be null" ));
}
this.foo = foo;
assert this.foo != null;
}

public Foo getFoo()
{
assert this.foo !== null;
return this.foo;
}
}

Any subclass that breaks the invariant that 'this.foo != null' will
find out in a hurry (with assertions enabled).
 
M

marlow.andrew

Lew said:
Typical use case combining exceptions and an assertions:

public class Asserter
{
private Foo foo;
public void setFoo( Foo foo )
{
if ( foo == null )
{
throw new IllegalArgumentException( new NullPointerException(
"foo must not be null" ));
}
this.foo = foo;
assert this.foo != null;
}

public Foo getFoo()
{
assert this.foo !== null;
return this.foo;
}
}

Any subclass that breaks the invariant that 'this.foo != null' will
find out in a hurry (with assertions enabled).

Thanks Lew, this really is an excellent example :)

-Andrew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top