« Hangover | Main | U-Bahn »
Ich bin immer wieder fasziniert von solchen Beiträgen, wenn jemand kleine, scheinbar simple Wahrheiten so formuliert und kombiniert, dass sie etwas Neues ergeben: Martin Fowlers schon älterer Artikel zur Frage, wann man einen Typ/Klasse erstellen sollte, statt bei Primitiven oder allzu generischen Typen zu bleiben.
When should you make your own type? To begin with, make a type if
it will have some special behavior in its operations that the base type
doesn’t have.
...
Often you’ll find things such as product
codes that are numeric in form. However, even though they look like a
number, they don’t behave like one. Nobody needs to do arithmetic on
product codes— with a special type you can avoid bugs.
...
Even
if a currency code looks like a string, if it doesn’t behave like one,
it should get a different type. Look at the string’s interface and ask
how much of it applies to a currency code? If most of it doesn’t, then
that’s a good argument for a new type.
...
Indeed,
communication is one of the biggest reasons to use a type. If you have a
method that expects to take a cur- rency parameter, you can communi-
cate this more clearly by having a currency type and using it in the
method declaration.
Eine Übersicht aller Perlen von ihm gibts hier:Immer mal lesen!