Take for instance a language like java or C++ which is a statically-typed language. Type checking in such language is performed at compile time. This is very useful as it warns the programmer of all type errors before the code executes successfully. This way, programmers are subjected to writing lesser error codes.
In C++ you would have to define a variable type before using it; You cannot assign a variable with the wrong data type. Hence, it ensures all your variables are checked to make sure they have the right datatype before the code is compiled.
For example, in C++, you would define an integer as:
int amount = 200;
this is correct and would compile successfully, then run successfully as well because 200 is an integer.
On the other hand, if you tried to declare
int amount = “200”;
this would fail at compile-time and you would have a compile-time error. Now this error is very useful as it warns you of a bug in your code before the program runs; helping you to fix it and preventing you from pushing a buggy code out for production.
var amount = 200;
then later do:
var amount = “200”
...both cases would compile successfully and even run successfully.
Now the fact that this compiles and runs successfully does not mean the code run as intended.
For a real world application, this simple bug could be a huge threat to the life of an organization.
If this code was part of a software handling some financial transactions, were the total amount in a certain user account is to be the sum of all the money coming in to the account. Then the outcome of the total amount in any user account would be wrong.
For example if:
var prevAmount = 500; var currAmount = “200” var totalAmount = prevAmount + currAmount console.log(totalAmount)
totalAmount = 500200
...This gives the user more money than should and puts the organization at risk.
Whereas an explicit conversion is a conversion stated by a programmer to change the datatype of a variable intentionally.
An example of an explicit conversion would be:
var totalAmount = prevAmount.toInt() + currAmount.toInt()
By performing the explicit conversion, each amount would be converted to an integer before addition is done.
Now you may ask:
"Why can’t I just do this and avoid using a type checker for my code".
Well you can but you would have to remember to do so in every part of your code where necessary and also, your code would still compile even though you forget to do so in some instance. This means that you are prone to getting unintended results.
We want to avoid a buggy code compiling as much as possible .
We want all errors fixed before our code finally runs. So that when it finally does, we know for sure we are not having unintended behaviors from type errors.
The differences between static type checking and dynamic type checking can be seen when a type error occurs. In a statically-typed language, type errors occur during compile time.
Whereas in dynamically-typed languages, the errors occur only once the program is executed. That is, at runtime.
On the other hand, if a program written in a statically-typed language (like Java or C++) contains a type error, it will fail to compile until the error has been fixed.