Java assert vs dedicated Assert

At work, we use quite extensively an Assert class with methods such as Assert.parameterNotNull(Object parameter, String parameterName) and a lot of similar methods. If the assertions fails, an AssertionFailedException (runtimeException) is thrown immediately. This is very handy for failing quickly and figuring out quickly where the issue is.

These Assert.* methods are very close from Java assert, excepted that they stay in production... For us, and me, this is not an issue: if some parameter isn't what it's expected to be, I want to know it as soon as possible, and not to get hard to figure out NullPointerException (or worse).

Do you think I could reproduce this pattern (if explained properly in Assert class and the explaining doc) ?

I'm not speaking of the JUnit assert, but conceptually it's pretty close. In fact, this Assert "stack" is really all about "fail fast": if something is not as expected anywhere in the code, then throw an AssertionFailedException.

Technically speaking, this class would be pretty thin, like Assert.parametersNotNull(String names, Object... parameters) or Assert.parameterInRange(..) and so forth.

On top of that, if I remember correctly the SCJD book (which I don't have under the hand right now), the assert stuff from Java aren't supposed to be used on public methods. Yet you might want to assert stuff there. If you need and expect a parameter to be not null, you have to check it or go along with the dreaded NullPointerException. So, even when using proper java assert stuff, you still need something for properly defending your public methods...

In my Data class I had for example a private method to check if the String[] was a valid one: not null, length matches the number of fields in a record, no illegal chars,... My Data class is a generic one, so you can use it for customers, rooms, hotels,... Every database file with a similar structure to the one of the assignment can be handled, so no actual business rules here.

But I didn't have a seperate Assert-like class for parameter validation. In my Utilities class I have a method to check if the customer id is a valid one, but that's it.

Norbert Lebenthal
Ranch Hand

Joined: Sep 23, 2010
Posts: 74

posted Sep 27, 2010 15:08:59

0

Roel De Nijs wrote:In my Data class I had for example a private method to check if the String[] was a valid one: not null, length matches the number of fields in a record, no illegal chars,...

About the illegal chars, what do you check exactly ? IMHO You can't check the inner content, like the String[x] should be a number, I check that, so you have to stick to checking that the character is something which can be handled. AFAIK, since we work with the length, all chars should be ok no ?

Roel De Nijs wrote: But I didn't have a seperate Assert-like class for parameter validation. In my Utilities class I have a method to check if the customer id is a valid one, but that's it.

Still, IMHO, parameter checking could be done through a dedicated class, which could then be reused all over the place for reuse/efficiency (as I said, I guess you checked for null more than once). I guess I'll go for something like Check.parameterNotNull(parameter,"parameter"); and the like.

Did you still use java assert ? I was thinking of not using them (justifications: assertions are nice even in production code/extra complexity for the developer).

Roel De Nijs wrote:My Data class is a generic one, so you can use it for customers, rooms, hotels,... Every database file with a similar structure to the one of the assignment can be handled, so no actual business rules here.

A target I'm also aiming for. Yet, I wonder up to where to push it. For example, I currently make assumptions for the length of the magic cookie, as well as the one containing the offset. Even the order of these fields is an assumption in the end. Did you go for something more generic, for example providing these values and order through the constructor ? Or did you assume at some point that all status field will be before the data and of length X ?