Programmers use Null as different flags. It can hint an absence, an undefined value, en error etc.
Multiple semantics lead to coupling and errors.
TL;DR: Null is schizofrenic and does not exist in real world. His creator regreted and programmers around the world suffer it. Don't be a part of it.
Problems
Coupling among the callers and the senders.
Mismatch among the callers and the senders.
If/Switch/Case Polluting.
Null is not polymorphic with real objects. Hence, Null Pointer Exception
Null does not exist on real world. Thus, it violates Bijection Principle
Solutions
Avoid Null.
Use NullObject pattern to avoid ifs.
Use Optionals.
Null: The Billion dollar mistake
Maxi Contieri ・ Nov 18 '20
Exceptions
- APIs, Databases and external systems where NULL does exist.
Sample Code
Wrong
class CartItem{
constructor(price) {
this.price = price;
}
}
class DiscountCoupon {
constructor(rate){
this.rate = rate;
}
}
class Cart{
constructor(selecteditems, discountCoupon){
this.items = selecteditems;
this.discountCoupon = discountCoupon;
}
subtotal(){
return this.items.reduce