Sentinel Values for Missing Data
A sentinel marks an exceptional condition in a value channel such as the absence of an optional argument.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Keep valid values available
A function that clears a stored nickname when given None needs another way to express omission. In Python it can use one unique object as the default sentinel. An identity test detects that exact object even when inputs have their own equality methods.
Follow three calls
Calling the function without a nickname keeps the current one. Passing None clears it. Passing "" stores an empty nickname if the domain allows it. Use the unset, empty, and zero distinction to define and test all three outcomes.[1][2]
Represent three outcomes in a patch
A profile edit can keep a nickname when no nickname argument is provided, clear it on an explicit null, or set it to an empty label if the domain permits blank. Using None to stand for omission would erase the distinction between the first two cases. One function-level solution is a single private object used only as a default. Check it by identity. A request parser can independently record whether the corresponding name appeared before it calls the function.
Keep internal markers out of a data format
The unique object is useful inside one language process. A serialized message needs an actual spelling or structural distinction. An omitted JSON member and a member whose value is null are two separate structures. Treat those through the parser under a documented update policy. See JSON null and absent members.
With a stored nickname of Alex, exercise omission, explicit null, and an explicit empty string in independent runs. Verify both return status and the new state. If the domain forbids the empty name, it should report that error without silently substituting absence. Optional record fields explain the surrounding meaning.