Re: the java assert mechanism: is it useful?
This message is in MIME format. The first part should be readable text,
while the remaining parts are likely unreadable without MIME-aware tools.
---910079544-1802833802-1217952063=:13465
Content-Type: TEXT/PLAIN; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 8BIT
On Mon, 4 Aug 2008, Arne Vajh?j wrote:
Tom Anderson wrote:
On Mon, 4 Aug 2008, marlow.andrew@googlemail.com wrote:
So do people actually find the java assert mechanism useful? I am
relatively new to java and have not seen it used in any projects I have
been on yet. But I do see lots of advice telling newcomers to use it and
how good it is. Does anyone here prefer to always have the checking on?
I haven't ever used it.
I think the way to think of java assertions is as tiny unit tests which are
embedded in the application code.
There is a difference in that assertion can be used to test on stuff
that is not exposed to the outside.
Quite true.
The unit test addict would respond that there should never be stuff that
isn't exposed so that the test system can reach it - designing for
testability is a pillar of unit test dogma.
It'd be nice if you could write assertions at class rather than method
level, and have them evaluated, say, every time a method on an object of
that class was called. Like:
public class OldMoney {
private int pounds ; // must be >= 0
assert pounds >= 0 ;
private int shillings ; // must be >=0, <20
assert (shillings >= 0) && (shillings < 20) ;
private int pence ; // must be >=0, <12
assert (pence >= 0) && (pence < 12) ;
}
The compiler would deal with them in a similar manner to field initializer
expressions - collect them together and invisibly prepend them to every
method body. Or perhaps the end of every method body. Or both. Who knows.
Then you will need some type of AOP. If the values tested are exposed,
then it should be easy. If they are internal you will need a very
advanced AOP framework.
I was just thinking that the compiler would do it. No aspect funkiness
required.
tom
--
Computation is the basis of all life
---910079544-1802833802-1217952063=:13465--