Code Smell 122 — Primitive Obsession

TL;DR: Use small objects instead of primitive ones.


  • Code Duplication
  • Small Objects Missing
  • Fail Fast principle violation.
  • Bijection Fault
  • Subset violations: Emails are a subset of strings, Valid Ages are a subset of Real, Ports are a subset of Integers, etc.
  • We spread Logic and Behavior in many places.
  • Premature Optimization.


  1. Create Small Objects
  2. Build missing abstractions using MAPPER
  3. Use Value-Objects.


We are very lazy to create small objects.

We are also lazy to separate What and How

We like very much to understand the internals of how things work.

We need to start thinking in a whitebox way and looking at the protocol and behavior of small components.

Sample Code


//Samples borrowed with permission from 
var port = 8080;var in = open("", port);
var uri = urifromPort("", port);
var address = addressFromPort("", port);
var path = pathFromPort("", port);


//Samples borrowed with permission from 
const server = Port.parse(this, "");
//Port is a smallobject with responsibilities and protocol
let in =;
const uri = port.asUri(this);
const address = port.asInetSocketAddress();
const path = port.path(this, "/index.html");


[X] Manual

We can automate checks on constructors for small objects missing opportunities.


  • Primitive Obsession


We need to transform our strings, numbers, and arrays into small objects


More Info


Photo by K. Mitch Hodge on Unsplash

Iteration allows us to progressively approach some goal. We can discard the steps that take us further away and prefer the steps that move us nearer. This is in essence how evolution works. It is also at the heart of how modern machine learning (ML) works.

Dave Farley



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Maximiliano Contieri

Maximiliano Contieri

I’m a senior software engineer specialized in declarative designs. S.O.L.I.D. and agile methodologies fan.