Good programming practices


Concise Java code

April 22nd, 2012

Here is an example how concise Java code can be. I wrote 2 methods that do the same, but looks different. It checks if an Iterable object has only one object, which is additionally equals to the one passed as the second argument. Both methods use Objects class added in Java 7. The first method use “&&” operator, which evaluates expression on right only when the left one was true. More…

How to iterate a list with index

April 10th, 2012

There is many ways to iterate a list or other collection in Java. Sometimes it is useful to have also current index, especially when it is a List, where the elements order matters. It can be achieved be several ways. Lets look at the example: 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…