Intent to stop using null
in my JS code
#7
Replies: 55 comments 6 replies
-
Other resource I found about this topic :
|
Beta Was this translation helpful? Give feedback.
-
Something can be defined, but allowed to be null (see it as a "no value" state). It’s different than just undefined. Especially with TypeScript and you’re even mentioning it a bit in the last point.
I’m interested in your thoughts about this difference. Don’t you encounter cases when you can benefit from it? |
Beta Was this translation helpful? Give feedback.
-
Since
The reason I don't use |
Beta Was this translation helpful? Give feedback.
-
Don't know if you care about what other libraries do but React's ref objects for instance are declared as
Using |
Beta Was this translation helpful? Give feedback.
-
How do you reason about |
Beta Was this translation helpful? Give feedback.
-
Screw null and undefined, we should just switch to |
Beta Was this translation helpful? Give feedback.
-
I tend to use I have been using |
Beta Was this translation helpful? Give feedback.
-
A couple of thoughts:
It does for properties: Another point: Many of the modern JS APIs return I'm personally not super strict with not using |
Beta Was this translation helpful? Give feedback.
-
I quit using null more than an year ago and i never felt the need for it. |
Beta Was this translation helpful? Give feedback.
-
About this custom The main problem with currently For example, if the custom converter works like:
It will only works in js world. This is a problem when working principally in backend between a lot of services with different languages. To custom Ex:
Makes sense? |
Beta Was this translation helpful? Give feedback.
-
I'm curious why you're choosing to use Or is this less about semantics and more about pragmatism? |
Beta Was this translation helpful? Give feedback.
-
To me it looks more as a TypeScript problem that put you in that weird corner. In 99.999% of validation cases, treating both |
Beta Was this translation helpful? Give feedback.
-
I don’t think it matters what you use in your own code - what matters is that you treat null and undefined the same as input. Also, the cost of needing custom JSON tooling seems pretty understated. |
Beta Was this translation helpful? Give feedback.
-
I switched to use only undefined when it's possible about half a year ago. Never regret it. |
Beta Was this translation helpful? Give feedback.
-
I used Or ditch both |
Beta Was this translation helpful? Give feedback.
-
Hey Sindre, this is "trending" in hacker news in case you're wondering why a year old issue is suddenly getting attention. |
Beta Was this translation helpful? Give feedback.
-
I don't have a strong opinion, but there are a few arguments which don't seem to have been presented yet;
|
Beta Was this translation helpful? Give feedback.
-
Actually my opinion is exactly opposite: To stop using |
Beta Was this translation helpful? Give feedback.
-
Imagine you are writing a library and you expose an interface interface ISomething {
param?: string;
} Then this library is consumed from by another dev, who see, that param is an optional property so he decides to use interface ISomething0 {
param?: string
}
interface ISomething1 {
param: T | undefined;
}
interface ISomething2 {
param: T | null;
}
// Sometimes i see even:
interface ISomething3 {
param?: T | null | undefined;
} I would never use function do(param0, param1?, param2?) {
switch(arguments.length) {
case 1: {
// use only param0
},
case 2: {
// use param0 and param1
},
case 3: {
// use param 0, param 1 and param2
}
} A developer who is used to use |
Beta Was this translation helpful? Give feedback.
-
functions like |
Beta Was this translation helpful? Give feedback.
-
@Indomitable Your several examples here feel contrived and seem to indicate anti-patterns to me. First, regarding the library exporting the interface OtherThing {
a: number;
b?: number;
}
const getDoubled = (e: OtherThing) => {
if ('b' in e) {
return e.b * 2; // error here
}
return e.a * 2;
} Specifically, TS will highlight an error on the As a side note, in contexts not involving object literals directly, TS interfaces only specify the minimal subset of properties. Other keys you might not expect can be "in" an
In your next example you say there is a huge difference between
TS will complain about Regarding your third example about the variadic function breaking when Regarding your final example, you are correct, although again it feels very contrived to me. As a developer I need to be aware that the standard implementation of Now for the full disclosure of where I'm coming from here... I'm primarily a TypeScript developer right now. Perhaps about a year ago, I found this thread and the discussion around it, and my immediate reaction was "NO, I've remained subscribed to this issue since then, to see if anyone came along with more compelling arguments (in either direction), and it's been pretty amusing to see the sudden burst of activity. With the barrage of gut disagreements coming in, I wanted to chime in from the perspective of someone who really appreciated @sindresorhus bringing up this suggestion and has found it valuable in their own work. Thank you. |
Beta Was this translation helpful? Give feedback.
-
Looks like a classic example of "but this one weird case" that nobody really needs. Why would you need to find |
Beta Was this translation helpful? Give feedback.
-
First I think the high activity is due to the recent publication in "Hacker News".
I have problem with Typescript, because this shouldn't be allowed, in this case
I really don't like that @fregante This is just an example and in this case I search for the first "Falsy" value, not if there is value or not. By returning |
Beta Was this translation helpful? Give feedback.
-
Whichever nullish value you prefer, unless you can ensure that only one exists, you need account for both either way. The first solution that comes to mind is to use |
Beta Was this translation helpful? Give feedback.
-
I'm 100% on the same page with @sindresorhus -- I've opened up a feature request for the Angular code base to abandon Thanks and Best Regards, Nic |
Beta Was this translation helpful? Give feedback.
-
There's two aspects to this discussion tbh. One is what you do with your projects (thread title), which should be driven entirely by maintenance burden and user friction. The other is the more general discussion this thread has become about avoiding null everywhere. On the first topic, I'd say do you what find makes your projects the safest, most usable, and most maintainable. People who aren't in both sets of shoes (user & maintainer) can't really evaluate all those things well. On the second topic:
Uh, no, not "everyone" and "nobody". These aren't arguments to stop using either one, only observations that many[citation needed] js devs lack some important knowledge/experience to make a reasoned choice. |
Beta Was this translation helpful? Give feedback.
-
as far as I know, you can't clear a state from useState, in react, by doing setState(undefined) because it works the same way as just calling setState(). For obvious reasons, calling setState without data is ignored. avoiding null would break this feature in react. |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
In my work, we've clearly settled over time on the following regarding
We've had constructions where they were distinct and served different purposes, but we refactored that away again soon enough and used a meaningful Symbol to represent deletion in that situation instead. |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
I intend to stop using
null
in my code, in favor ofundefined
. There are many reasons for doing so.Feedback wanted
Tweet: https://twitter.com/sindresorhus/status/1114894287387258886
Background
http://2ality.com/2013/05/history-undefined.html
Reasons
null
andundefined
inconsistently and interchangeably, and few know when to use which.null
andundefined
complicates input validation.undefined
.typeof null === 'object'
.null
andundefined
.null
makes TypeScript types more verbose:type A = {foo?: string | null}
vstype A = {foo?: string}
.Problems
null
from JavaScriptTrue, but we can pretend it doesn't exist (in most cases). And we can enforce no
null
usage with linters.null
.That's fine. At least we can improve user-land code.
undefined
We just have to live with that. We could implement a custom JSON.parse/stringify that handles the conversion.
Resources
null
(I intend to enforce this when
typescript-eslint
ports this rule)Beta Was this translation helpful? Give feedback.
All reactions