I'm proposing this name for a pattern to UseTheStaticTyping
system of a language in method parameters and return values and class variables. This is, of course, not a new concept, but it seems to be a NamelessConcept
. Or maybe it's the same as Ward's WholeValuePattern
. Comments greatly appreciated. --GeorgeDinwiddie
Make the meaning of a value explicit by using a specific type rather than generic type.
Rather than use generic types, String, Long, Collection, as a method parameter or return value, use a specific type. For example, replace
Long findCustomerId(String name)
CustomerId findCustomerId(Name name)
This prevents the mixing up of different parameters and easily disambiguates overloaded method signatures.
The use of specific types also enables easy introduction of the NullObject
. When the information carried is small, the specific type might be implemented using the FlyweightPattern
(Joshua Bloch, "Effective Java", p. 105)
The most common argument against this has to do with memory usage/performance. I don't buy it, myself, so if anyone feels strongly about using the generic types directly on grounds of performance, please explain why so that I might understand better. -- JbRainsberger
This is a common HaskellLanguage
idiom; the compiler optimizes so no performance penalty is incurred. Depending on how you declare the type, you can make it an error to accidentally mix it with its "actual" type, e.g. adding 5 to a CustomerId?
would be a compile error.
related to: NullObject