1 00:00:05,640 --> 00:00:06,060 Hello. 2 00:00:08,310 --> 00:00:11,760 In this video, we're going to talk about Question 21. 3 00:00:12,270 --> 00:00:14,840 What is the purpose of the correct keyword? 4 00:00:15,540 --> 00:00:22,380 The correct keyword is used to define a scope in which our automatic operations will be checked for 5 00:00:22,380 --> 00:00:23,130 overflow. 6 00:00:23,460 --> 00:00:30,180 In the previous lecture, we learned that some arithmetic operations can give unexpected results when 7 00:00:30,180 --> 00:00:34,770 it is not possible to represent the outcome with the numeric type that we used. 8 00:00:35,070 --> 00:00:42,000 For example, when using integers, adding two billion to two billion gives a negative number because 9 00:00:42,000 --> 00:00:47,430 the actual result four billion is too large to be represented with an integer. 10 00:00:47,820 --> 00:00:49,230 Let's see this encode. 11 00:00:57,870 --> 00:01:00,690 As you can see, the result is not correct. 12 00:01:01,140 --> 00:01:06,060 But notice a very important thing, no exception has been thrown. 13 00:01:06,450 --> 00:01:08,670 The program continued without problem. 14 00:01:09,150 --> 00:01:11,130 You may be a bit surprised by it. 15 00:01:11,430 --> 00:01:17,220 Usually when we do something, invite an exceptionally strong informing us what happened. 16 00:01:17,550 --> 00:01:19,280 Exceptions are a good thing. 17 00:01:19,290 --> 00:01:24,120 Actually, it's better to be clearer informed that something went wrong. 18 00:01:24,420 --> 00:01:31,140 In this case, the problem is the number overflow because the result four billion can't be represented 19 00:01:31,140 --> 00:01:34,140 on 32 bits would integer occupies. 20 00:01:34,560 --> 00:01:37,590 This situation is called the number overflow. 21 00:01:38,010 --> 00:01:40,770 The number overflow is a silent failure. 22 00:01:41,190 --> 00:01:46,800 The program doesn't work correctly, but it continues to work under way without exception. 23 00:01:47,310 --> 00:01:49,740 This can have disastrous effects. 24 00:01:50,100 --> 00:01:55,800 Invalid data may be stored in databases, overwriting old, valid data. 25 00:01:56,070 --> 00:02:02,430 Also, if the problem continues, it can be allowed to execute further invite operations. 26 00:02:03,780 --> 00:02:10,770 For example, imagine a banking system which stores the sum of daily transactions of the user and the 27 00:02:10,770 --> 00:02:15,030 blocks and the further payments if some limit has been exceeded. 28 00:02:15,540 --> 00:02:22,920 Let's imagine a very rich customer who is allowed to pay up to two billion of some currency every day. 29 00:02:23,280 --> 00:02:28,980 If the sum of daily payments exceeds two billion, the next payment should be blocked. 30 00:02:29,220 --> 00:02:32,430 And this is what this if statement is taking. 31 00:02:32,880 --> 00:02:35,130 But let's try something like this. 32 00:02:35,700 --> 00:02:43,530 Let's say the client makes a payment of one billion nine hundred million of some currency and then tries 33 00:02:43,530 --> 00:02:45,840 to make the next one for one billion. 34 00:02:46,230 --> 00:02:52,710 The second transaction should be blocked because the daily sum would be over the limit of two billion. 35 00:02:53,190 --> 00:02:54,690 Let's see if this will happen. 36 00:02:57,200 --> 00:03:00,770 As you can see, both transfers has been successful. 37 00:03:01,100 --> 00:03:04,400 We don't see the Massad transaction limit reached. 38 00:03:04,790 --> 00:03:10,670 This is because the daily Psalm became a negative number due to arithmetic overflow. 39 00:03:11,000 --> 00:03:14,750 And of course, and the negative number is less than two billion. 40 00:03:15,080 --> 00:03:19,190 So this condition will be true and we'll proceed with the transfer. 41 00:03:19,550 --> 00:03:23,540 We will allow the client to make more and more payments. 42 00:03:23,960 --> 00:03:29,750 And what if those payments are actually done by someone who hacked the client's account? 43 00:03:29,990 --> 00:03:34,700 Now the client moves all the money instead of somebody, we get some. 44 00:03:35,180 --> 00:03:39,650 I hope I convince you that arithmetic overflows are dangerous. 45 00:03:40,040 --> 00:03:41,870 So how to deal with them? 46 00:03:42,380 --> 00:03:45,020 This is where the Typekit keyword comes in handy. 47 00:03:45,440 --> 00:03:53,210 The tech world defines a scope in which arithmetic operations will be tracked for overflow if it happens. 48 00:03:53,300 --> 00:03:55,040 An exception will be thrown. 49 00:04:07,070 --> 00:04:14,240 In this scope and the overflow, we'll throw an exception instead of folding silently, let's make sure 50 00:04:14,240 --> 00:04:14,960 it works. 51 00:04:16,930 --> 00:04:21,190 As you can see, this time, the second transfer was not executed. 52 00:04:21,510 --> 00:04:23,680 An overflow exception has been thrown. 53 00:04:24,340 --> 00:04:28,210 You may wonder why isn't this done by default? 54 00:04:28,630 --> 00:04:31,720 Well, the reason is simple it's performance. 55 00:04:32,080 --> 00:04:38,140 Computers are very good at doing arithmetic operations, and they do them extremely fast. 56 00:04:38,440 --> 00:04:45,010 On the other hand, checking for overflow is actually quite a complex operation, and it takes some 57 00:04:45,010 --> 00:04:45,460 time. 58 00:04:45,970 --> 00:04:52,450 If we have a lot of arithmetic operations in the application, the performance impact may be noticeable. 59 00:04:53,570 --> 00:05:01,100 I've created a little program that measures the performance difference for checked and Eintracht operations. 60 00:05:01,340 --> 00:05:04,850 You can find it in the repository attached to the course. 61 00:05:05,390 --> 00:05:06,770 It works like this. 62 00:05:07,100 --> 00:05:13,540 I defined a method that in a loop executes some arithmetic operations in this method. 63 00:05:13,550 --> 00:05:15,680 It happens in the Typekit context. 64 00:05:16,010 --> 00:05:20,090 Then I have another method which does exactly the same. 65 00:05:20,210 --> 00:05:26,510 But in this one, the group is in the untucked block, which means the overflow will not be checked. 66 00:05:26,990 --> 00:05:29,900 Let's run it and see the performance difference. 67 00:05:30,980 --> 00:05:37,520 I said that the set size to one billion, which means each loop will be executed one billion times. 68 00:05:41,740 --> 00:05:48,940 On my computer for one billion iterations, it took over five and a half seconds in the correct context 69 00:05:49,090 --> 00:05:56,740 and 4.2 in the entourage, which means the look executed in the correct context took 33 percent more 70 00:05:56,740 --> 00:05:57,160 time. 71 00:05:57,520 --> 00:06:01,750 As you can see, the difference is not huge, but it is noticeable. 72 00:06:02,650 --> 00:06:06,610 Now we have the basics of the fury about the text keyword. 73 00:06:06,950 --> 00:06:09,460 Let's think how to apply it in practice. 74 00:06:09,880 --> 00:06:11,770 Here is a couple of tips. 75 00:06:12,160 --> 00:06:16,820 First of all, be aware of the limitations of the types you are using. 76 00:06:17,290 --> 00:06:21,700 Knowing that tools proper numeric types for your needs. 77 00:06:22,060 --> 00:06:28,780 For example, if you need a counter of other months, the user selected from the list that by design 78 00:06:28,780 --> 00:06:33,670 shows no more than 100 elements, then feel free to use Byte. 79 00:06:33,910 --> 00:06:39,160 It's tiny, but the limitation is 255, which is more than enough. 80 00:06:39,730 --> 00:06:46,300 On the other hand, let's consider what you could use if you need a number representing the total number 81 00:06:46,300 --> 00:06:50,290 of financial transactions ever made in your banking application. 82 00:06:50,830 --> 00:06:57,760 It sounds good, but what if your application becomes a roaring success and soon the number slightly 83 00:06:57,760 --> 00:06:59,950 over two billion is just not enough? 84 00:07:00,280 --> 00:07:03,340 In this case, long may be a better choice. 85 00:07:03,700 --> 00:07:12,700 The max value of long is over four billion times larger than the value of INS in case your number must 86 00:07:12,700 --> 00:07:13,690 be unlimited. 87 00:07:13,840 --> 00:07:19,690 For example, you are an astronomer and you want to measure the galaxy size in millimeters. 88 00:07:19,960 --> 00:07:22,840 You can always use big integer type. 89 00:07:23,110 --> 00:07:30,100 Big integer is only limited by the size of the memory of your computer, so you can represent gigantic 90 00:07:30,100 --> 00:07:31,150 numbers with it. 91 00:07:31,900 --> 00:07:38,620 If you have even the slightest concern that an undesired overflow may happen, you have two choices. 92 00:07:39,100 --> 00:07:44,740 Put this code in the correct context, so an exception is thrown in case of overflow. 93 00:07:45,070 --> 00:07:49,600 But also you can check for overflow before an actual operation. 94 00:07:49,960 --> 00:07:51,820 For example, like this? 95 00:07:53,130 --> 00:07:59,490 Here I have the Entourage context, but they also have an if checking if the result of the operation 96 00:07:59,670 --> 00:08:05,460 will be larger than the value of in this case, an exception will be thrown. 97 00:08:05,880 --> 00:08:08,910 Let's see what will be the performance of these methods. 98 00:08:13,220 --> 00:08:19,790 It turned out that taking the overflow like this is actually slightly better from the performance point 99 00:08:19,790 --> 00:08:22,580 of view than using the text keyword. 100 00:08:22,940 --> 00:08:28,910 Please note that which one is performance wise better depends a lot on the particular situation. 101 00:08:29,300 --> 00:08:31,790 It's best to run some benchmarks on your own. 102 00:08:32,570 --> 00:08:39,350 Also, remember that if you really need to, you can set the project setting to track arithmetic operations 103 00:08:39,350 --> 00:08:40,250 by default. 104 00:08:40,520 --> 00:08:47,150 In this case, if you want some code to be outworked, you can use the untracked keyword to define a 105 00:08:47,150 --> 00:08:52,880 scope in which the arithmetic operations will not be tracked before we move on. 106 00:08:53,030 --> 00:08:56,960 We must mention one thing about the direct keyword. 107 00:08:57,230 --> 00:09:04,370 The overflow check only applies to the immediate code block, not to any function calls inside the block. 108 00:09:04,640 --> 00:09:07,550 To understand this, let's consider this code. 109 00:09:08,240 --> 00:09:11,370 What do you think will happen at the first glance? 110 00:09:11,420 --> 00:09:14,750 You may think that the overflow exception will be thrown. 111 00:09:15,140 --> 00:09:22,220 After all, we call the automated in the text scope, so adding two billion to two billion should cause 112 00:09:22,220 --> 00:09:22,970 an exception. 113 00:09:23,420 --> 00:09:26,210 Well, let me run it and see if it's true. 114 00:09:33,690 --> 00:09:36,900 Well, as you can see, no exception has been thrown. 115 00:09:37,410 --> 00:09:43,560 This is because the strict scope doesn't affect any methods that are called within it. 116 00:09:44,010 --> 00:09:50,910 If you want the code to actually be checked, we must add the technique you worked inside the automated. 117 00:09:53,780 --> 00:09:56,240 And now the exception is here. 118 00:09:57,350 --> 00:10:04,640 Let's summarize, the tech world is used to define a scope in which our romantic operations will be 119 00:10:04,640 --> 00:10:06,120 tracked for overflow. 120 00:10:06,380 --> 00:10:12,530 If this key world will not be used and the arithmetic overflow will not cause an exception, but will 121 00:10:12,530 --> 00:10:19,490 simply result in an invalid value during the interview, you can be asked What is the purpose of the 122 00:10:19,490 --> 00:10:20,720 untracked keyword? 123 00:10:21,170 --> 00:10:27,380 This keyword defines a scope in which technique of arithmetic overflow is disabled. 124 00:10:27,950 --> 00:10:34,550 It makes sense to use it in projects in which they're checking for overflow is globally enabled. 125 00:10:34,940 --> 00:10:37,910 This can be searched on the product level settings. 126 00:10:38,450 --> 00:10:43,130 What is a silent failure is a kind of failure that happens. 127 00:10:43,220 --> 00:10:46,640 We fault and not ification to the users or developers. 128 00:10:47,150 --> 00:10:53,000 They are not informed that something went wrong and the application moves on passively in an invalid 129 00:10:53,000 --> 00:10:53,630 state. 130 00:10:54,170 --> 00:10:56,510 What is the big integer type? 131 00:10:56,870 --> 00:11:01,250 It's a numeric type that can represent an integer of any size. 132 00:11:01,610 --> 00:11:04,580 It is limited only by the applications memory. 133 00:11:04,760 --> 00:11:10,460 It should be used to represent gigantic numbers in most practical cases. 134 00:11:10,700 --> 00:11:12,380 Long is more than enough. 135 00:11:12,800 --> 00:11:18,050 The largest long is over four billion times larger than the maximal end. 136 00:11:18,500 --> 00:11:25,310 It means big integer should be used instead of wrong, only to represent unthinkably large numbers. 137 00:11:25,940 --> 00:11:28,190 All right, that's it for this lecture. 138 00:11:28,640 --> 00:11:31,610 Thanks for watching, and I'll see you in the next one.