DEV Community

Cover image for Code Smell 177 - Missing Small Objects
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

9

Code Smell 177 - Missing Small Objects

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

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

in the Wordle TDD Kata, 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

public class Person {
    private final String name; 

    public Person(String name) {
        this.name = name;
    }
}
Enter fullscreen mode Exit fullscreen mode

Right

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;
    }
}
Enter fullscreen mode Exit fullscreen mode

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 in choosing how and when to map something.

Relations

More Info

Disclaimer

Code Smells are just my opinion.

Credits

Photo by Shane Aldendorff on Unsplash


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


This article is part of the CodeSmell Series.

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (1)

Collapse
 
robertotonino profile image
Roberto Tonino

Very interesting! Where would you validate, for instance, Name? Directly in the constructor, with a validate() method, or else?

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay