Code Smell 177 - Missing Small Objects
Tags: webdev, javascript, beginners, programming
Published: 2022-11-05 13:42:54

*We see small primitive data everywhere*

> TL;DR: Don't forget to model the smallest ones

# Problems

- Primitive obsession

# Solutions

1. find responsibilities for small objects in the [MAPPER](https://dev.to/mcsee/what-is-wrong-with-software-5pa)

2. Reify them

# Context

Since computing early days we map all we see to the familiar primitive data types: Strings, Integers, Collections, etc.

Mapping to dates violates abstraction and [fail-fast](https://dev.to/mcsee/fail-fast-48dm) principles.

in the [Wordle TDD Kata](https://dev.to/mcsee/how-to-create-a-wordle-with-tdd-in-javascript-10ec), we describe a Wordle word to be different than a *String* or *Char(5)*, since they don't have the same responsibilities.

# Sample Code

## Wrong

[Gist Url]: # (https://gist.github.com/mcsee/9a4cce1354fa6b13bdf90b19e453fbb8)
```java
public class Person {
    private final String name; 

    public Person(String name) {
        this.name = name;
    }
}
```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/79e4e83a5f5f2a17f310a4ae743221b4)
```java
public class Name {
    private final String name; 

    public Name(String name) {
        this.name = name;
        // Name has its own creation rules, comparison etc.
        // Might be different than a string
    }
}
  
public class Person {
    private final Name name; 

    public Person(Name name) {
        // name is created as a valid one,
        // we don't need to add validations here 
        this.name = name;
    }
}
```

# Detection

[X] Manual

This is a semantic smell. It is related to design activity

# Exceptions

In a very small number of mission-critical systems, we have a tradeoff from abstraction to performance.

This is not the usual case. We do premature optimization not relying on a modern computer and virtual machine optimizations.

As always, we need to stick to evidence in real-world scenarios.

# Tags

- Primitive

# Conclusion

Finding small objects is a very hard task requiring experience to make a good job and avoid overdesign.

There's [no silver bullet](https://dev.to/mcsee/coupling-the-one-and-only-software-design-problem-2pd7) in choosing how and when to map something.

# Relations

{% post https://dev.to/mcsee/code-smell-122-primitive-obsession-4a55 %}

# More Info

{% post https://dev.to/mcsee/fail-fast-48dm %}

{% post https://dev.to/mcsee/how-to-create-a-wordle-with-tdd-in-javascript-10ec %}

# Disclaimer

Code Smells are just my [opinion](https://dev.to/mcsee/i-wrote-more-than-90-articles-on-2021-here-is-what-i-learned-1n3a).

# Credits

Photo by [Shane Aldendorff](https://unsplash.com/@pluyar) on [Unsplash](https://unsplash.com/s/photos/magnifying-glass)
  
* * *

> The secret to building large apps is never build large apps. Break your applications into small pieces. Then, assemble those testable, bite-sized pieces into your big application.

_Justin Meyer_
 
{% 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 %}