When I started programming computers,

Vergelijkbare documenten
Free ebook: Tai Chi Chuan 18 ways of the Mind by Douwe Geluk Douwe Geluk is a Tai Chi teacher at Tai Chi Apeldoorn in the Netherlands

Eventful, lots of information, but there is a possibility to immediately request a quote from the homepage

Vadim Blaustein, a resident of Monaco and an experienced lawyer dealing with numerous real state transactions provides an insight into the residential market and property developments in Monaco.

Authentic flavors D E L I C I O U S D I M S U M D I S H E S A T H O M E.

Nasal Irrigation (Neti Pot)

Smart cylinder control, monitoring system. Alarm system. Price list: Sample 99 EUR 10pcs 71 EUR 100pcs 49 EUR 1000pcs 36 EUR

How to connect to the Telenet WiFree network with a laptop (or other EAP WiFi s). Written by Erwin Craps IT Helps bvba

DEROOVERS - THE STANDARD IN CUSTOM DESIGN

Understanding and being understood begins with speaking Dutch

ANGSTSTOORNISSEN EN HYPOCHONDRIE: DIAGNOSTIEK EN BEHANDELING (DUTCH EDITION) FROM BOHN STAFLEU VAN LOGHUM

MyDHL+ Van Non-Corporate naar Corporate

RECEPTEERKUNDE: PRODUCTZORG EN BEREIDING VAN GENEESMIDDELEN (DUTCH EDITION) FROM BOHN STAFLEU VAN LOGHUM

Puzzle. Fais ft. Afrojack Niveau 3a Song 6 Lesson A Worksheet. a Lees de omschrijvingen. Zet de Engelse woorden in de puzzel.

2019 SUNEXCHANGE USER GUIDE LAST UPDATED

Eric Bolle. I literary create my own world In conversation with Marcel Wesdorp in Plato s Cave.

Stars FILE 7 STARS BK 2

My Inspiration I got my inspiration from a lamp that I already had made 2 years ago. The lamp is the you can see on the right.

!!!! Wild!Peacock!Omslagdoek!! Vertaling!door!Eerlijke!Wol.!! Het!garen!voor!dit!patroon!is!te!verkrijgen!op! Benodigdheden:!!

voltooid tegenwoordige tijd

HANDBOEK HARTFALEN (DUTCH EDITION) FROM BOHN STAFLEU VAN LOGHUM

SAMPLE 11 = + 11 = + + Exploring Combinations of Ten + + = = + + = + = = + = = 11. Step Up. Step Ahead

Ius Commune Training Programme Amsterdam Masterclass 16 June 2016

Ius Commune Training Programme Amsterdam Masterclass 15 June 2018

WEGWIJZER VOOR METHODEN BIJ PROJECTMANAGEMENT (PROJECT MANAGEMENT) (DUTCH EDITION) BY EDWIN BAARDMAN, GERARD BAKKER, JAN VAN BEIJNHEM, FR

Een vrouw, een kind en azijn (Dutch Edition)

irregular verbs onregelmatige werkwoorden

Read this story in English. My personal story

Ius Commune Training Programme Amsterdam Masterclass 22 June 2017

Love & Like FILE 2 LOVE & LIKE BK 2

Grammatica uitleg voor de toets van Hoofdstuk 1

(1) De hoofdfunctie van ons gezelschap is het aanbieden van onderwijs. (2) Ons gezelschap is er om kunsteducatie te verbeteren

Buy Me FILE 5 BUY ME BK 2

B1 Woordkennis: Spelling

The Official Newsletter of Projects Abroad Bolivia

Tesland.com is THE webshop for: Tesla Model S, X en 3 accessories for interieur and exterieur.

Interaction Design for the Semantic Web

Add the standing fingers to get the tens and multiply the closed fingers to get the units.

Tai Chi Uithuizen Groningen: Tai Chi Chuan 'The book of Essence' an awesome book in English about Tai Chi Chuan, Tai Chi Tao, Qi Qong, Yoga, lesseons in Groningen the Netherlands and the background of Yvonne Huizinga. For sport fans and martial arts athletes a free must have!

Duurzaam projectmanagement - De nieuwe realiteit van de projectmanager (Dutch Edition)

Firewall van de Speedtouch 789wl volledig uitschakelen?

Topic 10-5 Meeting Children s Intellectual Needs

De grondbeginselen der Nederlandsche spelling / Regeling der spelling voor het woordenboek der Nederlandsche taal (Dutch Edition)

WEGWIJZER VOOR METHODEN BIJ PROJECTMANAGEMENT (PROJECT MANAGEMENT) (DUTCH EDITION) BY EDWIN BAARDMAN, GERARD BAKKER, JAN VAN BEIJNHEM, FR

Auctioning Frequencies for Broadcasting. Institute for Information Law

Activant Prophet 21. Prophet 21 Version 12.0 Upgrade Information

