When To Make A Type
design
E d i t o r : M a r t i n F o w l e r s T h o u g h t W o r k s s f o w l e r @ a c m . o r g
When to Make a Type
Martin Fowler
hen I started programming com-
My favorite example is money. A lot of
Wputers, I began with fairly primitive computer horsepower is dedicated to manipu-
languages, such as Fortran 4 and
lating money, accounting, billing, trading, and
various early flavors of Basic. One
so forth—few things burn more cycles. Despite
of the first things you learn using
all this attention, no mainstream language has
such languages—indeed, even us-
a built-in type for money. Such a type could re-
ing more up-to-date languages—is which
duce errors by being currency aware, helping
types your language supports. Being oriented
us, for example, avoid embarrassing moments
toward number crunching, Fortran supported
of adding our dollars to our yen. It can also
integer and real types, with the in-
avoid more insidious rounding errors. It
teresting rule that any variable
would not only remove the temptation to use
whose name started with the let-
floats for money (never, ever do that) but also
ters I through N was an integer,
help us deal with tricky problems such as how
and all other variables were floats.
to split $10 equally between three people. In
I’m glad that convention hasn’t
addition, it could simplify a lot of printing and
caught on, although Perl is close.
parsing code. For more on this (why write the
Furthermore, using object-ori-
column if I can’t plug my books?), see Patterns
ented languages, you can define
of Enterprise Application Architecture (Addi-
your own types and in the best
son-Wesley, 2002).
languages, they act just as well as
The nice thing about OO programs is
built-in ones.
that you can easily define a type like this if
the language and libraries don’t include it.
Defining types
Other such low-level types I’ve often written
I first started playing with computers in
are ranges, because I’m sick of writing if
my math classes, where we were all frustrated
(x <= top && x >= bottom), quantities
by the fact that these oh-so-numerate com-
(to handle things such as “6 feet”), and
puters didn’t understand fractions (and our
dates (at least most languages include them
math teachers took a dim view of floating-
now, but they are usually incorrect).
point approximations). I was thus delighted
Once you start writing these kinds of fun-
to learn that Smalltalk supported fractions
damental objects, you begin to ask yourself
naturally—if you divided 1 by 3 you got a
where to stop. For example, one person re-
third, not some irritating long-running float.
cently asked me whether he should make a
When people talk about design, they of-
type for currency, even though the only data
ten don’t talk about little objects such as
was the international three-letter code? An-
fractions, presumably because many archi-
other person asked about a person class
tects consider such details unworthy of their
with an age attribute and whether he should
attention. However, defining such types of-
return an integer or define an age type and
ten makes life easier.
return that.
1 2
I E E E S O F T W A R E
P u b l i s h e d b y t h e I E E E C o m p u t e r S o c i e t y
0 7 4 0 - 7 4 5 9 / 0 3 / $ 1 9 . 0 0 © 2 0 0 3 I E E E
DESIGN
When is it worth your while?
stants. The value of this depends on
When should you make your own
how widely people create the objects.
type? To begin with, make a type if it
If creating an age only occurs in the
will have some special behavior in its
person class, then there’s less value in
operations that the base type doesn’t
making age a class than there would
How to
have. The attention to rounding in
be if ages were created all over the
money offers a good example: rather
code base.
Reach Us
than have every user of a number
One of the interesting variations to
class remember to round, provide the
consider is system evolution. Today,
methods in a money class so you can
an integral age might make sense, but
Writers
program the rounding once.
suppose in six months you need to
For detailed information on submitting articles,
Special types are handy when you
deal with someone’s age in years and
write for our Editorial Guidelines (software@
have bits of data that often go to-
months? Here you consider the effect
computer.org) or access http://computer.org/
gether. A range object that brings to-
of the type on refactoring to make the
software/author.htm.
gether top and bottom values is a
change. With your own type, chang-
good example. You might argue that
ing the data structure is much easier,
Letters to the Editor
if a.includes(x) isn’t much better
just add an extra field to the age type.
Send letters to
than if(x >= min && x <= max),
If you return an integer, you have a
Editor, IEEE Software
but using a range communicates that
more involved refactoring, but you
10662 Los Vaqueros Circle
the two values fit together. Indeed,
can do it steadily in stages. First, keep
Los Alamitos, CA 90720
communication is one of the biggest
the method that returns an integer,
software@computer.org
reasons to use a type. If you have a
probably renaming it for clarity, and
method that expects to take a cur-
provide a new method that returns the
Please provide an email address or
rency parameter, you can communi-
age type. That way, older code doesn’t
daytime phone number with your letter.
cate this more clearly by having a
have to change, but newer code can
currency type and using it in the
use the new capability. Then, look at
On the Web
method declaration. This is valuable
all the uses of the integral age and,
Access http://computer.org/software for
even if you don’t have static type
over time, alter them to use ages
information about IEEE Software.
checking, although such checking is
rather than integers. This could take a
yet another reason to make a type.
matter of minutes or months. Once
Subscribe
Some types don’t want to use their
you alter them all, you can remove the
Visit http://computer.org/subscribe.
full capabilities. Often you’ll find
integral age method.
things such as product codes that are
Subscription Change of Address
numeric in form. However, even
Send change-of-address requests for magazine
though they look like a number, they
subscriptions to address.change@ieee.org.
don’t behave like one. Nobody needs
Be sure to specify IEEE Software.
to do arithmetic on product codes—
with a special type you can avoid
On the whole, I’m inclined to say
that when in doubt, make a new
Membership Change of Address
bugs, such as using someone’s per-
type. It usually requires little ef-
Send change-of-address requests for IEEE
sonnel number in the arithmetic for
fort but often provides surprising re-
and Computer Society membership to
their paycheck.
sults. For a currency, even if only a
member.services@ieee.org.
That’s a common thing to watch
three-letter string, I’d make the type
for. Even if a currency code looks like
for communication reasons. For an
Missing or Damaged Copies
a string, if it doesn’t behave like one,
age returned by a person, I’d be more
If you are missing an issue or you received
it should get a different type. Look at
inclined to stick with the integer—
a damaged copy, contact help@computer.org.
the string’s interface and ask how
providing it wasn’t widely used and
much of it applies to a currency
everyone treated it as a number.
Reprints of Articles
code? If most of it doesn’t, then that’s
Refactoring from one type to another
For price information or to order reprints,
a good argument for a new type.
isn’t a huge deal—particularly if you
send email to software@computer.org or fax
Validation constraints are another
make the change as soon as you real-
+1 714 821 4010.
factor to consider. We might want to
ize the type is going to be used
throw an exception if someone sets
widely.
Reprint Permission
my age to –1 or 300. Some languages
To obtain permission to reprint an article,
have defined types purely on the ba-
contact William Hagen, IEEE Copyrights and
sis of ranges. Currencies often will
Martin Fowler is the chief scientist for ThoughtWorks, an
Trademarks Manager, at whagen@ieee.org.
have a fixed list that can be defined
Internet systems delivery and consulting company. Contact him
in terms of enums or symbolic con-
at fowler@acm.org.
J a n u a r y / F e b r u a r y 2 0 0 3
I E E E S O F T W A R E
1 3