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.

Rule proposal: no-null

See original GitHub issue

A rule to warn when null literal is used anywhere in the code.

Motivation for avoiding null

Because of historic accident JavaScript has two null-ish values: undefined and null. The language itself uses undefined for any uninitialized variable and it’s also the default return value of functions. Therefore one can argue that undefined is the natural null-value to use in JavaScript while null is the odd one.

The only places where one actually needs to use null is some API-s that require null. For example render() methods of React components must return null to render an empty component. In my experience there aren’t many such API-s though. One can explicitly disable the rule for the places where one really needs to use null.

This rule should fall in Best practices category.

Why is this rule a candidate for inclusion instead of creating a custom rule?

  • There already exists a companion rule no-undefined which enforces an opposite practice: never using undefined. Though the docs of no-undefined explain that the main reason for it’s existence is to avoid problems with declaring a custom variable named undefined - but really, wouldn’t it be better to have a rule for ensuring one never declares a variable named undefined? Anyway, ESLint should not pick sides, so it should support both pro-undefined and against-undefined camps.
  • There is no-restricted-syntax rule to allow one to easily disallow the use of any JavaScript syntax node. Unfortunately this doesn’t help with literals which all have node type Literal. I don’t see much reason for banning other literals besides null though. So I think it doesn’t make sense to add no-restricted-literal rule for which the only actual use would be to ban null.
  • It would be a trivial rule that would hardly require any maintenance. Creating a plugin for such a trivial thing feels a bit on the silly side.

Are you willing to create the rule yourself?

Yeah, already done.

PS. Perhaps somebody knows of a publicly available style guide that forbids null? Unfortunately I don’t know any besides the one I created for my own company.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
nenecommented, Sep 13, 2016

Published a custom rule for this: https://github.com/nene/eslint-plugin-no-null

2reactions
ajhyndmancommented, Apr 16, 2017

This is probably a bit late to be finding this conversation, but I think it’s a shame this didn’t make it into core.

In regards to public styleguides: the Typescript core team advises against the use of null: https://github.com/Microsoft/TypeScript/wiki/Coding-guidelines#null-and-undefined. In their expository notes, they cite Douglas Crockford’s 2014 talk in which he discusses null vs undefined: https://basarat.gitbooks.io/typescript/docs/tips/null.html

I would love to be able to write in a programming language where the majority of code I read didn’t have two quirk-filled null-ish values that need to be hacked around all the time (with rules like "eqeqeq": ["error", "always", {"null": "ignore"}]).

Read more comments on GitHub >

github_iconTop Results From Across the Web

SEC Proposed Rules
SEC Proposed Rules · Regulation NMS: Minimum Pricing Increments, Access Fees, and Transparency of Better Priced Orders · File No: S7-30-22 · Comments...
Read more >
Rule Formulas - Slate Knowledge Base - Technolutions
Rule Formulas allow you to use Query Exports within a Formula to set a Value. Formulas are supported with the following Rule Types:...
Read more >
ISO C3X proposal: nonnull qualifier
restrict has tricky rules about "based on" (6.7.3.1). >> >> Hmm, I think I can "base on" that, >> to define what I...
Read more >
ISO C3X proposal: nonnull qualifier - Clang Frontend
Hi all, I'd like to propose the following feature for ISO C (and also ISO C++) ... significant scrutiny on both committees to...
Read more >
`NOT NULL` constraints - GitLab Docs
For example, consider a migration that adds a new NOT NULL column active to table db_guides ... Propose functionality by submitting a feature...
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