Let's say you're managing the list of serial numbers of the bicycles owned by residents in your building, with the objective of planning ahead to build additional safe bike storage.
Some people will of course have no bikes.
In Dart > 2.12 (with null safety) you could
use a non-nullable List<String>
and initialize it to an empty list
class Resident {
String name;
List<String> bicycles = [];
}
or you could use a nullable List<String>
and use null
as a flag to signal that someone has no bikes.
class Resident {
String name;
List<String>? bicycles;
}
Both designs are of course workable, but does it turn out down the road that one is clearly better than the other—more idiomatic to the new Dart, for example? In other words, what's better, an initially empty non-nullable or an initially nullable and null container?
Even if I count the bits needed, it's not quite clear. There would be wasted storage to construct an empty list in the first case, but there is also storage wasted in the second case—though it's of an unknown, and perhaps implementation dependent—amount.