DEV Community

Cover image for Code Smell 192 - Optional Attributes
Maxi Contieri
Maxi Contieri

Posted on • Originally published at maximilianocontieri.com

4

Code Smell 192 - Optional Attributes

You need to model something optional. Have you tried collections?

TL;DR: Collections are fantastic. And Polymorphic.

Problems

Solutions

  1. Change the optional attribute to a collection.

Context

If you need to model something that might be missing, some fancy languages will provide optional, nullable, and many other wrong solutions dealing with The Billion Dollar Mistake.

Empty collections and non-empty collections are polymorphic.

Sample Code

Wrong

class Person {
  constructor(name, email) {
    this.name = name;
    this.email = email;
  }

  email() {
    return this.email;
    // might be null    
  }

}

// We cannot use safely person.email()
// We need to check for null explicitly
Enter fullscreen mode Exit fullscreen mode

Right

class Person {
  constructor(name, emails) {
    this.name = name;
    this.emails = emails;
    // emails should allways be a collection. 
    // even an empty one
    // We can check it here
  }

  emails() {
    return this.emails;
  }

  // We can mutate the emails since they are not essential

  addEmail(email) {
    this.emails.push(email);
  }

  removeEmail(email) {
    const index = this.emails.indexOf(email);
    if (index !== -1) {
      this.emails.splice(index, 1);
    }
  }
}

// we can iterate the person.emails() 
// in a loop without checking for null 
Enter fullscreen mode Exit fullscreen mode

Detection

[X] Semi-Automatic

You can detect nullable attributes and change them when necessary.

Tags

  • Null

Conclusion

This is a generalization of the null object pattern.

Relations

More Info

Disclaimer

Code Smells are just my opinion.

Credits

Photo by Levi Jones on Unsplash


To iterate is human, to recurse divine

Peter Deutsch


This article is part of the CodeSmell Series.

Warp.dev image

Warp is the #1 coding agent.

Warp outperforms every other coding agent on the market, and gives you full control over which model you use. Get started now for free, or upgrade and unlock 2.5x AI credits on Warp's paid plans.

Download Warp

Top comments (0)

Dev Diairies image

User Feedback & The Pivot That Saved The Project

🔥 Check out Episode 3 of Dev Diairies, following a successful Hackathon project turned startup.

Watch full video 🎥

👋 Kindness is contagious

Dive into this thoughtful piece, beloved in the supportive DEV Community. Coders of every background are invited to share and elevate our collective know-how.

A sincere "thank you" can brighten someone's day—leave your appreciation below!

On DEV, sharing knowledge smooths our journey and tightens our community bonds. Enjoyed this? A quick thank you to the author is hugely appreciated.

Okay