Refactoring 029 โ How to replace Null with the group

Transforming optional features into empty groups of cleaner, safety, and multiple shapes, which leads to the removal of the error by one billion dollars
TL; DR: Replace optional, null and empty groups to get rid of empty tests and take advantage of multiple shapes.
The problems that were taken 
Steps 
- Determine the false optional features that can be groups
- Replacing one of the empty groups with empty groups
- Remove all empty tests related to these optional features
- Update ways of working with groups instead of individual organisms
Code 
Before 
public class ShoppingCart {
private List- 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 hasUnsavedChanges() {
// Explicit null check
return !this.items.isEmpty() || this.coupon != null;
}
public boolean hasCoupon() {
return this.coupon != null;
}
}
public class ShoppingCart {
private final List- items = new ArrayList<>();
// This version uses Optionals
// Not all programming languages support this feature
private Optional
coupon = Optional.empty();
public void addItem(Item item) {
items.add(item);
}
public void redeemCoupon(Coupon coupon) {
// You need to understand how optionals work
this.coupon = Optional.ofNullable(coupon);
}
public boolean hasUnsavedChanges() {
return !items.isEmpty() || !coupon.isPresent();
}
public boolean hasCoupon() {
return coupon.isPresent();
}
}
After 
public class ShoppingCart {
private List- items = new ArrayList<>();
// 1. Identify nullable optional attributes
// that could be collections
// 2. Replace single nullable objects with empty collections
private List
coupons = new ArrayList<>();
public void addItem(Item item) {
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 hasUnsavedChanges() {
// 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();
}
}
Write 
Safety 
This is generally safe to restore the intention when controlling all points of access to assembly features.
You need to make sure that there is no external symbol expecting empty values โโand dealing with internal application programming facades.
Research with the same external behavior with simplification of internal logic.
You must check that all the creators and factory methods properly create groups.
Why is the symbol better? 
The code, which has been re -paved, removes an empty indicator and reduces conditional complexity.
Empty groups and non -empty combinations act, allowing you to treat them uniformly.
The symbol becomes more predictable because the groups are always present (at least empty) and respond to the same processes.
Method applications become shorter and more focused on business logic instead of dealing with the difference.
The approach is in line with the principle of making illegal countries unresolved in your field model, which leads to a more powerful and maintenance symbol.
Empty groups and non -empty groups Multi -shape.
How does the objection improve? 
In the real world, containers are found even when they are empty.
By representing optional groups as empty groups instead of empty, you can create a more accurate model of reality.
Null is not present in the real world, and it always breaks the objection.
This maintains individual correspondence between realistic concepts and your arithmetic model, creating a good map.
When returning a set instead of the leopards, you can also reduce the conjugation.
restrictions 
This re -expulsion may not be appropriate when null has a semantic meaning that differs from โemptyโ. Some old application programming facades may expect empty values, which require adaptation layers.
You need to ensure that all the software instructions are constantly creating groups to avoid empty and empty situations.
Refactor with Amnesty International 
Suggested Complex: 1. Determine the invalid optional features that can be groups 2. Replace one ingredient creatures with empty groups 3. Remove all empty tests related to these optional features 4.
Level 
See also 
Credit 
Image by EAK K. on Pixabay
This article is part of the re -sale chain.