Primitive Obsession



Kent Beck

Martin Fowler



Sandi Metz

Katrina Owen




a change made to the internal structure of software

to make it easier to understand and cheaper to modify

without changing its observable behavior.

— Martin Fowler,  Refactoring





If it ain't broke,

don't fix it.

Is it open for the new requirement?

Is it really open for the new requirement?


Is it really open for the new requirement?

Do you know how to make it open?


Is it really open for the new requirement?

Pick the code smell closest to the requirement

Apply a curative recipe for that code smell


Primitive Obsession

using primitive types to represent business objects

Primitive Obsession

Primitive Obsession


Replace Primitive with Object

  • Create a simple value class with a getter
  • Run static checks
  • Change the setting code to create an instance of the value class
  • Run tests
  • Consider applying "change reference to value" or "change value to reference"

Replace Conditional with Polymorphism

  • Create classes for polymorphic behavior
  • Create a factory function to return the correct instance
  • Use the factory function in calling code
  • Move the conditional to the parent class
  • Override each successive leg of the conditional
  • Replace the parent class conditional

Good luck!


Now Easy

  • getting starting without knowing the end state
  • stopping and shipping before reaching the end state

Important, Still Hard

  • running tests quickly
  • trusting your coverage
  • working with QAs
  • refactoring Rails magic methods
  • finding all callers of a method

Running Tests Quickly

  • set up guard and spring
  • small objects
  • decoupled from ActiveRecord

Trusting Your Coverage

  • test more
  • consider in-production tests
  • science experiments

Trusting Your Coverage

Working with QAs

  • talk about regression tests
  • talk about development practices

Refactoring Rails Magic

  • decouple where you can
  • use magic when it makes sense

Finding All Callers

  • use descriptive names
  • don't get fancy with dynamic send
  • consider deprecating, not deleting

"We can't take the time to refactor,

we have features we have to ship."

If you don't need to change your product

don't change your code

If you need to change your code

make it easy to change

Read These


We're hiring

Write the simplest thing that you need today;

you can always change it tomorrow

Made with