- Tony Hoare invented the null pointer in 1960 while developing the ALGOL programming language to handle errors when accessing array elements.
- He later called the invention of the null reference his "billion dollar mistake" because it has led to many errors, vulnerabilities and system crashes over decades of use.
- Dart's new null safety features make variables non-nullable by default and require handling null values explicitly to avoid errors. Migrating code to null safety involves adding question marks to variable types, handling null values, and using tools to help with the migration.
5. Tony Hoare
Sir Charles Antony Richard Hoare born 11 January 1934)
- is a British computer scientist. He developed the sorting algorithm
quicksort in 1959–1960.[5] He also developed Hoare logic for
verifying program correctness.
https://en.wikipedia.org/wiki/Tony_Hoare /DahabDev
6. - In 1960, Hoare left the Soviet Union and began working at Elliott Brothers.
- After 9 months of programming he was asked to design a new
programming language !!
- Most software was still written in machine code.
- In the library was a 23-page booklet entitled "Report on the international
language ALGOL60"
- He used it as a basis for the new language.
- In order to implement error messages, an array had a check to verify
whether its reference was in the bounds!.
- Adding checks to arrays added space and time to the program !.
- Then he went and invented the null pointer. Or he either have to
check every reference.
https://en.wikipedia.org/wiki/Tony_Hoare
Tony Hoare in Moscow 1960
/DahabDev
11. Null
Pointer/Reference
- A null pointer is a pointer that does
not point to any memory location.
- It represents an invalid memory
location .
- when a null value is assigned to a
pointer then the pointer is considered
as null pointer.
/DahabDev
12. Null Pointer/Reference Exception (NPE)
- Invoking a method from a null object.
- Accessing or modifying a null object’s field.
- Taking the length of null, as if it were an array.
- Accessing or modifying the slots of null
object, as if it were an array.
/DahabDev
17. Tony Hoare
https://en.wikipedia.org/wiki/Tony_Hoare
I call it my billion-dollar mistake. It was the invention of the null
reference in 1965. ... This has led to innumerable errors,
vulnerabilities, and system crashes, which have probably caused
a billion dollars of pain and damage in the last forty years.
2009
/DahabDev
18. Tony Hoare
https://en.wikipedia.org/wiki/Tony_Hoare
- Null references have historically been a bad idea.
- Programming language designers should be responsible for the
errors in programs written in that language.
- If the billion dollar mistake was the null pointer, the C gets()
function is a multi-billion dollar mistake that created the
opportunity for malware and viruses to thrive (Buffer overflow)
/DahabDev
22. Dart Null Safety
Null safety is the largest
change we’ve made to Dart
since we replaced the original
unsound optional type system
with a sound static type
system in Dart 2.0.
/DahabDev
28. Dart Null safety principles
/DahabDev
Dart null safety support is based on the following three core design principles:
● Non-nullable by default. Unless you explicitly tell Dart that a variable can be null, it’s considered non-nullable.
This default was chosen after research found that non-null was by far the most common choice in APIs.
● Incrementally adoptable. You choose what to migrate to null safety, and when. You can migrate incrementally,
mixing null-safe and non-null-safe code in the same project. We provide tools to help you with the migration.
● Fully sound. Dart’s null safety is sound, which enables compiler optimizations. If the type system determines
that something isn’t null, then that thing can never be null. Once you migrate your whole project and its
dependencies to null safety, you reap the full benefits of soundness —- not only fewer bugs, but smaller
binaries and faster execution
https://dart.dev/null-safety
29. /DahabDev
- All variables are non-nullable by default
- If the variable can have the value null, add ? to its type declaration.
String? name = null;
- If you know that a non-nullable variable will be initialized to a non-null value
before it’s used, but the Dart analyzer doesn’t agree, insert late before the
variable’s type.
late String name;
Dart null safety roles
https://dart.dev/null-safety
30. /DahabDev
- When using a nullable variable or expression, be sure to handle null values. For
example, you can use an if statement, the ?? operator, or the ?. operator to
handle possible null values.
String value = name ?? ''; // '' if it's null; otherwise, the String
- If you’re sure that an expression with a nullable type isn’t null, you can add ! to
make Dart treat it as non-nullable:
String? name = 'Ahmed';
String value = name!; // `name!` is an String.
// This throws if name is null.
Dart null safety roles
https://dart.dev/null-safety
31. Dart null safety roles
/DahabDev
- Once you opt into null safety, you can’t use the member access operator (.) if the
operand might be null. Instead, you can use the null-aware version of that
operator (?.):
double? d;
print(d?.floor()); // Uses `?.` instead of `.` to invoke `floor()`.
- If you’re sure that an expression with a nullable type isn’t null, you can add ! to
make Dart treat it as non-nullable:
String? name = 'Ahmed';
String value = name!; // `name!` is an String.
// This throws if name is null.
https://dart.dev/null-safety
32. list, set, and map
/DahabDev
https://dart.dev/null-safety
33. list, set, and map
/DahabDev
https://dart.dev/null-safety
46. Migrating to null safety
/DahabDev
1. Wait for the packages that you depend on to migrate.
2. Migrate your package’s code, preferably using the interactive migration tool.
3. Statically analyze your package’s code.
4. Test to make sure your changes work.
5. If the package is already on pub.dev, publish the null-safe version as a
prerelease version.
https://dart.dev/null-safety