DEV Community 👩‍💻👨‍💻

Discussion on: Code Smell 145 - Short Circuit Hack

Collapse
mcsee profile image
Maxi Contieri Author • Edited on

Null is the worst code smell

And optional chaining, IMHO hids this antipattern.
I've written a smell on optional chaining. I will publish it in the following weeks

hackernoon.com/null-the-billion-do...

And optional chaining is another code smell

Collapse
larsejaas profile image
Lars Ejaas

But we are not always writing the data or code with null ourselves.

One example: Auto-generated types made with GraphQL Code Generator.
We still need to be able to handle null.
Null does indeed make sense if you do not know if data will return empty.

Thread Thread
mcsee profile image
Maxi Contieri Author

Of course
In case null is beyond your own control you need to deal with it

That is why it is the billion dollar mistake

We must be mature enough to avoid creating NEW nulls

Thread Thread
larsejaas profile image
Lars Ejaas

Yeah, that makes sense 😄

Collapse
lukeshiru profile image
Luke Shiru

While I agree with your point about null, that doesn't make optional chaining bad. Optional chaining also works with undefined, and is amazing. If we pretend that the type of functionDefinedOrNot is (() => void) | undefined, then instead of doing this:

if (functionDefinedOrNot !== undefined) {  
    functionDefinedOrNot();
}
Enter fullscreen mode Exit fullscreen mode

We can simply:

functionDefinedOrNot?.();
Enter fullscreen mode Exit fullscreen mode

Which at least from my point of view is far better.