Good programming practices

null

Replace null with Null Object

December 18th, 2011

Dealing with possible null pointers often require to use “if” statement. To help programmers in those situations, I wrote a utility class which returns some default object, if null will appear. Usage is very simple – just wrap a possible null with an appropriate method from this class and you can use a variable without fear of NullPointerException. This is example of Null Object design pattern. More…

Iterating over non null elements only

October 27th, 2011

Since Java 5 in this language exists very useful “foreach” statement. But if collection contains null elements, iterator will also return null. If code doesn’t check against null, this may cause a NullPointerException. So I made a simple class based on Decorator Design Pattern. It just wraps an Iterable or Iterator and return only non null values. More…