Refactoring

Primitive Obsession

jamesdabbs

[Live]

Kent Beck

Martin Fowler

@martinfowler

@KentBeck

Sandi Metz

Katrina Owen

@kytrinyx

@sandimetz

Refactoring

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

ri-fak-tər-iŋ

(n)

How

But

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

Recipes

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!

Takeaways

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

Practice

We're hiring

Write the simplest thing that you need today;

you can always change it tomorrow

Refactoring Live: Primitive Obsession

By James Dabbs

Refactoring Live: Primitive Obsession

Let's roll up our sleeves and clean up some smelly code. In this session, we'll dig in to Primitive Obsession - what happens when our domain logic is all wrapped up in primitive data types? And most importantly, how do we untangle it? Code presented in this talk is available at https://github.com/jamesdabbs/greatbook.

  • 2,223