Code Smell 126 - Fake Null Object

Code Smell 126 - Fake Null Object

Null Objects are great alternatives to The Billion Dollar Mistake. Sometimes we don't need them

TL;DR: Don't abuse patterns. Even NullObject.

Problems

  • Empty Classes
  • Namespace Polluting
  • Duplicated Behavior

Solutions

  1. Create?Null Objects?instantiating real-object classes.

Context

Null Object pattern is a great alternative to?Nulls?and?IFs?(Both are code smells).

The structure of the pattern tells us to create a hierarchy.

This is not really necessary, we need real objects to be polymorphic to null objects.

Inheritance is not a proper way to achieve polymorphism.

A simple solution is to create a real object behaving like a null one.

For example: '0' is numbers' null object.

'' (or "") is String's null object

An empty collection is collection's null object.

Sample Code

Wrong

abstract class Address {
    public abstract String getCity();
    public abstract String getState();
    public abstract String getZipCode();
}

//Using inheritance for null objects is a mistake
//We should use interfaces (when available)
public class NullAddress extends Address {

    public NullAddress() { }

    public String getCity() {
        return Constants.EMPTY_STRING;
    }

    public String getState() {
        return Constants.EMPTY_STRING;
    }

    public String getZipCode() {
        return Constants.EMPTY_STRING;
    }

}

public class RealAddress extends Address {

    private String zipCode;
    private String city;
    private String state;

    public RealAddress(String city, String state, String zipCode) {
        this.city = city;
        this.state = state;
        this.zipCode = zipCode;
    }

    public String getZipCode() {
        return zipCode;
    }

    public String getCity() {
        return city;
    }

    public String getState() {
        return state;
    }

}        

Right

//There are just "addresses"
public class Address {

    private String zipCode;
    private String city;
    private String state;

    public Address(String city, String state, String zipCode) {
    //Looks anemic :(
        this.city = city;
        this.state = state;
        this.zipCode = zipCode;
    }

    public String zipCode() {
        return zipCode;
    }

    public String city() {
        return city;
    }

    public String state() {
        return state;
    }

}

Address nullAddress = new Address(Constants.EMPTY_STRING, Constants.EMPTY_STRING, Constants.EMPTY_STRING);
//we have our null object
//we should NOT assign it to a singleton, static or global
//It behaves like a null object. That's enough
//No premature optimizations
        

Detection

[X] Manual

This is a semantic smell.

Tags

  • Null

Conclusion

Creating Null Object classes is sometimes overdesign.

We need to create a real object.

This real object should never be?global,?singleton, or?static.

Too many smells to avoid.

Relations

Code Smell 12 - Null

Code Smell 32 - Singletons

Code Smell 114 - Empty Class

Code Smell 18 - Static Functions

Code Smell 17 - Global Functions

More Info

Credits

Photo by?Juan Davila?on?Unsplash

Thanks to Hernan Wilkinson for this idea on his course?Dise?o a la Gorra?(in Spanish)

All models are wrong but some models are useful

George Box

Software Engineering Great Quotes

This article is part of the CodeSmell Series.

How to Find the Stinky parts of your Code

要查看或添加评论,请登录

Maximiliano Contieri的更多文章

  • Refactoring 025 - Decompose Regular Expressions

    Refactoring 025 - Decompose Regular Expressions

    Make Regular Expressions Testable and Understandable TL;DR: You can break down a complex validation regex into smaller…

    1 条评论
  • Code Smell 295 - String Concatenation

    Code Smell 295 - String Concatenation

    Untangling the string mess in your code TL;DR: Avoid string concatenation for complex strings, use templates. Problems…

  • Code Smell 294 - Implicit Return

    Code Smell 294 - Implicit Return

    Your language adds clever features. Making YOU more obsolete TL;DR: Overusing implicit returns makes your code harder…

  • The Great Programmer Purge: How AI Is Taking Over the Tech Workforce

    The Great Programmer Purge: How AI Is Taking Over the Tech Workforce

    How AI is Redefining the Role of Programmers in the Tech Industry TL;DR: AI-generated code outperforms lazy…

    1 条评论
  • Refactoring 024 - Replace Global Variables with Dependency Injection

    Refactoring 024 - Replace Global Variables with Dependency Injection

    Break Hidden Dependencies for Cleaner Code TL;DR: Replace global variables with dependency injection to improve…

  • 10 More Simple Tips to Boost Your Productivity x2

    10 More Simple Tips to Boost Your Productivity x2

    The previous article on life hacks was a huge success. Let's start the year with more productivity tips! TL;DR: More…

  • Code Smell 293 - isTesting

    Code Smell 293 - isTesting

    Don’t let test code sneak into production TL;DR: Avoid adding isTesting or similar flags. Problems ?? Leaky abstraction…

  • Refactoring 001 - Remove Setters

    Refactoring 001 - Remove Setters

    Setters violate immutability and add accidental coupling TL;DR: Make your attributes private to favor mutability…

  • Code Smell 292 - Missing Return

    Code Smell 292 - Missing Return

    When your code loses its way TL;DR: Missing return statements cause unexpected behavior. Problems ?? Silent failures…

  • Code Smell 291 - Mutable Keys

    Code Smell 291 - Mutable Keys

    Changing Keys, Losing Values TL;DR: When you use mutable objects as keys in hashed collections, changing them breaks…

    2 条评论

社区洞察

其他会员也浏览了