JavaAntiPatterns

Collection of bad coding practices

Unbuffered I/O

with 2 comments

Reading and writing I/O streams byte-by-byte is too expensive, as every read()/write() call refers to the underlying native (JNI) I/O subsystem. Usage of buffered streams reduces the number of native calls and improves I/O performance considerably.

Read the rest of this entry »

Advertisements

Written by Alex

November 22, 2007 at 2:41 pm

Posted in I/O

Tagged with

‘equals()’ does not check for null argument

with 7 comments

If you override equals() method in your class, always check if an argument is null. If a null value is passed, equals() must unconditionally return false (no NullPointerException should be thrown!).

Read the rest of this entry »

Written by Alex

November 22, 2007 at 2:08 pm

Posted in Objects

Tagged with , ,

‘compareTo()’ is incompatible with ‘equals()’

with one comment

If a class implements Comparable, compareTo() method must return zero if and only if equals() returns true for the same non-null argument (and vice versa). Violating this rule may cause unexpected behavior of the objects.

Read the rest of this entry »

Written by Alex

November 22, 2007 at 1:53 pm

Posted in Objects

Tagged with , ,

LinkedLists as arrays

leave a comment »

java.util.LinkedList is a special type of collection designed for sequential access (stacks and queues). Being used as a random-accesed array, it is much slower than other List implementations. For instance, getting an item by index (get(n)) has constant complexity O(1) for ArrayLists, while for LinkedList, the complexity of that operation is O(n).

Use ArrayList (or Vector if synchronization is required) for random-accessed lists.

Written by Alex

November 22, 2007 at 1:03 pm

Posted in Collections

Tagged with , ,

Using URLs in Collections

with one comment

java.net.URL objects used as Map keys or Set items can result in a big performance issue. Internal implementation of equals() and hashCode() methods of java.net.URL class performs domain name resolution, so the time of their execution relies on an internet connection speed. If a computer is off-line, the time is nearly equal to a connection timeout (tens of seconds).

The same problem arises every time when URLs equality checking or hash code computation is performed.

Do not use java.net.URL to keep URL values. Use java.net.URI objects or the simple Strings instead.

Read the rest of this entry »

Written by Alex

November 22, 2007 at 12:25 pm

Posted in Collections

Tagged with , , , , , ,

Accessing the Map values using keySet iterator

with 13 comments

A common mistake is to retrieve values from a Map while iterating over the Map keys with keySet(). Calling Map.get(key) for each entry is expensive and should be avoided for better performance.

Use entrySet() iterator to avoid the Map.get(key) lookup.

Read the rest of this entry »

Written by Alex

November 22, 2007 at 12:02 pm

Posted in Collections

Tagged with , , ,

Synchronized collections everywhere

with 2 comments

Vector and Hashtable are just the synchronized versions of the ArrayList an HashMap but working much slower. Use unsynchronized collections unless thread-safety is really required.

Read the rest of this entry »

Written by Alex

November 22, 2007 at 11:21 am

Posted in Collections

Tagged with , , ,