Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid null deferences #1610

Closed
wants to merge 3 commits into from
Closed

fix: avoid null deferences #1610

wants to merge 3 commits into from

Conversation

paulschreiber
Copy link
Member

Description

Avoid some null deferences that pop up in debugging.

@@ -63,7 +63,7 @@ export const formatPercent = (value: number) => {
};

export const formatCoordinate = (value: number) => {
return value.toLocaleString(undefined, {
return value?.toLocaleString(undefined, {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is correct. The value parameter should be declared as optional. This will also return undefined for a zero coordinate value, which seems incorrect since zero is a valid lat/lon value.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

value is not optional here. but if it's undefined, I don't want to try to call .toLocaleString() and crash.

This works fine for 0. Optional chaining only fails on undefined or null, not other falsy values.

Did I misunderstand?

Copy link
Contributor

@tm-ruxandra tm-ruxandra Jun 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel that if we're possibly expecting value to be undefined in the method body. we should declare it in the method signature as well. Otherwise someone might come along this code in the future, assume that the method signature is correct, and accidentally remove the check.

You're right on optional chaining - I forgot that it specifically checks for null/undefined and not just a falsy value.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

value should not be undefined, but I don't want to crash when it is. This seems to mostly come up in hot reloads.

Copy link
Contributor

@tm-ruxandra tm-ruxandra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment on formatCoordinate function.

@paulschreiber
Copy link
Member Author

The fixes in Soil ID ranking seem to be for Sentry 5563928079, which is likely the cause of #1694

@paulschreiber paulschreiber deleted the fix/null branch July 4, 2024 22:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants