1 00:00:00,240 --> 00:00:00,990 Welcome back. 2 00:00:01,290 --> 00:00:07,560 And this video, I would like to start with a little history lesson in order to get novels because nobles 3 00:00:07,560 --> 00:00:15,780 are a type that is only necessary because of a decision of Tony Aware, which was made in 1965 as part 4 00:00:15,780 --> 00:00:18,720 of his Algol W language development. 5 00:00:18,720 --> 00:00:20,010 So he invented this language. 6 00:00:20,280 --> 00:00:27,750 And then he also invented this now, a reference which now finally results in a lot of null reference 7 00:00:27,750 --> 00:00:30,510 exceptions or multipoint or exceptions. 8 00:00:30,510 --> 00:00:38,730 So errors that come up quite often in programming and they are called by himself as a billion dollar 9 00:00:38,730 --> 00:00:41,040 mistake or his billion dollar mistake. 10 00:00:41,250 --> 00:00:44,510 So it was the invention of the null reference in 1965. 11 00:00:44,760 --> 00:00:50,100 At the time, I was designing the first comprehensive type system for references in an object oriented 12 00:00:50,100 --> 00:00:50,630 language. 13 00:00:50,640 --> 00:00:57,840 I'll go W. My goal was to ensure that all use of references should be absolutely safe, with checking 14 00:00:57,840 --> 00:00:59,850 performed automatically by the compiler. 15 00:01:00,120 --> 00:01:05,820 But I couldn't resist the temptation to put in a null reference simply because it was so easy to implement. 16 00:01:06,450 --> 00:01:12,180 This has led to innumerable errors, vulnerabilities and system crashes, which have probably caused 17 00:01:12,180 --> 00:01:19,680 a billion dollars of pain and damage in the last 40 years, and I believe that is quite a lot more than 18 00:01:19,680 --> 00:01:25,020 just a billion dollars because it takes so much time very often to figure out what the problem is. 19 00:01:25,350 --> 00:01:28,070 And now it's a lot easier than it was in the past. 20 00:01:28,110 --> 00:01:30,840 I can imagine it was really a pain back in the day. 21 00:01:31,230 --> 00:01:36,630 And yeah, now there are a bunch of cool tools which make life so much easier when it comes to know 22 00:01:36,630 --> 00:01:39,000 pointers or null references. 23 00:01:39,300 --> 00:01:43,380 So we're going to look at one of them, which are another Bowles in Scotland. 24 00:01:43,620 --> 00:01:51,420 And if you come from a programming language like Swift, then you know this term as a optional so and 25 00:01:51,420 --> 00:01:53,310 swift is called optional here. 26 00:01:53,340 --> 00:01:54,540 It's called the Nullarbor. 27 00:01:55,200 --> 00:02:02,100 So the idea is just that it's a novel type that where a type that allows to be an Noel type, so just 28 00:02:02,100 --> 00:02:06,030 a type that allows variables to have a null as its value. 29 00:02:06,180 --> 00:02:07,590 So not a zero, but a null. 30 00:02:07,800 --> 00:02:12,750 And in some programming languages, it's called nil and Objective C, for example. 31 00:02:12,840 --> 00:02:13,860 But in. 32 00:02:14,960 --> 00:02:21,830 Java and in Scotland, it's cold now, so let's go back to our code, and here I cleaned up again, 33 00:02:21,890 --> 00:02:24,000 so I have only my main function. 34 00:02:24,890 --> 00:02:30,320 So Scotland supporters and culpability as part of its type system, that means that you have the ability 35 00:02:30,320 --> 00:02:34,130 to declare whether a variable can hold a null value or not. 36 00:02:34,400 --> 00:02:40,760 And by supporting the liability in the type system to compiler can detect possible now pointer exception 37 00:02:40,760 --> 00:02:45,380 errors at compile time and reduce the possibility of having them thrown at runtime. 38 00:02:45,590 --> 00:02:51,590 And that is super useful because you can see null pointer exceptions before they happen, so to speak. 39 00:02:51,800 --> 00:02:55,790 So before they happen on your phone, for example, when developing apps. 40 00:02:56,210 --> 00:03:00,260 All right, so let's see how we can even create such situations. 41 00:03:00,270 --> 00:03:04,760 So first of all, I'm going to create a new variable and I'm going to call this one name, and it's 42 00:03:04,760 --> 00:03:06,320 going to have the value of Dennis. 43 00:03:06,740 --> 00:03:12,680 And I'm going to explicitly state that this is going to be a string and it will make a little more sense 44 00:03:12,680 --> 00:03:15,470 once we go to the multiple types. 45 00:03:16,130 --> 00:03:20,060 So what I can do is I can assign a new value here. 46 00:03:20,060 --> 00:03:24,260 So instead of Dennis, I could rename myself and my name would be Adam. 47 00:03:24,860 --> 00:03:30,830 But what I cannot do is I cannot assign null to my name variable. 48 00:03:31,370 --> 00:03:31,940 Why is that? 49 00:03:32,000 --> 00:03:39,260 Well, because as you can see here, null cannot be a value of a non null type string. 50 00:03:39,650 --> 00:03:46,610 So if you create a variable, as we've done in the past, then it's a non null type and it doesn't accept 51 00:03:46,610 --> 00:03:48,830 to be empty, so it has to have a value. 52 00:03:49,550 --> 00:03:54,080 So what can we do if we want to have a variable that accepts null as a value? 53 00:03:54,830 --> 00:03:57,770 Well, we can make it a knowable value. 54 00:03:57,890 --> 00:04:00,240 So I'm just going to call this one Nullarbor. 55 00:04:01,900 --> 00:04:07,990 Name, and it doesn't have to have this Nullarbor at the beginning, so this is just a name that I give 56 00:04:07,990 --> 00:04:08,110 it. 57 00:04:08,530 --> 00:04:12,040 And I explicitly say again that it's a string. 58 00:04:12,220 --> 00:04:14,050 But now I'm adding a question mark. 59 00:04:14,740 --> 00:04:22,000 So this question mark now allows me to assign an empty value there so I can go ahead and say Nullarbor 60 00:04:22,029 --> 00:04:23,170 name is going to be now. 61 00:04:24,040 --> 00:04:28,340 So here I don't get a compilation error and it totally accepts it. 62 00:04:28,360 --> 00:04:34,050 That's because I said, OK, this will be of type string Nullarbor or alpha type multiple string. 63 00:04:34,060 --> 00:04:41,050 So we have this new variable and I call this one syllable name and it gets a value at the beginning 64 00:04:41,440 --> 00:04:42,900 and then I assign null to it. 65 00:04:42,910 --> 00:04:46,270 But of course, I could have also assigned null to it straight from the get go. 66 00:04:46,630 --> 00:04:48,280 It could have been an option as well. 67 00:04:49,330 --> 00:04:51,100 So now I have a noble. 68 00:04:52,240 --> 00:04:55,690 And let's have a look at the difference between the two. 69 00:04:55,900 --> 00:05:02,770 So what is really the idea behind all of that now, let's say I want to go ahead and get the length 70 00:05:02,950 --> 00:05:03,790 of my name. 71 00:05:04,120 --> 00:05:08,650 Well, I can very easily do so by using named at length. 72 00:05:09,550 --> 00:05:17,440 Now, if I go ahead and do the same thing with my Nullarbor name, I suddenly get an error because it 73 00:05:17,440 --> 00:05:17,830 says. 74 00:05:18,890 --> 00:05:25,490 Only safe or non-animal asserted calls are allowed on the Nullarbor receiver of type string. 75 00:05:26,800 --> 00:05:28,210 So how can we fix that? 76 00:05:28,930 --> 00:05:32,110 Well, there is one way and there is another way. 77 00:05:32,410 --> 00:05:36,570 So let's go ahead with the first way, which is the old fashioned way. 78 00:05:36,580 --> 00:05:43,420 So this is how you would have done it in the past if you hadn't or when you didn't have the option to 79 00:05:43,420 --> 00:05:44,110 use another boat. 80 00:05:44,470 --> 00:05:50,770 So what you could do is you could check if Nullarbor name is not equal MOE. 81 00:05:51,610 --> 00:05:54,190 Only then go ahead and do that. 82 00:05:56,560 --> 00:05:57,640 And otherwise. 83 00:05:58,940 --> 00:06:00,800 Just return null. 84 00:06:01,280 --> 00:06:03,410 So that would be an option to do it. 85 00:06:04,410 --> 00:06:11,900 Okay, so you can see we can now access the length if the Nullarbor name is not null, otherwise it 86 00:06:11,900 --> 00:06:12,770 will return null. 87 00:06:14,250 --> 00:06:17,010 All right, so that's the old fashioned way. 88 00:06:17,880 --> 00:06:24,420 And this is not necessary and current because we have a much shorter approach to this. 89 00:06:24,810 --> 00:06:33,390 So instead of doing all of that here, what we can do is we can go ahead and get this variable lenta 90 00:06:33,750 --> 00:06:37,200 as novel name that length. 91 00:06:37,590 --> 00:06:39,710 But now we add a question mark here. 92 00:06:41,120 --> 00:06:46,880 OK, so this line here is the same as this line over here. 93 00:06:48,450 --> 00:06:52,110 Now, the problem, of course, is that our another name here is, in fact now. 94 00:06:52,530 --> 00:06:54,900 So now I will get rid of this here. 95 00:06:55,410 --> 00:07:01,260 And as you can see now, it's not mal anymore, so we can even get rid of this whole line. 96 00:07:01,410 --> 00:07:04,200 But I'm just going to comment it out. 97 00:07:05,970 --> 00:07:11,790 OK, so this is a lot of work you need to put in a lot of work, you need to write all of this code, 98 00:07:11,790 --> 00:07:12,060 right? 99 00:07:12,360 --> 00:07:18,870 Or you can just add a question mark to the Nullarbor variable that you have here, and it does the same 100 00:07:18,870 --> 00:07:19,500 thing for you. 101 00:07:20,160 --> 00:07:25,290 So it says, OK, either it's going to work, and if it works, then give the length. 102 00:07:25,470 --> 00:07:30,720 So if the noble name is in fact not now, so if there is a value in there, like in our case, it's 103 00:07:30,720 --> 00:07:38,340 Dennis, then just store the result in lenta and otherwise store null and then to. 104 00:07:39,660 --> 00:07:40,500 All right, like here. 105 00:07:41,610 --> 00:07:50,340 Now you can go ahead and not only get properties of multiple variables, but also can use methods on 106 00:07:50,340 --> 00:07:50,580 them. 107 00:07:50,610 --> 00:07:55,350 So in this case, I'm using the two lower case method on my Nullarbor name. 108 00:07:55,560 --> 00:07:57,780 And if I run this, then what? 109 00:07:57,780 --> 00:08:02,400 It will return if it's empty and we'll just return null. 110 00:08:02,920 --> 00:08:08,670 So otherwise, it's going to return the name Dennis, in my case, so that we are, says Dennis. 111 00:08:08,970 --> 00:08:10,710 But let's say we make it now. 112 00:08:10,710 --> 00:08:14,190 So I uncomment this line here and I run it again. 113 00:08:14,670 --> 00:08:18,030 Then, well, it's not even accepting it, so I can't even run the code. 114 00:08:18,570 --> 00:08:19,170 It's even. 115 00:08:20,620 --> 00:08:21,610 Not even printing it. 116 00:08:21,940 --> 00:08:24,250 So that's the thing about the Nullarbor. 117 00:08:26,240 --> 00:08:28,490 Variables and using the question mark here. 118 00:08:30,260 --> 00:08:32,390 Which is the safe call operator. 119 00:08:33,020 --> 00:08:40,760 So now let's say we only want to do something or print something if the variable is in fact not know 120 00:08:41,990 --> 00:08:43,070 what we can then do. 121 00:08:43,549 --> 00:08:46,430 We can use the safe call operator with that. 122 00:08:47,090 --> 00:08:48,830 So let me show you that real quick. 123 00:08:50,650 --> 00:08:51,280 So here. 124 00:08:53,670 --> 00:08:55,890 I'm just going to use the Nullarbor name again. 125 00:08:57,110 --> 00:09:02,500 Then the question mark, so I need to use the save call operator that led. 126 00:09:02,840 --> 00:09:08,000 And then in curly brackets, the code that I want to execute only if it's not now. 127 00:09:08,270 --> 00:09:12,320 So only if there is a value in there, I want to execute something. 128 00:09:12,590 --> 00:09:16,400 So this is a very short way of doing another, if else, statement here. 129 00:09:17,290 --> 00:09:18,970 OK, so let me run it again. 130 00:09:22,060 --> 00:09:22,930 And there we are. 131 00:09:22,960 --> 00:09:25,990 It says, five, because the length of my name is five. 132 00:09:27,270 --> 00:09:31,350 So now let's say it's now let's test it, and there we are. 133 00:09:31,860 --> 00:09:33,480 There's nothing, so it says nothing. 134 00:09:33,510 --> 00:09:37,820 It didn't do anything because our another name, in fact, was null. 135 00:09:38,760 --> 00:09:39,180 All right. 136 00:09:39,180 --> 00:09:41,300 So that's the first part of Nullarbor us. 137 00:09:41,310 --> 00:09:48,030 In the next video, we are going to look at the Elvis operator and not null assertion operator. 138 00:09:48,330 --> 00:09:56,160 So, yeah, let's just check those out, because then we have a rather full understanding of what those 139 00:09:56,640 --> 00:09:59,100 multiples do and how we can use them. 140 00:09:59,580 --> 00:10:00,720 So see you in the next video.