Code Smell 122 - Primitive Obsession
Tags: oop, webdev, tutorial, beginners
Published: 2022-03-17 03:11:38

*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](https://dev.to/mcsee/fail-fast-48dm) principle violation.

- [Bijection Fault](https://dev.to/mcsee/the-one-and-only-software-design-principle-3086)

- 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](https://dev.to/mcsee/what-is-wrong-with-software-5pa)

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

[Gist Url]: # (https://gist.github.com/mcsee/12dc64f150fb047541b9aaa795d85faf)
```javascript
//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);
```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/32202947a8d5ef760936a1e094d3f6b3)
```javascript
//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");
```

# 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

{% post https://dev.to/mcsee/code-smell-121-string-validations-38ph %}

{% post https://dev.to/mcsee/code-smell-04-string-abusers-1nma %}

# More Info

- [Why a Host is not a String and a Port is not an Integer](https://towardsdev.com/why-a-host-is-not-a-string-and-a-port-is-not-an-integer-595c182d817c)

- [Primitive Obsession — A Code Smell that Hurts People the Most](https://medium.com/the-sixt-india-blog/primitive-obsession-code-smell-that-hurt-people-the-most-5cbdd70496e9)

- [Refactoring Guru](https://refactoring.guru/es/smells/primitive-obsession)

# Credits

Photo by [K. Mitch Hodge](https://unsplash.com/@kmitchhodge) on [Unsplash](https://unsplash.com/s/photos/prehistoric)
  
* * *

> 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_
 
{% post https://dev.to/mcsee/software-engineering-great-quotes-26ci %}

* * *

This article is part of the CodeSmell Series.

{% post https://dev.to/mcsee/how-to-find-the-stinky-parts-of-your-code-1dbc %}