Return to site

JS TIP: R.isNil() checks null AND undefined

โš ๏ธ Be careful when replacing classic ===๐’๐’–๐’๐’ with Ramda APi ๐‘น.๐’Š๐’”๐‘ต๐’Š๐’() because it could mislead โ†ฉ๏ธ your business logic.

Imagine you declare an object but does not assign it.

๐’๐’†๐’• ๐’Ž๐’š๐‘ถ๐’“๐’…๐’†๐’“;

And later๐Ÿ•‘ in the code you set it to null when there is an errorโŒ:

onError(){
 myOrder=null
}

And somewhere๐ŸŽฏ in the code you check with ===๐ง๐ฎ๐ฅ๐ฅ, your order:

if(myOrder===null){
 dropOrder()
} else {
 //continue order processing
 myOrder = getOrderFromSomeService();
}

if you replace it with ๐‘น.๐’Š๐’”๐‘ต๐’Š๐’() of Ramda API, you will take the ๐’๐’–๐’๐’ values AND the ๐’–๐’๐’…๐’†๐’‡๐’Š๐’๐’†๐’… values.

Maybe you process the ๐’–๐’๐’…๐’†๐’‡๐’Š๐’๐’†๐’… later in the business implementation, in such case, the ๐‘น.๐’Š๐’”๐‘ต๐’Š๐’() will mislead๐Ÿ”ฅ your intent.

if(R.isNil(myOrder)){
 dropOrder()
} else {
 // will never happen for undefined myOrder ๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ
 myOrder = getOrderFromSomeService();
}

#javascript #programming #nullcheck #ramda

 

Section image