DEV Community

Cover image for Code Smell 144 - Fungible Objects
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

3

Code Smell 144 - Fungible Objects

We have heard a lot about NFTs. Now we master the Fungible concept

TL;DR: Respect the MAPPER. Make fungible what is Fungible in real-world and vice-versa.

Problems

Solutions

  1. Identify fungible elements on your domains

  2. Model them as interchangeable

Context

According to Wikipedia

Fungibility is the property of a good or a commodity whose individual units are essentially interchangeable and each of whose parts is indistinguishable from another part.

In software, we can replace fungible objects with others.

When mapping our objects with real ones, we sometimes forget about the partial model and build over design.

Image description

Sample Code

Wrong

public class Person implements Serializable {
    private final String firstName;
    private final String lastName;

    public Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
}

shoppingQueueSystem.queue(new Person('John', 'Doe'));
Enter fullscreen mode Exit fullscreen mode

Right

public class Person  { 
} 

shoppingQueueSystem.queue(new Person());
// The identity is irrelevant for queue simulation 
Enter fullscreen mode Exit fullscreen mode

Detection

[X] Manual

This is a semantic smell.

We need to understand the model to check whether it is right or not.

Tags

  • Over Design

Conclusion

Make fungible what is fungible and vice-versa.

Sounds easy but requires design skills and avoiding accidental complexity.

Credits

Photo by Andrey Metelev on Unsplash


People think that computer science is the art of geniuses but the actual reality is the opposite, just many people doing things that build on each other, like a wall of mini stones.

Donald Knuth


This article is part of the CodeSmell Series.

Gen AI apps are built with MongoDB Atlas

Gen AI apps are built with MongoDB Atlas

MongoDB Atlas is the developer-friendly database for building, scaling, and running gen AI & LLM apps—no separate vector DB needed. Enjoy native vector search, 115+ regions, and flexible document modeling. Build AI faster, all in one place.

Start Free

Top comments (1)

Collapse
 
gjaryczewski profile image
Gerard Jaryczewski

I have seen it a few times, and always feel something, but I didn't know what and why. Now I know - thank you.

MongoDB Atlas runs apps anywhere. Try it now.

MongoDB Atlas runs apps anywhere. Try it now.

MongoDB Atlas lets you build and run modern apps anywhere—across AWS, Azure, and Google Cloud. With availability in 115+ regions, deploy near users, meet compliance, and scale confidently worldwide.

Start Free

AWS Security LIVE! from AWS Partner Summit New York City

Join AWS Security LIVE! Streaming live from the AWS Partner Summit - New York City, July 15 8:00-9:00am ET and 10:15am-5:00pm ET where we talk all things Security!

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️