Main Advantages of Typed Languages
The concept of data types is one of the most powerful in the study of programming languages. Many languages have been devised with the main goal of delivering a type system with improved capabilities. Some of these languages coming from research labs, such as ML and Haskell, have become successful in their own right. Other languages have tried to emulate some of the techniques developed there.
Early languages such as Fortran had a type system that was composed of very few basic types. In a sense, this mimicked the available support on assembly languages, which have commonly provided only access to integer, floating point, and character data types. However, a large number of extensions to these simple type systems have been proposed in later languages.
For example, languages such as C have since the beginning supported a type system that is a little more sophisticated than the one provided by Fortran. With support for structures, enumerations and unions, C has provided at least a feel of the options now available on higher level languages. Such facilities allowed programmers to create more complex software than it was possible with the earlier generation of languages.
Fundamental Advantages
There are a few reasons why so much effort has been spent, both in terms of research time as well as compiler development time, to improve type checking in modern programming languages. Chief among them is the idea that it is better to catch errors in a program as soon as possible. When the compiler is able to verify the correct use of types, a whole set of errors is immediately removed from the resulting program. All operations that read and modify data of a particular type are known to be valid, because the compiler had the opportunity to check the correctness of that operation.
Although this may not seem such a big result, the possibility of checking types open the doors to other guarantees that are difficult to enforce without a type system. For example, a classic application of user-defined data types is to guarantee that data is handled in a uniform way by the system. For example, an Employee data type can be used to maintain all accesses to employee-related data contained to a single object in memory. In this way, it is easy to verify the name of the employee for which the salary is being modified. Without type checking of user-defined types, it is difficult to enforce that related data is stored in the same object.
In addition, more recent languages also provide mechanisms to encapsulate related data, so that only a small number of procedures can have access to the memory object. For example, in C++ it is possible to define a member variable as private or protected, so that only member functions in a particular class (or derived classes in the case of protected members) can have proper permission to access that data. That level of protection can be enforced because of type checking provided by the C++ compiler.
Refactoring
Avoiding conversion problems at compile time is one of the advantages of type systems, but it is not the only one. Another advantage is the possibility of using automated tools more efficiently to perform such tasks as refactoring or static analysis, for instance.
Refactoring is a good example of feature that is much easier to support in the presence of a type system. The basic idea of refactoring is to provide program transformations that simplify the code, while maintaining its operation unchanged.
In a typed language it is much easier for automated tools to determine the kinds of operations that would maintain the run-time and compile-time properties of an existing program. Since the type system requires particular properties from each operation in the target code, it is relatively safe to perform simplifying operations that maintain these properties.
On the other hand, non-typed languages have a disadvantage in this respect. Due to the dynamic nature of their expressions, it is hard for an automated tool to determine the exact result of some expressions. For example, an expression in a dynamic language can legitimately return objects of different types each time it is executed. What this means is that the refactoring tool may not guarantee that a transformation is valid for all inputs.
For example, changing the type of an argument passed to a function is a simple transformation in a typed language. In a dynamic language, on the other hand, it becomes hard to determine what needs to be done when the modified function is called. Only manual checking (or a well constructed set of automated tests) can determine if the resulting program is valid.
Conclusion
Typed languages have been the subject of research in computer science for more than four decades. Such languages provide the great advantage of improved support for automated checking of code. While the standard checking allowed by simple types is limited, much more sophisticated strategies can be devised when such a system is applied to user-defined data types.
We just scratched the surface of the possibilities here, but it is useful to become aware of some of the advantages provided by programming languages such as Haskell and ML, which provide a well designed type system, and in a lesser extent to languages such as C++ and Java, which provide some of the features and advantages provided by the concept of type checking.
Similar Posts:
About the Author
Carlos Oliveira holds a PhD in Systems Engineering and Optimization from University of Florida. He works as a software engineer, with more than 10 years of experience in developing high performance, commercial and scientific applications in C++, Java, and Objective-C. His most Recent Book is Practical C++ Financial Programming.
Speed, speed, speed.
By Jean Hugues Robert (@jhr) on Nov 24, 2011