Rule proposal: no-null
See original GitHub issueA 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 usingundefined
. Though the docs ofno-undefined
explain that the main reason for it’s existence is to avoid problems with declaring a custom variable namedundefined
- but really, wouldn’t it be better to have a rule for ensuring one never declares a variable namedundefined
? 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 typeLiteral
. I don’t see much reason for banning other literals besidesnull
though. So I think it doesn’t make sense to addno-restricted-literal
rule for which the only actual use would be to bannull
. - 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:
- Created 7 years ago
- Reactions:1
- Comments:6 (4 by maintainers)
Top GitHub Comments
Published a custom rule for this: https://github.com/nene/eslint-plugin-no-null
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"}]
).