For example, a bit or a boolean can be either 0 or 1 so the number 2 is associated with it. Similarly, for a byte which is 8 bits, the maximum number of different assignments would be 2^8.
Is there a name for this number?
When we pass everything through our system that has ECMAScript, Java and MySQL, then a boolean does not have only two possible assignments. For instance, a false boolean gets saved as a 0 and the boxed value could be null so a boolean suddenly can get true, false, 0, 1, null or even undefined or <missing>
.
I think it could get problematic in tests to guarantee that the values are not inconsistent. For instance, a value boolean locked
could become null
and then when a script or a layout template evaluates it then it will evaluate to false
somewhere if the real value was null
and similar problems.
So why don’t we always assert that a boolean has the same number of possible values (2 values) and similarly for other types?
There is a mathematical term named “arity” that is something similar but not exactly, and statistics and probability theory also has the concept of “event space” that would be almost exactly what I mean. For instance, the event space for a boolean would be the set {0,1}
which has cardinality 2 and that cardinality doesn’t get preserved throughout the system, especially when data is passed as polyglots and/or serialized (json, jsonp, xml, yaml).
4
I would call it cardinality (and indeed I have used it in that sense). It is strictly the cardinality of the set of all values a variable can take.
Edit: for example, a 16-bit integer can take exactly 65536 values. The cardinality of the set of all values that the integer can take is 655536.
Once you start to consider variables that have a range of valid values and also a range of invalid values (like a boolean with a bitwise value of 17), then you have to extend the concept somewhat, but you can still describe those values with set notation and the concept of cardinality still applies.
Edit: Yes, this is the mathematical set, not programming Set. If you were to apply the concept to the C# type short and derive the type short?, this would not have 65537 values, represented by the union of the set of 65536 values mentioned previously and one additional value, null.
5
If you subscribe to the “types as a set of values” interpretation of type theory, then cardinality is in fact a good name.
So, the cardinality of bool
would be 2, the cardinality of byte
would be 256, and the cardinality of String
would be ℵ0.