0

Following is the code for lazy intialization of an instance member. This leaves me with question of when to perform parameter validation. In the code are 2 differenct functions performing NPE check one delays it, other does not. I chose the first option, but just curios to know whats the industry wide convention / best practice.

public class FreeMain {

    private List<Integer> fooList;

    FreeMain ( ) {  }

    /**
     * This code does a check null pointer kind of just in time.
     * 
     * @param barList
     */
    public void putListExceptionCheckDoneLater(List<Integer> barList) {
        if (this.fooList == null) {
            if (barList == null) throw new NullPointerException();
            this.fooList = barList;
        }
    }

    /**
     * In the even that fooList != null, 
     * throwing an exception would be of no benefit, 
     * since regardless of input, null or not, net effect would still be a no-op.
     * 
     * @param args
     */
    public void putListExceptionCheckDoneBefore(List<Integer> barList) {
        if (barList == null) throw new NullPointerException();
        if (this.fooList == null) {
            this.fooList = barList;
        }
    }

    public static void main(String[] args) {

    }
}

This code is custom designed to ask a specific doubt, so please refrain from questions like why not use constructor to pass list ? etc, or suggest code improvements which are not connected to this question.

JavaDeveloper
  • 5,320
  • 16
  • 79
  • 132
  • I would say perform parameter validation first. So, I'd go with `putListExceptionCheckDoneBefore`. Plus, I think it's more efficient (*save yourself a check*). – mre Jan 12 '14 at 01:55

1 Answers1

3

I can't see the point of deferring parameter validation, certainly for something as light-weight as checking for null. And it has some definite downsides:

  • (True) lazy validation makes your code more complicated.

  • (True) lazy validation is going to lead to validation errors popping up when it is too late to do anything about them.


Having said that, the logic of your example doesn't make much sense to me. The real difference between the two versions is that the first one doesn't check its argument at all if it isn't going to use it. This is NOT what "lazy" means. This is just doing things in a different order ... and giving different outcomes as a result.

(For what it is worth, I would prefer to check that barList is not null all of the time ... on the assumption that it is never meaningful for the parameter to be null. That way is likely to pick up bugs earlier.)


For the record, this is what true lazy validation might look like:

public class LazyValidationExample {

    private List<Integer> fooList;

    public void putListExceptionCheckDoneLater(List<Integer> barList) {
        this.fooList = barList;
    }

    public List<Integer> getList() {
        if (this.fooList == null) throw new NullPointerException();
        return this.fooList;
    }
    ...
}
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216