This article is included in the official wechat account “Meandni”.
In 2009, Tony Hoare, author of the fast sorting algorithm and 1980 Turing Prize winner, presented a paper titled Null References at QCon in London. The Billion Dollar Mistake speech, which was supposed to be a lively sharing session, was full of remorse and apology throughout, because he believed that it was he who introduced The null pointer into The programming language in 1964 that led to many immeasurable losses later.
According to Hoare’s own description, the reason why the null pointer was introduced in ALGO was that it was so easy to implement, Nulls later led to innerable errors, shapatleniency, and system crashes, all of which led to billions of dollars in losses over decades.
“I call the Null reference my billion-dollar mistake. It was invented in 1965, when I designed the first comprehensive reference type system in an object-oriented language (ALGOL W). My goal is to make sure that all references are perfectly safe to use and that the compiler checks automatically. But I couldn’t resist adding Null references, simply because it was so easy to implement.”
According to the survey, 8 out of the 10 most common exceptions in Javascript are caused by null and undefined.
So, it’s kind of a language designer’s problem, and there’s a lot of disagreement about what nullpointers should be. Some people think that nullpointers should be an integral part of programming languages. Hoare’s good friend Edsger Dijkstra later compared null to adultery, saying that every property in an object with a value of NULL is XX by NULL XX, which I will not comment on here.
At the same time, many modern advanced languages, such as Kotlin, Swift, and Rust, have consciously tried to avoid null Pointers. Dart, which I’ll talk about today, began refactoring the type architecture earlier this year to support null-safe, which detects null pointer errors at compile time. This means that the Flutter application will no longer crash due to null pointer errors. .
Dart 2.10 air safety features
Before Dart 2.10, we often made the following mistakes:
// Without null safety:
bool isEmpty(String string) => string.length == 0;
main() {
isEmpty(null);
} Copy the code
Since null is of type NULL and there is no getter for the Length property, a NoSuchMethodError exception is raised, which is fatal to us low-level client developers.
The advantage of strongly typed languages is that we can find bugs in our programs right at the compiler, and we can fix them, but null-pointer errors aren’t on his list. So Dart introduced null-safe in 2.10. During compilation in the IDE, we can take care of every possible null pointer error.
Dart’s null-Safe feature is currently in phase 2 testing and will be incorporated into Flutter testing next monthDartPadTaste the fresh.
In addition, the introduction of null-safety directly requires us to write code without null Pointers, so the Dart compiler and runtime can eliminate the need to optimize internal null-checking, and the application will be lighter and faster.
Since the Dart is currently in preview and not ready for production, feel free to give feedback if you run into technical problems during beta testing.
Method of use
The new operators and keywords introduced by null-safe include? ,! For those of you who have worked with Kotlin, TypeScript, or C#, this feature is already familiar. Here’s how to use it in the Dart language.
At this point, all variables written in the code are not null by default:
// These variables are not empty and are initialized directly
var i = 42;
String name = getFileName();
final b = Foo();
Copy the code
If you want a variable to be null, you can use? The operator declares a nullable variable as follows:
int? aNullableInt = null;
Copy the code
If you are certain that the variable must be initialized before use, you can use the late keyword to delay the operation:
class IntProvider {
late int aRealInt;
IntProvider() {
aRealInt = calculate();
} } Copy the code
In this case, Dart Analyzer does not require the aRealInt variable to immediately give a specified value, which we can call a late variable.
When using controllable variables, you must use? Or if statement to short:
int value = aNullableInt ?? 0;
int definitelyInt(int? aNullableInt) {
if (aNullableInt == null) {
return 0;
} return aNullableInt; // Can't be null! } Copy the code
If you can guarantee that a nullable variable will never be null when used, use it! Key word statement:
int? aNullableInt = 2;
intvalue = aNullableInt! ;// `aNullableInt! 'is determined to be int.
Copy the code
If you want to change the type of a nullable variable, you can use it instead! In addition to the operator, we can also use as:
return maybeNum() as int;
Copy the code
Finally, if null-safe features are introduced, methods cannot be called directly on nullable variables using the. Operator. Instead, use? . :
double? d;
print(d? .floor());/ / use? Instead.
Copy the code
Introduce when the time is right
Null-safe is officially available as an optional feature because null-Safe will not be null by default, affecting changes to the Dart type system. It is suggested that we find the right time to do this risky operation.
To phase in
It is also recommended that we refactor the code in a specific order, introducing this feature first in a standalone module project. For example, if C depends on B, and B depends on A, refactor A to air security first, then B, then C, regardless of whether A, B, and C are libraries, packages, or applications, the order of refactoring should be followed.
Why refactor the code in this order? , although we can reconstruct project code directly, but if the dependencies in the process of reconstruction changed their API, it may also reconstructs the secondary, the official will also provide a series of development tools to help developers to find out which rely on library is migration, the author of the open source project attention, in order to avoid accidental error, Wait until all of your dependencies have been refactored before releasing an air-safe version.
Automation tool
Once the dependencies have been refactored, the project code can be refactored using an official migration tool. The development tool analyzes all existing code to find which declarations can be non-null (unchanged) and which must be nullable (using the nullable flag “? Blank mark).
The migration tool is interface interactive, so we can directly see the nullability properties inferred by the tool, or if we don’t want to refactor the code in the check results, we can use? Character is marked as nullable.
The next step
There will be more updates on Dart’s air safety features in the months following the conference, and they will also be introduced into the framework with the help of the Flutter team developers. For details on how to use air safety, please refer to the official documentation. Please refer to the Understanding Null Safety for more details on Dart’s official design motivation and philosophy. I will keep you updated on the Dart design in the near future.
read
https://medium.com/dartlang/announcing-dart-2-10-350823952bd5
https://blog.maxkit.com.tw/2015/08/null-null-reference-is-billion-dollar.html?m=1
https://blog.csdn.net/turingbook/article/details/3954798
https://dev.to/joelnet/null-the-billion-dollar-mistake-maybe-just-nothing-1cak
Welcome everyone to communicate with me, follow the public account “Meandni” to read the latest technology trends or add my wechat “MeandniBlog”.