Primitive Obsession
When you smell complexity and lack of clarity in the air, look around, you’ll find your code swimming in a (smelly) soup of primitives (low level data-types, functions and language components). Unable to bare the stink, your code is screaming and screeching, asking you to rescue it.
This is my friend, primitive obsession, the stinkiest code smell. You can rescue your code (yes we can) by creating higher level abstractions (functions, data types, objects) and giving some sense to this anarchy.
Primitive Obsession is about lack of abstractions. In the OO world, Methods, Objects, Packages/Namespaces are ways of creating abstraction. Similarly functions, procedures, modules, etc are also valid ways of creating abstractions.
Adding more objects does not always lead to better abstraction. Sometimes removing objects is more useful.
There are many different refactorings that can be used as a remedies:
- Extract Class
- Replace Data Value with Object
- Replace Type Code with Class
- Introduce Parameter Object
- Replace Array with Object
One of my favorite example of Primitive Obsession (before and after).
Related posts:
- An Example of Primitive ObsessionCouple of years ago, I was working with a team which was building an application for handling User profile. It had the following functionality User Signup User Profile Login/Logout API...
- Biggest StinkersAt the SDTConf 2009, Corey Haines & I hosted a session called Biggest Stinkers. During this session we were trying to answer the following two (different) questions: As an experienced...
- Refactoring Teaser IV – Part 2Time to take the next baby step. Lets draw our attention to: public class IDTokens extends ChildStrategyParam { public IDTokens(final String token1, final String token2) { super(token1, token2, null);...
- Brett’s Refactoring Exercise Solution Take 1Recently Brett Schuchert from Object Mentor has started posting code snippets on his blog and inviting people to refactor it. Similar to the Daily Refactoring Teaser that I’m conducting at...
- Refactoring Teaser 1: Take 1Last week I posted a small code snippet for refactoring under the heading Refactoring Teaser. In this post I’ll try to show step by step how I would try to...




