Type Scum 32

Posted by Michael Feathers Mon, 08 Sep 2008 22:43:00 GMT

Getting existing code under test is hard work, but it is fruitful. Yes, you get code that is easier to change, but more importantly, you get knowledge – you learn things about programming which make you better at avoiding common traps. Sadly, many of these traps aren’t well recognized yet.

The trap that I am going to write about today is one that I call type scum. It’s most prevalent in C and C++ but it can attack in any of the traditional statically typed languages.

Type Scum is the cruft in a code base which makes it impossible to compile a single file without an entire sub-stratum of defined types. I’m not talking about the primary, or even the secondary abstractions in your system, but rather the 200 or so basic types and structs that your abstractions depend upon.

Again, the problem is worst in C++ and C. At some point, every C or C++ developer feels the urge to isolate him or herself from the basic types of the language. The unsigned int type becomes uint32 and unsigned char * becomes uchar16_ptr. And, if that was all, it would be okay. But no, people define data transfer objects which aggregate these type pseudonyms together into large muddles. No file can compile without bringing in a world of types which cushion the code from dangerous things like the platform and testing harnesses.

No wait, testing harnesses are good. What can we do?

The unfortunate thing is that it is very hard to pull type scum out of a system once it’s been infected, but we can learn how to avoid it or at least manage it better:

  1. If you must provide a sub-stratum of basic types in your system, do it in one place. There should be a single library (and associated headers) that you include whenever you need it. This library (and headers) should contain nothing else.
  2. If you must create DTO (Data Transfer Object) types, minimize them. A good general purpose structure can carry a wide variety of different types of data and simplify testing.
  3. Push the DTOs to the edges. There are some systems where you really do care whether an internal computation happens in unsigned long int or unsigned long long int but they are rare. Basic data types and tolerances matter when two systems need to agree upon them, and that happens at component boundaries. In many systems, the internal code can use platform types directly.

There you go. Type scum bad.

I’m sure that some people reading this will say “Hey, isn’t this the exact opposite of the advice that people give for a system with the primitive obsession code smell?” The answer is “yes.” But, to me, primitive obsession is a different problem. It’s something which is the result of a lack of real behavioral abstractions in a system, not the lack of larger data holders.

Different problem.

Type scum bad.

Comments

