Hello.
In this video, we're going to talk about Question 21.
What is the purpose of the correct keyword?
The correct keyword is used to define a scope in which our automatic operations will be checked for
overflow.
In the previous lecture, we learned that some arithmetic operations can give unexpected results when
it is not possible to represent the outcome with the numeric type that we used.
For example, when using integers, adding two billion to two billion gives a negative number because
the actual result four billion is too large to be represented with an integer.
Let's see this encode.
As you can see, the result is not correct.
But notice a very important thing, no exception has been thrown.
The program continued without problem.
You may be a bit surprised by it.
Usually when we do something, invite an exceptionally strong informing us what happened.
Exceptions are a good thing.
Actually, it's better to be clearer informed that something went wrong.
In this case, the problem is the number overflow because the result four billion can't be represented
on 32 bits would integer occupies.
This situation is called the number overflow.
The number overflow is a silent failure.
The program doesn't work correctly, but it continues to work under way without exception.
This can have disastrous effects.
Invalid data may be stored in databases, overwriting old, valid data.
Also, if the problem continues, it can be allowed to execute further invite operations.
For example, imagine a banking system which stores the sum of daily transactions of the user and the
blocks and the further payments if some limit has been exceeded.
Let's imagine a very rich customer who is allowed to pay up to two billion of some currency every day.
If the sum of daily payments exceeds two billion, the next payment should be blocked.
And this is what this if statement is taking.
But let's try something like this.
Let's say the client makes a payment of one billion nine hundred million of some currency and then tries
to make the next one for one billion.
The second transaction should be blocked because the daily sum would be over the limit of two billion.
Let's see if this will happen.
As you can see, both transfers has been successful.
We don't see the Massad transaction limit reached.
This is because the daily Psalm became a negative number due to arithmetic overflow.
And of course, and the negative number is less than two billion.
So this condition will be true and we'll proceed with the transfer.
We will allow the client to make more and more payments.
And what if those payments are actually done by someone who hacked the client's account?
Now the client moves all the money instead of somebody, we get some.
I hope I convince you that arithmetic overflows are dangerous.
So how to deal with them?
This is where the Typekit keyword comes in handy.
The tech world defines a scope in which arithmetic operations will be tracked for overflow if it happens.
An exception will be thrown.
In this scope and the overflow, we'll throw an exception instead of folding silently, let's make sure
it works.
As you can see, this time, the second transfer was not executed.
An overflow exception has been thrown.
You may wonder why isn't this done by default?
Well, the reason is simple it's performance.
Computers are very good at doing arithmetic operations, and they do them extremely fast.
On the other hand, checking for overflow is actually quite a complex operation, and it takes some
time.
If we have a lot of arithmetic operations in the application, the performance impact may be noticeable.
I've created a little program that measures the performance difference for checked and Eintracht operations.
You can find it in the repository attached to the course.
It works like this.
I defined a method that in a loop executes some arithmetic operations in this method.
It happens in the Typekit context.
Then I have another method which does exactly the same.
But in this one, the group is in the untucked block, which means the overflow will not be checked.
Let's run it and see the performance difference.
I said that the set size to one billion, which means each loop will be executed one billion times.
On my computer for one billion iterations, it took over five and a half seconds in the correct context
and 4.2 in the entourage, which means the look executed in the correct context took 33 percent more
time.
As you can see, the difference is not huge, but it is noticeable.
Now we have the basics of the fury about the text keyword.
Let's think how to apply it in practice.
Here is a couple of tips.
First of all, be aware of the limitations of the types you are using.
Knowing that tools proper numeric types for your needs.
For example, if you need a counter of other months, the user selected from the list that by design
shows no more than 100 elements, then feel free to use Byte.
It's tiny, but the limitation is 255, which is more than enough.
On the other hand, let's consider what you could use if you need a number representing the total number
of financial transactions ever made in your banking application.
It sounds good, but what if your application becomes a roaring success and soon the number slightly
over two billion is just not enough?
In this case, long may be a better choice.
The max value of long is over four billion times larger than the value of INS in case your number must
be unlimited.
For example, you are an astronomer and you want to measure the galaxy size in millimeters.
You can always use big integer type.
Big integer is only limited by the size of the memory of your computer, so you can represent gigantic
numbers with it.
If you have even the slightest concern that an undesired overflow may happen, you have two choices.
Put this code in the correct context, so an exception is thrown in case of overflow.
But also you can check for overflow before an actual operation.
For example, like this?
Here I have the Entourage context, but they also have an if checking if the result of the operation
will be larger than the value of in this case, an exception will be thrown.
Let's see what will be the performance of these methods.
It turned out that taking the overflow like this is actually slightly better from the performance point
of view than using the text keyword.
Please note that which one is performance wise better depends a lot on the particular situation.
It's best to run some benchmarks on your own.
Also, remember that if you really need to, you can set the project setting to track arithmetic operations
by default.
In this case, if you want some code to be outworked, you can use the untracked keyword to define a
scope in which the arithmetic operations will not be tracked before we move on.
We must mention one thing about the direct keyword.
The overflow check only applies to the immediate code block, not to any function calls inside the block.
To understand this, let's consider this code.
What do you think will happen at the first glance?
You may think that the overflow exception will be thrown.
After all, we call the automated in the text scope, so adding two billion to two billion should cause
an exception.
Well, let me run it and see if it's true.
Well, as you can see, no exception has been thrown.
This is because the strict scope doesn't affect any methods that are called within it.
If you want the code to actually be checked, we must add the technique you worked inside the automated.
And now the exception is here.
Let's summarize, the tech world is used to define a scope in which our romantic operations will be
tracked for overflow.
If this key world will not be used and the arithmetic overflow will not cause an exception, but will
simply result in an invalid value during the interview, you can be asked What is the purpose of the
untracked keyword?
This keyword defines a scope in which technique of arithmetic overflow is disabled.
It makes sense to use it in projects in which they're checking for overflow is globally enabled.
This can be searched on the product level settings.
What is a silent failure is a kind of failure that happens.
We fault and not ification to the users or developers.
They are not informed that something went wrong and the application moves on passively in an invalid
state.
What is the big integer type?
It's a numeric type that can represent an integer of any size.
It is limited only by the applications memory.
It should be used to represent gigantic numbers in most practical cases.
Long is more than enough.
The largest long is over four billion times larger than the maximal end.
It means big integer should be used instead of wrong, only to represent unthinkably large numbers.
All right, that's it for this lecture.
Thanks for watching, and I'll see you in the next one.