Refactoring 029 - Replace NULL With Collection

Table of contents

TL;DR: Replace nullable optional attributes with empty collections to eliminate null checks and leverage polymorphism.
Problems Addressed π
- Nulls reference exceptions
- Excessive conditional logic and IFs
- Fragile error handling
- Optional Attributes
- Complex validation code
- Polymorphism Violation
Related Code Smells π¨
Steps π£
- Identify nullable optional attributes that could be collections
- Replace single nullable objects with empty collections
- Remove all null checks related to these optional attributes
- Update methods to work with collections instead of single objects
- If you need to keep the old behavior, assert no more than element is added
Sample Code π»
Before π¨
public class ShoppingCart {
private List<Item> items = new ArrayList<>();
private Coupon coupon = null;
public void addItem(Item item) {
this.items.add(item);
}
public void redeemCoupon(Coupon coupon) {
this.coupon = coupon;
}
public double total() {
double total = 0;
for (Item item : this.items) {
total += item.getPrice();
}
// This a polluted IF and null check
if (this.coupon != null) {
total -= this.coupon.getDiscount();
}
return total;
}
public boolean isEmpty() {
// Explicit null check
return this.items.isEmpty() && this.coupon == null;
}
public boolean hasCoupon() {
return this.coupon != null;
}
}
After π
public class ShoppingCart {
private List<Item> items = new ArrayList<>();
// 1. Identify nullable optional attributes
// that could be collections
// 2. Replace single nullable objects with empty collections
private List<Coupon> coupons = new ArrayList<>();
public void addItem(Item item) {
// 5. If you need to keep the old behavior, assert no more than element is added
if (!this.items.isEmpty()) {
throw new IllegalStateException("Only one item can be added to the cart");
}
this.items.add(item);
}
// Step 4: Work with collection
// instead of single nullable object
public void redeemCoupon(Coupon coupon) {
this.coupons.add(coupon);
}
// Step 4: Simplified logic without null checks
public double total() {
double total = 0;
for (Item item : this.items) {
total += item.getPrice();
}
// 3. Remove all null checks
// related to these optional attributes
for (Coupon coupon : this.coupons) {
total -= coupon.getDiscount();
}
return total;
}
// Consistent behavior with empty collections
public boolean isEmpty() {
// 4. Update methods to work with collections
// instead of single objects
return this.items.isEmpty() && this.coupons.isEmpty();
}
// 3. Remove all null checks
// related to these optional attributes
// Collection-based check instead of null check
public boolean hasCoupon() {
return !this.coupons.isEmpty();
}
}
Type π
[X] Semi-Automatic
Safety π‘οΈ
This refactoring is generally safe when you control all access points to the collection attributes.
You need to ensure that no external code expects null values and deal with inside APIs.
The refactoring maintains the same external behavior while simplifying internal logic.
You should verify that all constructors and factory methods initialize collections properly.
Why is the Code Better? β¨
The refactored code eliminates null pointer exceptions and reduces conditional complexity.
Empty collections and non-empty collections behave polymorphically, allowing you to treat them uniformly.
The code becomes more predictable since collections always exist (at least empty) and respond to the same operations.
Method implementations become shorter and more focused on business logic rather than null handling.
The approach aligns with the principle of making illegal states unrepresentable in your domain model, leading to more robust and maintainable code.
Empty collections and non-empty collections are polymorphic.
How Does it Improve the Bijection? πΊοΈ
In the real world, containers exist even when empty.
By representing optional collections as empty collections rather than null, you create a more accurate model of reality.
Null does not exist in real world and it always breaks the bijection .
This maintains the one-to-one correspondence between real-world concepts and your computational model, creating a good MAPPER
When you return a collection instead of nulls, you also reduce the coupling.
Limitations β οΈ
This refactoring may not be suitable when null has semantic meaning different from "empty". Some legacy APIs might expect null values, requiring adaptation layers.
You need to ensure all code paths initialize collections consistently to avoid mixed null and empty states.
Refactor with AI π€
Suggested Prompt: 1. Identify nullable optional attributes that could be collections 2. Replace single nullable objects with empty collections 3. Remove all null checks related to these optional attributes 4. Update methods to work with collections instead of single objects 5. Test that empty and non-empty collections behave consistently
Without Proper Instructions | With Specific Instructions |
ChatGPT | ChatGPT |
Claude | Claude |
Perplexity | Perplexity |
Copilot | Copilot |
Gemini | Gemini |
DeepSeek | DeepSeek |
Meta AI | Meta AI |
Grok | Grok |
Qwen | Qwen |
Tags π·οΈ
- Null
Level π
[X] Intermediate
Related Refactorings π
See also π
Credits π
This article is part of the Refactoring Series.
Subscribe to my newsletter
Read articles from Maxi Contieri directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

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