Agile FAQs
  About   Slides   Home  

 
Managed Chaos
Naresh Jain’s Random Thoughts on Software Development and Adventure Sports
     
`
 
Amplify your Agility
Industrial Logic
Coaching | Training | Assessment | eLearning

What's New?
RSS Feed

Recent Thoughts
Tags
Recent Comments

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

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:

  1. An Example of Primitive Obsession
    Couple 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...
  2. Biggest Stinkers
    At 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...
  3. Refactoring Teaser IV – Part 2
    Time 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);...
  4. Brett’s Refactoring Exercise Solution Take 1
    Recently 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...
  5. Refactoring Teaser 1: Take 1
    Last 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...
blog comments powered by Disqus
    Licensed under
Creative Commons License