PRIVACYVERKLARING KLANT- EN LEVERANCIERSADMINISTRATIE

VERPLEEGKUNDIGE INTERVENTIES: VERTALING VAN NURSING INTERVENTIONS CLASSIFICATION (NIC) (DUTCH EDITION) FROM BOHN STAFLEU VAN LOGHUM

Handleiding Zuludesk Parent

Luister alsjeblieft naar een opname als je de vragen beantwoordt of speel de stukken zelf!

It s all about the money Trade Fair trade

Opgave 2 Geef een korte uitleg van elk van de volgende concepten: De Yield-to-Maturity of a coupon bond.

Meet your mentor and coach

1. In welk deel van de wereld ligt Nederland? 2. Wat betekent Nederland?

Group work to study a new subject.

Een vrouw, een kind en azijn (Dutch Edition)

Speaking and Language Learning. 3rd February 2016

Win a meet and greet with Adam Young from the band Owl City!

Het beheren van mijn Tungsten Network Portal account NL 1 Manage my Tungsten Network Portal account EN 14

Registratie- en activeringsproces voor de Factuurstatus Service NL 1 Registration and activation process for the Invoice Status Service EN 10

AlarmShield Interactive Security System Quickstart Guide. Model No. OPG2204

Duurzaam gedrag via subtiele beïnvloeding: De kracht van nudging 1 december 2017

Choices elementary GRAMMAR

Duiding Strafuitvoering (Larcier Duiding) (Dutch Edition) Click here if your download doesn"t start automatically

[BP-ebMS-H-000] Welke versie van Hermes moet er gebruikt worden?

Although eventually I became a writer I have struggled my entire life with reading and

Vergelijken in het Engels

Illustrator Tutorial - How to Create a Watch

Business Opening. Very formal, recipient has a special title that must be used in place of their name

Tentamen Objectgeorienteerd Programmeren

Grammatica overzicht Theme 5+6

LDA Topic Modeling. Informa5ekunde als hulpwetenschap. 9 maart 2015

General info on using shopping carts with Ingenico epayments

ook (niet)

Fall Detector Add-on Device Quickstart Guide

Vergaderen in het Engels

BISL EEN FRAMEWORK VOOR BUSINESS INFORMATIEMANAGEMENT (DUTCH LANGUAGE) (GERMAN EDITION) (DUTCH EDITION) BY REMKO VAN DER POLS, RALPH DONA

Vertaling Engels Gedicht / songteksten

Archief Voor Kerkelijke Geschiedenis, Inzonderheid Van Nederland, Volume 8... (Romanian Edition)

ZWEEDS LEREN - PARALLELLE TEKST EENVOUDIG LEZEN EENVOUDIG LUISTEREN: DE MEEST FANTASTISCHE ZWEEDSE AUDIOCURSUS 1 (DUTCH EDITION) FROM

Ik heb zo n angst. De laatste 12 uren heb ik met een koevoet geslapen, een fles naast mijn bed en de telefoon binnen handbereik.

Wi-Fi Range Extender Add-on Device Quickstart Guide

Buy Me! FILE 5 BUY ME KGT 2

MyDHL+ ProView activeren in MyDHL+

Settings for the C100BRS4 MAC Address Spoofing with cable Internet.

20 twenty. test. This is a list of things that you can find in a house. Circle the things that you can find in the tree house in the text.

Profile visitors NRC Q

Communication about Animal Welfare in Danish Agricultural Education

Understanding Behavior Alaska

Automatic Medication Dispenser Add-on Device Quickstart Guide

Over dit boek. Richtlijnen voor gebruik

Usage guidelines. About Google Book Search

Nederlands. Stap 1: Installatie. Stap 2: Uw Smartphone instellen

Comics FILE 4 COMICS BK 2

should(n t) / should(n t) have to zouden moeten / hadden meestergijs.nl

VDO Manipulation Key Error Codes - Cross Reference List

LDAP Server on Yeastar MyPBX & tiptel 31xx/32xx series

word order woordvolgorde

Medication Tracking Sensor Add-on Device Quickstart Guide

Transcriptie:

design Editor: Martin Fowler ThoughtWorks fowler@acm.org When to Make a Type Martin Fowler When I started programming computers, I began with fairly primitive languages, such as Fortran 4 and various early flavors of Basic. One of the first things you learn using such languages indeed, even using more up-to-date languages is which types your language supports. Being oriented toward number crunching, Fortran supported integer and real types, with the interesting rule that any variable whose name started with the letters I through N was an integer, and all other variables were floats. I m glad that convention hasn t caught on, although Perl is close. Furthermore, using object-oriented languages, you can define your own types and in the best languages, they act just as well as built-in ones. Defining types I first started playing with computers in my math classes, where we were all frustrated by the fact that these oh-so-numerate computers didn t understand fractions (and our math teachers took a dim view of floatingpoint approximations). I was thus delighted to learn that Smalltalk supported fractions naturally if you divided 1 by 3 you got a third, not some irritating long-running float. When people talk about design, they often don t talk about little objects such as fractions, presumably because many architects consider such details unworthy of their attention. However, defining such types often makes life easier. My favorite example is money. A lot of computer horsepower is dedicated to manipulating money, accounting, billing, trading, and so forth few things burn more cycles. Despite all this attention, no mainstream language has a built-in type for money. Such a type could reduce errors by being currency aware, helping us, for example, avoid embarrassing moments of adding our dollars to our yen. It can also avoid more insidious rounding errors. It would not only remove the temptation to use floats for money (never, ever do that) but also help us deal with tricky problems such as how to split $10 equally between three people. In addition, it could simplify a lot of printing and parsing code. For more on this (why write the column if I can t plug my books?), see Patterns of Enterprise Application Architecture (Addison-Wesley, 2002). The nice thing about OO programs is that you can easily define a type like this if the language and libraries don t include it. Other such low-level types I ve often written are ranges, because I m sick of writing if (x <= top && x >= bottom), quantities (to handle things such as 6 feet ), and dates (at least most languages include them now, but they are usually incorrect). Once you start writing these kinds of fundamental objects, you begin to ask yourself where to stop. For example, one person recently asked me whether he should make a type for currency, even though the only data was the international three-letter code? Another person asked about a person class with an age attribute and whether he should return an integer or define an age type and return that. 12 IEEE SOFTWARE Published by the IEEE Computer Society 0740-7459/03/$19.00 2003 IEEE

DESIGN When is it worth your while? 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. The attention to rounding in money offers a good example: rather than have every user of a number class remember to round, provide the methods in a money class so you can program the rounding once. Special types are handy when you have bits of data that often go together. A range object that brings together top and bottom values is a good example. You might argue that if a.includes(x) isn t much better than if(x >= min && x <= max), but using a range communicates that the two values fit together. Indeed, communication is one of the biggest reasons to use a type. If you have a method that expects to take a currency parameter, you can communicate this more clearly by having a currency type and using it in the method declaration. This is valuable even if you don t have static type checking, although such checking is yet another reason to make a type. Some types don t want to use their full capabilities. 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, such as using someone s personnel number in the arithmetic for their paycheck. That s a common thing to watch for. 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. Validation constraints are another factor to consider. We might want to throw an exception if someone sets my age to 1 or 300. Some languages have defined types purely on the basis of ranges. Currencies often will have a fixed list that can be defined in terms of enums or symbolic constants. The value of this depends on how widely people create the objects. If creating an age only occurs in the person class, then there s less value in making age a class than there would be if ages were created all over the code base. One of the interesting variations to consider is system evolution. Today, an integral age might make sense, but suppose in six months you need to deal with someone s age in years and months? Here you consider the effect of the type on refactoring to make the change. With your own type, changing the data structure is much easier, just add an extra field to the age type. If you return an integer, you have a more involved refactoring, but you can do it steadily in stages. First, keep the method that returns an integer, probably renaming it for clarity, and provide a new method that returns the age type. That way, older code doesn t have to change, but newer code can use the new capability. Then, look at all the uses of the integral age and, over time, alter them to use ages rather than integers. This could take a matter of minutes or months. Once you alter them all, you can remove the integral age method. On the whole, I m inclined to say that when in doubt, make a new type. It usually requires little effort but often provides surprising results. For a currency, even if only a three-letter string, I d make the type for communication reasons. For an age returned by a person, I d be more inclined to stick with the integer providing it wasn t widely used and everyone treated it as a number. Refactoring from one type to another isn t a huge deal particularly if you make the change as soon as you realize the type is going to be used widely. Martin Fowler is the chief scientist for ThoughtWorks, an Internet systems delivery and consulting company. Contact him at fowler@acm.org. How to Reach Us Writers For detailed information on submitting articles, write for our Editorial Guidelines (software@ computer.org) or access http://computer.org/ software/author.htm. Letters to the Editor Send letters to Editor, IEEE Software 10662 Los Vaqueros Circle Los Alamitos, CA 90720 software@computer.org Please provide an email address or daytime phone number with your letter. On the Web Access http://computer.org/software for information about IEEE Software. Subscribe Visit http://computer.org/subscribe. Subscription Change of Address Send change-of-address requests for magazine subscriptions to address.change@ieee.org. Be sure to specify IEEE Software. Membership Change of Address Send change-of-address requests for IEEE and Computer Society membership to member.services@ieee.org. Missing or Damaged Copies If you are missing an issue or you received a damaged copy, contact help@computer.org. Reprints of Articles For price information or to order reprints, send email to software@computer.org or fax +1 714 821 4010. Reprint Permission To obtain permission to reprint an article, contact William Hagen, IEEE Copyrights and Trademarks Manager, at whagen@ieee.org. January/February 2003 IEEE SOFTWARE 13