DEV Community

Cover image for Code Smell 122 - Primitive Obsession
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

7 1

Code Smell 122 - Primitive Obsession

Objects are there for the picking. Even the smallest ones.

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

Problems

  • 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.

Solutions

  1. Create Small Objects

  2. Build missing abstractions using MAPPER

  3. Use Value-Objects.

Context

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

Wrong

//Samples borrowed with permission from 
//https://towardsdev.com/why-a-host-is-not-a-string-and-a-port-is-not-an-integer-595c182d817c

var port = 8080;

var in = open("example.org", port);
var uri = urifromPort("example.org", port);
var address = addressFromPort("example.org", port);
var path = pathFromPort("example.org", port);
Enter fullscreen mode Exit fullscreen mode

Right

//Samples borrowed with permission from 
//https://towardsdev.com/why-a-host-is-not-a-string-and-a-port-is-not-an-integer-595c182d817c

const server = Port.parse(this, "www.kivakit.org:8080");
//Port is a smallobject with responsibilities and protocol

let in = port.open(this);
const uri = port.asUri(this);
const address = port.asInetSocketAddress();
const path = port.path(this, "/index.html");
Enter fullscreen mode Exit fullscreen mode

Detection

[X] Manual

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

Tags

  • Primitive Obsession

Conclusion

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

Relations

More Info

Credits

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


This article is part of the CodeSmell Series.

AWS Q Developer image

Build your favorite retro game with Amazon Q Developer CLI in the Challenge & win a T-shirt!

Feeling nostalgic? Build Games Challenge is your chance to recreate your favorite retro arcade style game using Amazon Q Developer’s agentic coding experience in the command line interface, Q Developer CLI.

Participate Now

Top comments (0)

Feature flag article image

Create a feature flag in your IDE in 5 minutes with LaunchDarkly’s MCP server ⏰

How to create, evaluate, and modify flags from within your IDE or AI client using natural language with LaunchDarkly's new MCP server. Follow along with this tutorial for step by step instructions.

Read full post

👋 Kindness is contagious

Discover fresh viewpoints in this insightful post, supported by our vibrant DEV Community. Every developer’s experience matters—add your thoughts and help us grow together.

A simple “thank you” can uplift the author and spark new discussions—leave yours below!

On DEV, knowledge-sharing connects us and drives innovation. Found this useful? A quick note of appreciation makes a real impact.

Okay