1
00:00:01,980 --> 00:00:03,790
To even talk about nulls, we need to

2
00:00:03,790 --> 00:00:06,680
introduce Sir Tony Hoare, a very

3
00:00:06,680 --> 00:00:08,620
accomplished computer scientist whose

4
00:00:08,620 --> 00:00:11,500
contributions to the field are legendary.

5
00:00:11,500 --> 00:00:13,500
He's renowned for his invention of the

6
00:00:13,500 --> 00:00:15,880
Quicksort algorithm, but he contributed

7
00:00:15,880 --> 00:00:18,290
far more to computer science, like the

8
00:00:18,290 --> 00:00:21,650
addition of multiple languages. He created

9
00:00:21,650 --> 00:00:25,720
the null reference in 1965. It was during

10
00:00:25,720 --> 00:00:27,960
this activity of creating new languages

11
00:00:27,960 --> 00:00:30,410
that he decided to expose a location of

12
00:00:30,410 --> 00:00:33,330
null memory to the programmer, and the

13
00:00:33,330 --> 00:00:37,010
null reference was born. He refers to this

14
00:00:37,010 --> 00:00:39,820
as his billion‑dollar mistake, referencing

15
00:00:39,820 --> 00:00:42,400
the amount of money lost debugging,

16
00:00:42,400 --> 00:00:45,380
finding, and fixing null reference issues,

17
00:00:45,380 --> 00:00:47,550
costing staggering amounts of time and

18
00:00:47,550 --> 00:00:51,610
money. So what do nulls do to our code

19
00:00:51,610 --> 00:00:53,910
today? How many times do we write this

20
00:00:53,910 --> 00:00:56,470
code, particularly in view constructors

21
00:00:56,470 --> 00:00:58,790
for web applications, for example, or in

22
00:00:58,790 --> 00:01:01,530
controllers? A null check is practically a

23
00:01:01,530 --> 00:01:03,220
requirement when accepting nullable

24
00:01:03,220 --> 00:01:05,320
arguments into functions or constructors

25
00:01:05,320 --> 00:01:08,370
like this. Each null check increases a

26
00:01:08,370 --> 00:01:11,240
measure known a cyclomatic complexity,

27
00:01:11,240 --> 00:01:13,750
which is simply a measure of the number of

28
00:01:13,750 --> 00:01:16,090
logical branches in your code. Bigger

29
00:01:16,090 --> 00:01:18,710
numbers are bad. At this point, we've got

30
00:01:18,710 --> 00:01:21,400
a cyclomatic complexity score of 2, and we

31
00:01:21,400 --> 00:01:23,940
haven't even written anything useful yet.

32
00:01:23,940 --> 00:01:26,550
And how many times have we done this and

33
00:01:26,550 --> 00:01:28,240
seen it in our code? We have to be

34
00:01:28,240 --> 00:01:30,530
careful, right? What if an object property

35
00:01:30,530 --> 00:01:33,300
we depend on is null? What's our

36
00:01:33,300 --> 00:01:35,810
cyclomatic complexity score now? I'll

37
00:01:35,810 --> 00:01:37,560
leave that to the viewer to research for

38
00:01:37,560 --> 00:01:39,450
fun, but it's clear that our code

39
00:01:39,450 --> 00:01:41,850
complexity is rising, and we haven't even

40
00:01:41,850 --> 00:01:45,180
written anything truly useful yet. The

41
00:01:45,180 --> 00:01:48,610
program calling Person.Render may not know

42
00:01:48,610 --> 00:01:50,110
that the Person object needed for

43
00:01:50,110 --> 00:01:53,160
rendering has not been initialized. This

44
00:01:53,160 --> 00:01:56,010
is a recipe for a null reference exception

45
00:01:56,010 --> 00:02:00,160
to be thrown by the PersonView class or to

46
00:02:00,160 --> 00:02:03,440
implement some other horrific workaround.

47
00:02:03,440 --> 00:02:05,000
But what if there were a default

48
00:02:05,000 --> 00:02:07,660
implementation of the Person class? Let's

49
00:02:07,660 --> 00:02:10,530
call it NullPerson, and it was always used

50
00:02:10,530 --> 00:02:13,690
in place of a null person value instead of

51
00:02:13,690 --> 00:02:15,930
letting that value be null within the

52
00:02:15,930 --> 00:02:18,970
PersonView. In other words, PersonView can

53
00:02:18,970 --> 00:02:21,700
use the NullPerson type in its render

54
00:02:21,700 --> 00:02:24,130
method and not worry about getting a null

55
00:02:24,130 --> 00:02:26,830
reference exception. One way to implement

56
00:02:26,830 --> 00:02:29,030
a NullPerson is shown here through

57
00:02:29,030 --> 00:02:33,690
inheritance. Another way to structure the

58
00:02:33,690 --> 00:02:36,530
NullPerson would be for it to implement a

59
00:02:36,530 --> 00:02:38,865
common interface used by the Person class.

60
00:02:38,865 --> 00:02:41,310
The NullPerson would just provide a

61
00:02:41,310 --> 00:02:43,960
default implementation. Let's see this in

62
00:02:43,960 --> 00:02:47,950
a little code. Using the interface method

63
00:02:47,950 --> 00:02:50,330
of creating a NullPerson object just means

64
00:02:50,330 --> 00:02:52,000
that we're essentially going to return

65
00:02:52,000 --> 00:02:54,570
some default values that will not cause

66
00:02:54,570 --> 00:02:56,930
the null reference exception when it's

67
00:02:56,930 --> 00:02:59,760
referenced in code. The First and LastName

68
00:02:59,760 --> 00:03:01,435
properties will always return something.

69
00:03:01,435 --> 00:03:04,980
In this case, my name. This is why every

70
00:03:04,980 --> 00:03:07,010
time I think of a null object pattern, it

71
00:03:07,010 --> 00:03:09,430
makes me think it might instead be named

72
00:03:09,430 --> 00:03:12,540
default object pattern. Nevertheless,

73
00:03:12,540 --> 00:03:14,910
having the null object to use is only the

74
00:03:14,910 --> 00:03:18,770
first part of the story. The real goodness

75
00:03:18,770 --> 00:03:22,120
happens when our recipient of a person

76
00:03:22,120 --> 00:03:25,570
type can always depend on the person being

77
00:03:25,570 --> 00:03:28,710
non‑null. Now we don't need to perform any

78
00:03:28,710 --> 00:03:31,310
null checks, our code complexity score

79
00:03:31,310 --> 00:03:33,775
drops, our code comprehension increases,

80
00:03:33,775 --> 00:03:39,000
fewer lines of code are needed. Nirvana, right?

