JavaScript Nulls and Programming Trivia

JavaScript Nulls and Programming Trivia


Socrates as soon as mentioned “the unexamined life will not be value residing.” He was instantly sentenced to dying afterwards.

I, too, typically discover myself inspecting the trivialities of programming languages. Fortunately, I’ve not been put to dying for it (but).

After spending greater than a decade honing my Android growth abilities, I’ve not too long ago switched again to my first foray into skilled growth, JavaScript. It has lots to look at, and very similar to Socrates, I’ve many, many questions.

Right now, let’s have a look at a seemingly easy query: how do I signify absent information in perform returns?

Suppose I’ve bought a perform that queries the database for Foo:

async perform getFoo(id: string): Foo | ??? {
  const rows = await question(/* ...some SQL... */)
  if (rows.size > 0) return rows[0]
  else return ???
}

When Foo will not be discovered, what do I return?

For no matter cause, JavaScript supplies two choices right here: null and undefined. There are refined distinctions between them, however essentially they each signify the absence of information.

Which ought to I take advantage of? Let’s attempt to reply this query with out ingesting hemlock.

So What?

Earlier than we start, I’d prefer to reply a extra basic query: who cares?

On its face, this downside feels fairly dumb. null and undefined each imply roughly the identical factor, simply return no matter you’re feeling like, proper? It would not actually matter, so you possibly can leverage the vibes as an alternative of your mind.

These types of selections come up in all places: in languages, frameworks, APIs, and even your individual codebase. Conditions the place there’s a couple of approach to do one thing, but it surely’s not at all times clear which means is best as a result of the choices are so comparable.

Ought to I concatenate strings utilizing + or string templating? Ought to I take advantage of a for-loop or a forEach() iterator? Ought to I take advantage of tabs or areas?

Right here’s why I give a hoot:

First, whenever you research a seemingly arbitrary selection, you generally come out with some actual enlightenment. Maybe the selection appeared arbitrary at first, however by means of cautious research, you understand there are compelling causes to make use of one methodology or one other. For instance, I investigated properties vs. capabilities in Kotlin and got here out with a deeper understanding of val. My preliminary (naive) understanding equated val with “immutable” when it truly simply means “learn solely”, and all my future work with class properties was extra nuanced due to my analysis.

Second, the extra ambiguous a selection is, the longer individuals spend time on it, so it’s finest to cut back the anomaly. If it have been apparent what the precise reply was, everybody would do the precise factor and never spend any additional time interested by it. However when there’s no clearly right reply, it’s important to cease and assume. And when discussing ambiguous selections, oftimes all you’ve bought are private opinions, and opinion-based arguments are pointless and infinite (see: the endless dialogue of tabs vs. areas). If we are able to provide you with a well-reasoned nudge in a single course, we are able to spend much less time stressing about it going ahead.

Lastly, generally these questions simply drill their means into my head and I can’t get them out till I’ve a solution. That’s a private downside and applies on to the null vs. undefined dilemma (which, imagine it or not, I have been ruminating upon for years).

Alright, Let’s Debate

Let’s get again to the query at hand: coming to a conclusion on utilizing null vs. undefined (hopefully with out getting canceled on Hacker information for having imperfect ideas).

My first angle of inquiry: is it potential to write down a codebase that solely makes use of null or undefined? That will be awfully handy (which is why virtually each different programming language solely has one null!). Sadly, a fellow sophist already dug into this concept and got here up with the reply “no”, at the very least for many codebases. I’ll summarize the argument right here:

  • It’s simple to rule out a codebase that solely makes use of null as a result of it’s inconceivable to get away from undefined. It’s the worth that properties begin with earlier than initialization, and it’s returned by many core JavaScript capabilities (e.g. Array.discover()).

  • It’s potential to solely use undefined however solely in slim circumstances. For instance, TypeScript’s codebase has no nulls. Nonetheless, should you use JSON or rely on third social gathering libraries you open your self as much as the opportunity of utilizing nulls (since each can provide you nulls anytime).

It’s value pursuing an undefined-only codebase, however could also be inconceivable for you. For my very own use case, I should take care of nulls, so I have to scratch this utopian thought.

Can we reply this query by shifting our perspective to that of the buyer? How will they react to receiving a null vs. undefined?

The annoying factor about being a client is you gained’t at all times know should you’re getting again null or undefined, so it’s important to deal with each circumstances.

How can we if-check “not null and never undefined”? This is a bunch of how, most with drawbacks:

  • if (myVar !== undefined && myVar !== null) {} is just too verbose.
  • if (myVar) {} coerces additional values (“” and 0 are additionally false).
  • if (myVar != null) {} breaks the rule of thumb in opposition to utilizing !=
  • if (!isNil(myVar)) {} (by way of lodash) works!

Of all these choices, if (!isNil(myVar)) works finest. It’s succinct and sort inference nonetheless works. And should you’re allergic to dependencies, you possibly can write your individual isNil().

As a result of shoppers must be defensive, it doesn’t matter whether or not I return null or undefined. That mentioned, I discover it revolting that typeof null === ‘object’, so my choice is to return undefined. Plus, that selection helps out anybody who may need to ultimately attempt to go for an undefined-only codebase.

tl;dr

My private conclusion:

  • Characterize absent information with undefined.
  • Use isNil(xyz) for null checks.

I don’t really feel very hot and fuzzy about my conclusions, however I suppose that’s simply the way it goes whenever you’re working with JavaScript.

Screencap from the movie Chinatown, from the ending; a character is saying “Forget it, Jake. It’s JavaScript.”

author avatar
roosho Senior Engineer (Technical Services)
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog. 
rooshohttps://www.roosho.com
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog. 

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here


Latest Articles

author avatar
roosho Senior Engineer (Technical Services)
I am Rakib Raihan RooSho, Jack of all IT Trades. You got it right. Good for nothing. I try a lot of things and fail more than that. That's how I learn. Whenever I succeed, I note that in my cookbook. Eventually, that became my blog.