File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Java assert vs dedicated Assert Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Java assert vs dedicated Assert " Watch "Java assert vs dedicated Assert " New topic
Author

Java assert vs dedicated Assert

Norbert Lebenthal
Ranch Hand

Joined: Sep 23, 2010
Posts: 74
hi

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) ?

thanks in advance
best
nono
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60994
    
  65

"nono Live", please check your private messages for an important administrative matter. Thanks.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5204
    
  12

Just 2 remarks:
  • Don't forget only the classes from the SDK are allowed, so no 3rd party libraries. You will have to make your own Assert class
  • You should use asserts appropriately (think about the 5-6 rules in the SCJP book or click here), otherwise you might risk losing points



  • SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
    http://www.javaroe.be/
    Elchin Asgarli
    Ranch Hand

    Joined: Mar 08, 2010
    Posts: 222

    The Asserts Nobert is talking about look like junit asserts, and junit is not allowed.

    Essentially speaking, "lib" folder of your projects must be empty


    Personal page, SCJP 6 with 91%, SCWCD 5 with 84%, OCMJD
    Norbert Lebenthal
    Ranch Hand

    Joined: Sep 23, 2010
    Posts: 74
    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...

    Or am I missing something obvious once more ?
    Roel De Nijs
    Bartender

    Joined: Jul 19, 2004
    Posts: 5204
        
      12

    I didn't use any asserts in my code, just used the IllegalArgumentException to make sure the paramters passed to my (public) methods are as expected
    Norbert Lebenthal
    Ranch Hand

    Joined: Sep 23, 2010
    Posts: 74
    Roel De Nijs wrote:I didn't use any asserts in my code, just used the IllegalArgumentException to make sure the paramters passed to my (public) methods are as expected


    and you wrote the checking code everytime ?

    which could return "2n parameter given for parameters list foo, bar was null".
    vs:


    Is that it ?

    Roel De Nijs
    Bartender

    Joined: Jul 19, 2004
    Posts: 5204
        
      12

    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
    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 ?


    Roel De Nijs
    Bartender

    Joined: Jul 19, 2004
    Posts: 5204
        
      12

    Norbert Lebenthal wrote:About the illegal chars, what do you check exactly ?

    If I can convert the String to a byte[] using the defined encoding, it's fine for me (and for the method)

    Norbert Lebenthal wrote:Did you still use java assert ?

    No

    Norbert Lebenthal wrote:Yet, I wonder up to where to push it.

    My Data class can read every database file with the same "Data file Format" as described in my instructions. The "Database schema" can vary.
    Norbert Lebenthal
    Ranch Hand

    Joined: Sep 23, 2010
    Posts: 74


    thanks !
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Java assert vs dedicated Assert