Ross Esmond

Code, Prose, and Mathematics.

a picture of me
Written — Last Updated

Nil Value

Nil Values are those that exist outside the expected space of possible values given the type of the value. For objects, this is often null, which represents the lack of an instance. The very existence of the null value is questionable, as it will likely break any code which attempts to interact with it. There is utility, though, in allowing a value that is clearly not what was intended to exist. One example of this is the NaN value, which stands for “Not a Number.” The NaN value is defined by the IEEE 754 specification for floating-point numbers to be used whenever a mathematical operation would result in an undefined or complex number, such as with Math.sqrt(-1). Importantly, NaN has the special property that NaN !== NaN, since it is very unlikely that two undefined numbers should be considered to be equal. This distinction leads us to Null Flavors, a term used in medicine for values that represent why their intended value is missing.

Null flavors

A null flavor represents why its value is missing, rather than just the fact that the value is missing. It’s often used in medical informatics, where the different null flavors might have vastly different procedures associated with them.

Most generally, the null flavors are

  • No Information. You don’t know why the value is null. In most programming languages, this would be the default null.
  • Not Applicable. It was expected that this value is missing.
  • Other. The value exists, but cannot be represented by the value’s type.
  • Unknown. The value exists, but you don’t have it for some reason.
  • Temporarily Unknown. The value exists and is expected to be available eventually.
  • Intentionally Unknown. The value exists but has not been sought out. There may be some mechanism to request its acquisition.
  • Unauthorized. The value exists, but the accessing code is not yet permitted to see it. The code may become permitted to see it through authentication or access control change.
  • Forbidden. The value exists, but the accessing code is not, and will never be, permitted to see it.

These flavors help to determine how code should react to a missing value. An illustrative example being Not Applicable vs Intentionally Unknown. Not Applicable is a value in and of itself—it is the value—whereas Intentionally Unknown implies that the program may seek out the value if it is needed.

In fact, the only null flavors for which a system may still attempt to determine the value are Unknown, Temporarily Unknown, Intentionally Unknown, and Unauthorized. These values, along with Forbidden, may all be considered to be different flavors of Unknown, where the value exists and is as expected, but is just not available. Conversely, Not Available is considered to be its own, fully-realized value, and Other implies that the value is outside of the space of values which could be stored at this location, though it is unclear if the information of the value is still present elsewhere in the program or cannot be modeled at all. In terms of numbers, the square root of -1 would be considered an Other value if the answer could only be a floating-point, as floating-point numbers cannot encode a complex number. In many languages, the square root of -1 is NaN.

See also James Hart’s article on NaN.