You need to model something optional. Have you tried collections?
TL;DR: Collections are fantastic. And Polymorphic.
Problems
Solutions
- 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
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
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
Code Smell 12 - Null
Maxi Contieri βββ γ» Oct 31 '20 γ» 2 min read
Code Smell 149 - Optional Chaining
Maxi Contieri βββ γ» Jul 16 '22 γ» 2 min read
Code Smell 19 β Optional Arguments
Maxi Contieri βββ γ» Nov 7 '20 γ» 2 min read
More Info
Null: The Billion dollar mistake
Maxi Contieri βββ γ» Nov 18 '20 γ» 6 min read
Disclaimer
Code Smells are just my opinion.
Credits
Photo by Levi Jones on Unsplash
To iterate is human, to recurse divine
Peter Deutsch
Software Engineering Great Quotes
Maxi Contieri βββ γ» Dec 28 '20 γ» 13 min read
This article is part of the CodeSmell Series.

Top comments (0)