DEV Community

Discussion on: Unhealthy Code: Null Checks Everywhere!

Collapse
 
kopseng profile image
Carl-Erik Kopseng • Edited

"makes the code leaking to any executing".

What?

"instead of making guards using null, you propose to create useless empty array and objects"

No. Instead of making 10 null checks, you do 1.This pattern never makes sense if you just want to avoid a single null check. But when you do the same checking at multiple levels, this vastly simplifies logic at times.

Collapse
 
dimpiax profile image
Dmytro Pylypenko

Review your comment in 10 years.

Thread Thread
 
kopseng profile image
Carl-Erik Kopseng

Immutable code where you can drop all null checking is fantastic to work with. And I have been using the Null object (/DefaultObject pattern) for a decade in various settings. Still does wonders for avoiding nasty NPEs in Java and works quite well in JS as well, though I am pragmatic: I do not create a full 1:1 mapping for every class. Just the ones where it makes sense.

Thread Thread
 
dimpiax profile image
Dmytro Pylypenko

Hi, the talk in my comments is not about mutability , but about clean and not overused code with default values where execution must be terminated, but not being executed with empty values.