Asdfasf

Sunday, September 07, 2014

EJ-41 Use Overloading Judiciously

Ref: Effective Java by Joshua Bloch



A safe conservative policy is never to export two overladings with the same number of parameters.
For example, consider the class ObjectOutputStream. It has a variant of its write method for every primitive type and for several reference types. Rather than overloading the write method, these variants have signature like writeBoolean(boolean), writeInt(int), writeLong(long) etc. An added benefit of this naming pattern, when compared to overloading, is that it is possible to provided read methods with correspoing names, for example readBoolean(), readInt().

For constructors, you don't have the option of using different names: multiple constructors for a class are always overloaded. You do, in many cases,have the option of exporting static factories instead of constructors.

No comments: