logoalt Hacker News

shepherdjerred • today at 4:15 AM • 0 replies • view on HN

I hate sentinel values. Even/especially with AI-generated code.

1. If your date does not have a value, do not return 0, an empty string, or None. Use a discriminated union and model your domain rigorously.

2. If your data model accepts None/undefined as a value, that should translate to your business model as well. For example

3. Do not make your `name` field as `string | undefined` just because there might be an intermediate loading or processing state. It's better to model that as something like `Loaded<string>` where Loaded is something like a Result or Optional type (though ideally more rigorously defined)

Here is what I've done for years in TypeScript, though now in a nice package thanks for AI: https://github.com/shepherdjerred/monorepo/blob/main/package...