Leave a response

  1. Avatar
    Phil about 2 hours later:

    You sound like you’ve been programming in C+, where lots of types get defined in a single file. Since C+ dependencies propagate on a file-by-file basis a sprinkling of typedefs and DTO definitions can cause a huge snarl of interdependencies.

    If that’s the case then your first suggestion (isolate the supra-primitive types to their own library) would solve the whole problem wouldn’t it? You’d still have to be very careful about changing that library of course, because the whole project would have to be rebuilt every time you touched it.

    My personal opinion is that it was a mistake to include polymorphic primitive types in programming languages in the first place. Then you wouldn’t have everyone rolling their own uint32’s all over the place. It would be more appropriate for people to roll their own in the rare cases where they actually want the meaning of “int” to be ambiguous.

  2. Avatar
    Antti Tuppurainen about 14 hours later:

    http://www.opengroup.org/onlinepubs/000095399/basedefs/stdint.h.html

    stdint.h is C99, will be in the next C++ standard (as cstdint), is supported by most C and C++ implementations (even if they don’t support C99) and for those that don’t, portable implementations are available from various sources; there are some floating around that can be found with a google search, there’s one in boost, etc etc.

    People should always be using stdint.h (or cstdint) when writing new C or C++ code.

  3. Avatar
    Anthony Williams about 15 hours later:

    I agree that a proliferation of “basic” types defined in random places can be a pain to deal with.

    My guideline is that if you need a type in two (or more) places it should be defined somewhere where it is accessible to those places without circular dependencies. This may often mean its own header file or module or unit or whatever. A library of basic types fits the bill nicely in this case. As Phil says, this solves most of the problems with “Type Scum”.

    A whole header just for the definition of uint32_t is probably overkill, but you need to use a bit of thought and avoid a single all-encompassing header for all types.

    It’s all about Coupling and Cohesion.

  4. Avatar
    Michael Feathers 1 day later:

    @Antti Yes, I remember hearing about that. Hopefully new projects will use it. In most existing projects, the madness has already set in.

    @Anthony I agree that consolidation can solve many of the problems, but I think the problem has a couple of faces. It is sort of like the use of what are called “way of life” frameworks in Java. The framework touches everything and it is hard to compile anything without linking to the framework. The Java community reacted with the notion of POJOs (Plain old Java objects). Although, I’ve seen the term POCO coined for C++, I haven’t seen much of a movement to champion their use.

    Some projects will always have that layer of pseudonyms for primitive types, but beyond that, I think that POCO-hood is a good thing.

  5. Avatar
    Sebastian Kübeck 1 day later:

    What can I do if third party includes force me to have lots of different definitions of primitive types around? Examples would be the Java Native Interface and OpenGL.

  6. Avatar
    Michael Feathers 1 day later:

    @Sebastian I would try to isolate myself as much as possible. Let those types play at the boundary. As soon as you step away from the boundary, use native types if you can. This doesn’t work for all systems, but I’m hard pressed to think of cases where it wouldn’t work for applications that use JNI and OpenGL.

  7. Avatar
    Josh Stone 1 day later:

    You didn’t mention the other property of type-scum—it shows up in all your code, because when you have distinct sets of type-scum, you have to convert them. So you end up casting left and right so you can get rid of all the compiler warnings.

  8. Avatar
    http://www.blacktowhiteiphone4.com over 2 years later:

    People love fashion will always love white iphone 4. Now you can get the advance chance to take the hottest iphone 4 conversion kit. Here we go!

  9. Avatar
    hublot replicas over 2 years later:

    contributed this reportSugarloaf Ski fake panerai Ski Accident Investigation: Winds to Blame?Skier Rebecca breitling crosswind racing Rebecca London Said Wind Unlikely Cause; Undaunted replica bvlgari watches Undaunted by Accident,

  10. Avatar
    accounting services over 2 years later:

    I agree that a proliferation of “basic” types defined in random places can be a pain to deal withaccounting services

  11. Avatar
    okey oyunu oyna over 3 years later:

    nice article… i love this site

    internette görüntülü olarak okey oyunu oyna, gerçek kisilerle tanis, turnuva heyecanini yasa.

  12. Avatar
    suntradetop over 3 years later:

    You do not keep anything for a special occasion.because every day that you live is a SPECIAL OCCASION

  13. Avatar
    beats by dr dre over 3 years later:

    Ski fake panerai Ski Accident Investigation: Winds to Blame?Skier Rebecca breitling crosswind beats by dr dre beats by dre saleracing Rebecca London Said Wind Unlikely Cause;

  14. Avatar
    chelsea shirt over 3 years later:

    chelsea shirt

  15. Avatar
    Puma espera shoes over 3 years later:

    Both styles were seen in the World Cup worn by Eusebio da Silva Ferreira and in the Olympics worn by Lee Evans and Thomas Smith. Rudolf passed in 1974 of lung cancer, and his son ran the company. In 1989, Armin and Gerd Dassler sold Puma to Cosa Liebermann for a unknown amount. Since Puma was seen on the soccer field many years ago it’s still very popular with the players and fans. Puma offers tons of styles to pick from and with the basic look they never go out of style. Puma will be around for a very very long time.

  16. Avatar
    robert.robot369@gmail.com over 3 years later:

    It was really very helpful info in this website and the nice services in this blog. Thank you very much for the nice info in this blog that to sharing the great info in those guys in this blog. Coroner Job Description|Medical Technologist Job Description|Corrections Officer Job Description|Financial Analyst Job Description

  17. Avatar
    how to become over 3 years later:

    I How To Become A Dj am very much happy for providing the nice How To Become A Famous Actor info is visible How To Become A Surgical Tech in this blog and using How To Become Air Traffic Controller the nice How To Become A Fire Fighter In California services How Do I Become Firefighter in this blog

  18. Avatar
    how to become over 3 years later:

    It is really How To Become A Manager amazing services in this How To Become A Professional Photographer blog How Many Years Does It Take To Become Pharmacist that How To Become A Astronaut to very much How To Become A Wrestler happy for using How To Become A Baseball Player the nice services

  19. Avatar
    how to become over 3 years later:

    How To Become A Respiratory Therapist in this blog. I was searching the different info is visible in this blog and sharing the great technology in this blog How To Become A Pilates Instructor Thanks a lot for visiting the nice info in this blog and using the amazing services in this How Many Years Does It Take To Become A Dentist blog

  20. Avatar
    ysbearing over 3 years later:

    Slewing ring is also called slewing bearing, some people called: rotary support, swing support. English Name: slewing bearing or slewing ring bearing or turn table bearing, slewing ring in the real industrial applications is very wide.

  21. Avatar
    BLOW STAR CASINO over 3 years later:

    I am very much enjoyed for the great services in this blog that to using the great technology in this blog. Thanks a lot for providing the great info is visible in this blog that to new info is visible in this blog.

  22. Avatar
    HIGH END CASINO SLOTS over 3 years later:

    Thanks a lot for providing the great services in this blog that to sharing the great services in this blog. I am very much satisfied by the great info is visible in this blog that to using the great technology.

  23. Avatar
    christian louboutin over 3 years later:

    Christian Louboutin Rolando Hidden-Platform Pumps Golden is a fashion statement – that’s sexy, it makes you look longer highlight, and it highlights the curves in the woman body and makes the body look more elegant and thinner without any diet.

    ?Brand: Christian Louboutin ?Material: Golden leather ?Specialty: Signature red sole ?Color: Golden ?Heel height: Approximately 130mm/ 5.2 inches high and a concealed 20mm/ 1 inch platform ?Condition: Brand New in box with dust bags & Original Box

    Fashion, delicate, luxurious Christian louboutins shoes on sale, one of its series is Christian Louboutin Rolando Pumps, is urbanism collocation. This Christian louboutins shoes design makes people new and refreshing. Red soles shoes is personality, your charm will be wonderful performance.

  24. Avatar
    backup iphone sms over 3 years later:

    Take effect to save the file and you can retrieve them when necessary.

  25. Avatar
    Office 2010 over 3 years later:

    This article is GREAT it can be EXCELLENT JOB and what a great tool!

  26. Avatar
    Franklin Marshall over 3 years later:

    Thanks a lot for providing the great services in this blog that to sharing the great services in this blog.

  27. Avatar
    ugg italia@hotmail.com over 4 years later:

    ugg italia - Benvenuti nel nostro negozio ugg Italia outlet. Miglior ugg stivali vendita, vari stivali UGG in vendita con il prezzo basso e alta qualità

  28. Avatar
    Franklin Marshall outlet@hotmail.com over 4 years later:

    Franklin Marshall outlet Welcome to Franklinmarshalloutlet.org, a leader in designer-inspired item online! We are proud to offer the widest selection of quality, fashionable item at prices you can afford. Whether you are looking for a special occasion clutch, a neutral everyday shoulder bag, or a bold statement piece,Franklinmarshalloutlet.org is sure to please.

  29. Avatar
    ugg saldi@hotmail.com over 4 years later:

    ugg saldi World War I the rise in Australia, Australian pilot with two pieces of sheepskin wrapped their feet warm, this UGG prototype, dating back nearly a hundred years of history

  30. Avatar
    Franklin Marshall Soldes@hotmail.com over 4 years later:

    WELCOME TO THE NEW Franklin Marshall SoldesWe are the Franklin Marshall outlet,we provide so many different style ,Franklin Marshall hoodies ,sweater,pants,jacket,T-shirt.and other accessories

  31. Avatar
    UGGs Outlet Rotterdam@hotmail.com over 4 years later:

    UGGs Outlet Rotterdam first saw her will be this ugly bulky appearance can not understand, and is a boots swept the Eurasian land, and now all over the world have whipped up a pop style, this gust of mainstreamEuropean and American stars are brought

  32. Avatar
    Abercrombie Online Shop@hotmail.com over 4 years later:

    Abercrombie Online Shop the first big American leisure, today’s young adults are among the most popular brand, but also one of the most IN brand American college students.

Comments