Good programming practices

design pattern

StructuredInfo – objects instead of plain String

December 29th, 2011

Common method toString is a simple way to display inner values of an object. But when you get a String want to present it in other format it can be difficult or unsafe to parse it. So better way is to pass a structured object witch can be next serialized to desired format. So I created a project in Scala 2.9.1 for gathering and serializing information in an application. More…

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…