Type checking is the stage of parsing that a compiler (or interpreter) passes through in order to ensure that data types are all being used correctly and consistently. That is, all assignments are being made correctly, functions are being passed arguments of the correct type, comparisons are only done between variables of the same time, etc. Languages like Perl and C are not very strict about this, wheras languages like Java and SML are.

Type-checking (or more correctly, static type-checking) is a compile time feature provided by some programming languages.

Apart from in pure OO languages, every piece of data in a programming language has a type. In laymans' terms, the type can be viewed as telling the computer what type of data is going to be stored in a certain place (known as a variable). For instance, it could have the type integer for numbers and string for strings of text.

Static type-checking is a feature that attempts to make sure that certain rules of how to use types are followed. As a simple example, it is clearly wrong to try and put data of type string into a space meant for an integer. As an example of why this is wrong, imagine what would happen if we tried to add two strings together, thinking they were integer numbers!

Static type checking should prevent such things ever happening by throwing an error at compile time. There are disadvantages to this feature, the biggest of which is probably added language verbosity. For any given function, you have to define the return type, arguments and exception types. This makes function definitions somewhat verbose.

There are also dangers to code reuse (something C++ pretty much gets around with templates). Because any given function is so tightly defined, it can only be used for the exact task which it was defined. Imagine we have a function print() that took an argument of type integer and we want to print a string! We'd need another function.

Despite these drawbacks, many people feel that static type-checking is worth it for the plethora of runtime errors it can help avoid. However, the polar viewpoint is that the added language complexity and verbosity can lead to more errors.

Note that type-checking also happens at run time, at least in strongly typed languages. It is important in OO systems, to see if objects can be assigned to statically-typed variables. It is important in dynamically-typed systems like LISP, to see if any given operation is possible, or if things should break now.

Log in or registerto write something here or to contact authors.