August 2021
Intermediate to advanced
376 pages
11h 2m
English
If the language we’re using supports null safety (and we’ve enabled it, should that be required), there will be a mechanism for annotating types to indicate that they can be null. This often involves a ? character to indicate nullability. Code will often look something like the following:
Element? getFifthElement(List<Element> elements) { ❶
if (elements.size() < 5) {
return null;
}
return elements[4];
}
❶ The ? in Element? indicates that the return type can be null.
If an engineer using this code forgets to handle the scenario where getFifthElement() returns null, their code will not compile, as demonstrated in the following listing:
void displayElement(Element element) { ... } ❶Read now
Unlock full access