Code Smell 252 - NullCustomer
You love Null Object Design Patter because it avoids the billion-dollar mistake.
TL;DR: Prefer real domain names to Implementation names
Problems
Bijection Fault
Naming
Solutions
Search for a real-world metaphor
Context
Naming is essential when designing software.
\
Using Pattern Names is a common software problem where programmers bind design patterns to real-world concepts.
\
You need to search for these abstractions in the real world and name them after their essential behavior instead of the accidental structure.
Null Customers don't exist in the same way NULL doesn't exist.
Sample Code
Wrong
import React from 'react';
const NullCustomer = () => {
return (
<div>
<h2>No customer found</h2>
<p>Sorry, we couldn't find any customer matching your criteria.</p>
</div>
);
};
const App = () => {
const customerDataAvailable = false;
return (
<div>
<h1>Customer Details</h1>
{customerDataAvailable ? (
<div>
{/* Render customer data */}
<h2>Customer Name: Cosmo Kramer</h2>
<p>Email: [email protected]</p>
<p>Phone: 123-456-7890</p>
</div>
) : (
<NullCustomer />
)}
</div>
);
};
export default App;
Right
import React from 'react';
// This is more closely related to real world
const InexistantCustomer = () => {
return (
<div>
<h2>Inexistant customer</h2>
<p>Sorry, we couldn't find any customer matching your criteria.</p>
</div>
);
};
const App = () => {
const customerDataAvailable = false;
return (
<div>
<h1>Customer Details</h1>
{customerDataAvailable ? (
<div>
{/* Customer exists */}
<h2>Customer Name: Newman</h2>
<p>Email: [email protected]</p>
<p>Phone: 666-666-6666</p>
</div>
) : (
<InexistantCustomer />
)}
</div>
);
};
export default App;
Detection
[x] Semi-Automatic
You can search for names including implementation patterns and check if they represent a real-world concept.
Tags
Naming
Level
[x] Beginner
AI Generation
AI generators are better at finding these names and don't abuse design patterns unless we prompt them.
AI Detection
When prompted to use better names with the wrong code tested AI suggested a few, but none detected by itself as a mistake.
Conclusion
Some concepts are harder to find than others.
\
Choose your names wisely.
Relations
Code Smell 25 - Pattern Abusers
More Info
Null: The Billion Dollar Mistake
Disclaimer
Code Smells are my opinion.
Credits
Photo by engin akyurt on Unsplash
We comfort ourselves with the belief that if the customers had just been happy with what they said they needed, the design would have been fine. It’s the customer’s fault for changing the requirements on us.
\
Robert Martin
Software Engineering Great Quotes
This article is part of the CodeSmell Series.
How to Find the Stinky Parts of your Code
Welcome to Billionaire Club Co LLC, your gateway to a brand-new social media experience! Sign up today and dive into over 10,000 fresh daily articles and videos curated just for your enjoyment. Enjoy the ad free experience, unlimited content interactions, and get that coveted blue check verification—all for just $1 a month!
Account Frozen
Your account is frozen. You can still view content but cannot interact with it.
Please go to your settings to update your account status.
Open Profile Settings