Don’t listen to those opinionated purists that don’t like certain styles for some reason (probably because they didn’t have that operator when growing up and think it’s a hack)
Abuse of the nullish coalescing operator in JS/TS
21–30 of 70 posts
Re: Abuse of the nullish coalescing operator in JS/TS
#22In general, I agree. You don’t want silent failures. They’re awful and hard to reason about. > By doing this, you're opening up for the possibility of showing a UI where the name is "". Is that really a valid state for the UI? But as a user, if I get a white screen of death instead of your program saying “Hi, , you have 3 videos on your watchlist” I am going to flip out. Programmers know this so they do that so that…
This is a fine line. If you get a white screen of death you know something is wrong. If the first name is missing it may mean other things are missing and the app is in a bad state. That means the user could lose any work they try to do, which is a cardinal sin for an app to commit. Context matters a lot. If it's Spotify and my music won't play is a lot different than I filled a form to send my rent check and it sile…
Re: Abuse of the nullish coalescing operator in JS/TS
#23```ts user?.name ?? "" ```
The issue isn't the nullish coalescing, but trying to treat a `string | null` as a string and just giving it a non-sense value you hope you'll never use.
You could have the same issue with `if` or `user?.name!`.
Basically seems the issue is the `""`. Maybe it can be `null` and it should be `NA`, or maybe it shouldn't be `null` and should have been handled upstream.
Re: Abuse of the nullish coalescing operator in JS/TS
#24Is this a kink? You like being on call?
You think your performance review is going to be better because you fixed a fire over a holiday weekend or will it be worse when the post mortem said it crashed because you didn’t handle the event bus failing to return firstName
Re: Abuse of the nullish coalescing operator in JS/TS
#25Re: Abuse of the nullish coalescing operator in JS/TS
#26---
I disagree that erroring out when the app doesn't have ALL the data is the best course of action. I imagine it depends a bit on the domain, but for most apps I've worked on it's better to show someone partial or empty data than an error.
Often the decision of what to do when the data is missing is best left up the the display component. "Don't show this div if string is empty", or show placeholder value.
---
Flip side, when writing an under the hood function, it often doesn't matter if the data is empty or not.
If I'm aggregating a list of fooBars, do I care if fooBarDisplayName is empty or not? When I'm writing tests and building test-data, needing to add a value for each string field is cumbersome.
Sometimes a field really really matters and should throw (an empty string ID is bad), but those are the exception and not the rule.
Re: Abuse of the nullish coalescing operator in JS/TS
#27Is this a weakness in the type definition? If we're sure the value cannot be undefined, then why doesn't the type reflect that? Why not cast to a non-undefined type as soon as we're sure (and throw if it is not)? At least that would document our belief in the value state. I may not understand.
> Why not cast to a non-undefined type as soon as we're sure (and throw if it is not)
This is exactly what the OP author suggests.
Re: Abuse of the nullish coalescing operator in JS/TS
#28In general, I agree. You don’t want silent failures. They’re awful and hard to reason about. > By doing this, you're opening up for the possibility of showing a UI where the name is "". Is that really a valid state for the UI? But as a user, if I get a white screen of death instead of your program saying “Hi, , you have 3 videos on your watchlist” I am going to flip out. Programmers know this so they do that so that…
Re: Abuse of the nullish coalescing operator in JS/TS
#29Re: Abuse of the nullish coalescing operator in JS/TS
#30It seems Rust's unwrap is the exact opposite of ?? "". It throws an error instead of using a fallback value, which is exactly what the author suggests instead of using ?? "".
> In Rust, however, you're forced to reason about the "seriousness" of calling .unwrap() as it could terminate your program. In TS you're not faced with the same consequences.