09 July 2012

This time a short post about sending list parameters. I’ve been reading yesterday an interesting Kōan about sending list parameters and validating list parameters, and by a strange coincidence, later on I found a very nasty bug in my code related to lists.

I’ve seen uncountable times people checking the input parameter, a list one, for null or empty before considering that there is nothing to process. Just as many times I’ve seen people sending null for the list parameter if there was nothing to send.

Let’s dissect the call to a method with a list parameter.

public void CheckOut(List<Product> products)
    //..don't care for the implementation now

If the customer has 0 products in his cart, what will be passed in the products parameter? null? Your cart is empty, so how about sending an empty list of products to CheckOut. (Of course this might not be the best way, you should probably have a verification before, but also the CheckOut should verify that it receives any products.)

Of course null can be used in some cases as a different state of the parameter, not the list. For instance, the optional parameters might be setup to have default value null, which will mean that it has not been passed by the calling code. The bottom line is, there are empty lists, that you should send if you have no elements, and there is the null, that means.. null.

blog comments powered by Disqus