Skip to main content

Command Palette

Search for a command to run...

Code Smell 144 - Fungible Objects

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

Published
2 min read
Code Smell 144 - Fungible Objects
M

I’m a senior software engineer loving clean code, and declarative designs. S.O.L.I.D. and agile methodologies fan.

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.

fungible.drawio.png

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'));

Right

public class Person  { 
} 

shoppingQueueSystem.queue(new Person());
// The identity is irrelevant for queue simulation

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.

Code Smells

Part 1 of 50

In this series, we will see several symptoms and situations that make us doubt the quality of our developments. We will present possible solutions. Most are just clues. They are no hard rules.