Wednesday 5 March 2008

Null object pattern

- Avoid using a null value to represent an error. An error is an error and it must be treated as such by throwing an exception.

- When returning a collection of objects, return an empty list if no results are obtained. The consumer method will not need to test if the returned collection is null or not. It will only need to verify that atleast one element is present in the collection to know that results were obtained.

- When returning results of a binary nature, initialize your binary variable to a default value. If you want to maintain 3 states (true, false, not set), use a class like the Nullable class of the .Net framework.

Remark: The use of any pattern depends on your problem context.