question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Replace uses of `null` with `undefined`

See original GitHub issue

I call it my billion-dollar mistake. It was the invention of the null reference in 1965.

Sir Tony Hoare

JavaScript unfortunately has two billion-dollar mistakes: null and undefined.

While we can’t remove them from the language, we can reduce our usage down to a single type. I’d like to propose that we replace all uses of null with undefined.

The reason I suggest undefined is due to its natural existence through JavaScript and TypeScript. For example, check out this TypeScript function with an optional argument:

function useOptionalArg(optionalArg?: string) {
  console.log(optionalArg)
}

The use of the ? operator makes optionalArg have a type of string | undefined.

Additionally, using only undefined allows us to avoid awkward type situations like this one:

https://github.com/Developer-DAO/web3-ui/blob/f481a3670ec92a7653772b8ee5be3123a7641eb6/packages/hooks/src/hooks/useTokenBalance.ts#L13-L22

The type of provider on line 22 is ethers.providers.Web3Provider | null | undefined. This is due to the way provider is defined on Web3Context:

https://github.com/Developer-DAO/web3-ui/blob/f481a3670ec92a7653772b8ee5be3123a7641eb6/packages/hooks/src/Provider.tsx#L16

There’s no reason for it to also be typed as null when it’s already potentially undefined due to it being optional.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
with-heartcommented, Jan 16, 2022

That’s interesting that you lay that case out @etr2460 because I used to hold the same view. I was pushing for essentially the same idea at my last startup I worked for and ran into a few issues:

  • That concept is just not a part of the worldview of most devs, even among the more senior js devs that I worked with
  • The value of trying to add that concept to everyone’s worldview didn’t work very well, I think mostly because it wasn’t seen as very valuable
  • It’s not really enforceable in any way since the difference is based on intent in the mind of the author (no null is enforceable, see: unicorn/no-null by @sindresorhus)

We ended up moving everything to undefined and it basically didn’t matter in the slightest so I came to see it as being an idea that seemed interesting and useful, but in practicality it’s just not. imo there are better ways to handle the situation that makes it more obvious to the consumer than “know the distinction between null and undefined”.

I guess as an example, if getENSAddress was exported from some library and the user didn’t know about the distinction, would its types suggest to the user how to use it? Not really, they’d assume it means there’s no address and come asking how they could distinguish between loading and just not having one yet. If it was something like { isLoading: true; address: undefined } | { isLoading: false; address?: string }, it’d provide a lot more context out of the box (at least for TypeScript users).

0reactions
e-roycommented, Jul 15, 2022

closing because of massive structure change

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to replace undefined with null or treat it as false in ...
For keep object with null values, just remove delete and set a null value ... I started with changing undefined to null for...
Read more >
Replace a value if null or undefined in JavaScript?
To replace a value if null or undefined, you can use below syntax −var anyVariableName= null; var anyVar ...
Read more >
How to Replace a value if null or undefined in JavaScript?
Example 2: By using logical OR (||) operator. In this method, if the value is null or undefined, then it will simply replaced...
Read more >
converting `null` to `undefined` · Issue #2412 - GitHub
I'm trying to simplify how I handle default values. undefined is used for ES6 default parameters, React default props and applying defaults in ......
Read more >
Difference between null and undefined in JavaScript
A variable is undefined when you haven't assigned any value yet, not even a null. ... Generally, variables are undefined when you forgot...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found