CSC/ECE 517 Fall 2009/wiki1b 2 ps: Difference between revisions
No edit summary |
|||
(26 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
=Advantages of statically typed vs. dynamically typed languages= | |||
Type checking can be performed in programming languages either during run-time or at compile time[1]. Based on such a behavior of the [http://en.wikipedia.org/wiki/Type_system | [http://en.wikipedia.org/wiki/Type_checking#Type_checking Type checking] can be performed in [http://en.wikipedia.org/wiki/Programming_languages programming languages] either during [http://en.wikipedia.org/wiki/Run_time_%28computing%29 run-time] or at [http://en.wikipedia.org/wiki/Compile_time compile time][1]. Based on such a behavior of the [http://en.wikipedia.org/wiki/Type_system type system], the programming languages are classified as statically typed or dynamically typed. The statically typed languages like [http://en.wikipedia.org/wiki/C_(programming_language) C], [http://en.wikipedia.org/wiki/C%2B%2B C++], [http://en.wikipedia.org/wiki/C_Sharp_(programming_language) C#], [http://en.wikipedia.org/wiki/Java_(programming_language) Java], [http://en.wikipedia.org/wiki/Fortran FORTRAN] etc., eliminate the type checks to be performed every time the program is executed [1]. The condition checked once will hold good for all the future executions of the program as well. The dynamically typed languages like [http://en.wikipedia.org/wiki/PHP PHP], [http://en.wikipedia.org/wiki/Prolog_%28programming_language%29 Prolog], [http://en.wikipedia.org/wiki/Python_%28programming_language%29 Python], [http://en.wikipedia.org/wiki/Ruby_%28programming_language%29 Ruby], [http://en.wikipedia.org/wiki/Smalltalk Smalltalk] etc., perform the run-time checks uses the information from the compile time as well and hence could be more comprehensive[1]. | ||
== Typed and Untyped languages == | == Typed and Untyped languages == | ||
The “type” provides the information about the chunk of memory which holds the data and thus categorizes it such that the valid operations that can be performed on that data are defined. In [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming], the types include [http://en.wikipedia.org/wiki/Class_(computer_science) classes] (user-defined), strings and integers ([http://en.wikipedia.org/wiki/Primitive_types primitive types]), lists/arrays/vectors/hash ([http://en.wikipedia.org/wiki/Container_(data_structure) containers]) [2]. A variable in a program can accept a certain range of values during its execution. The upper bound value for that range marks the type for that particular variable. For example, if there is a variable ''num'' of type ''int'' in the program then ''num'' can accept only integer values during every execution of the program. Such languages where all the variables are associated with the data type are called | |||
The languages can defined based on how the [http://en.wikipedia.org/wiki/Data_types data types] are associated with the variables in the [http://en.wikipedia.org/wiki/Computer_program programs]. Based on this, the [http://en.wikipedia.org/wiki/Programming_languages programming languages] are categorized into [http://en.wikipedia.org/wiki/Programming_language#Typed_versus_untyped_languages typed] and [http://en.wikipedia.org/wiki/Programming_language#Typed_versus_untyped_languages untyped languages]. The “type” provides the information about the chunk of memory which holds the data and thus categorizes it such that the valid operations that can be performed on that data are defined. In [http://en.wikipedia.org/wiki/Object-oriented_programming object-oriented programming], the types include [http://en.wikipedia.org/wiki/Class_(computer_science) classes] (user-defined), strings and integers ([http://en.wikipedia.org/wiki/Primitive_types primitive types]), lists/arrays/vectors/hash ([http://en.wikipedia.org/wiki/Container_(data_structure) containers]) [2]. A variable in a program can accept a certain range of values during its execution. The upper bound value for that range marks the type for that particular variable. For example, if there is a variable ''num'' of type ''int'' in the program then ''num'' can accept only integer values during every execution of the program. Such languages where all the variables are associated with the data type are called typed languages. Certain languages do not specify separate data types for each of the variables but contains a single universal type that holds all the values. They do not define the range for the variables and are called untyped languages ([http://en.wikipedia.org/wiki/Assembly_language assembly languages]) [4]. Depending on when ([http://en.wikipedia.org/wiki/Compile_time compile time] or [http://en.wikipedia.org/wiki/Run_time_%28computing%29 run-time]) the the “type” of the variable is checked, the typed languages can be classified as statically typed or dynamically typed. | |||
== Type Checking == | == Type Checking == | ||
Type checking is done only for the [http://en.wikipedia.org/wiki/Programming_language#Typed_versus_untyped_languages typed languages]. All the variables and expressions in the program are associated with the data type. Errors might occur due the explicit or implicit mismatch of the type information. Type checking identifies the errors that may or may not produce wrong computations in the program [3]. In order to eliminate these errors the programming languages have the [http://en.wikipedia.org/wiki/Type_system | |||
Type checking is done only for the [http://en.wikipedia.org/wiki/Programming_language#Typed_versus_untyped_languages typed languages]. All the variables and expressions in the program are associated with the [http://en.wikipedia.org/wiki/Data_types data type]. Errors might occur due the explicit or implicit mismatch of the type information. Type checking identifies the errors that may or may not produce wrong computations in the program [3]. In order to eliminate these errors the [http://en.wikipedia.org/wiki/Programming_languages programming languages] have the [http://en.wikipedia.org/wiki/Type_system type systems]. Type system is programming language specific and defines the rules of how the typed programs should behave. The programs which do not follow these rules produce error [1]. The type system performs the static check on the statically typed languages and dynamic check on the dynamically typed languages. | |||
== Statically typed languages == | == Statically typed languages == | ||
In statically typed languages, types are associated with the variables or the expressions and not the values. The type checking is performed at the compile time and thus many types of errors can be identified in the early phases of the development cycle [1]. Consider the code of the program below. It shows the illegal statements in the code the static-type checker will identify. | |||
In statically typed languages, types are associated with the variables or the expressions and not the values. The type checking is performed at the [http://en.wikipedia.org/wiki/Compile_time compile time] and thus many types of errors can be identified in the early phases of the development cycle [1]. Consider the code of the program below. It shows the illegal statements in the code the static-type checker will identify. | |||
<p> | <p> | ||
<pre> | <pre> | ||
Line 19: | Line 24: | ||
</p> | </p> | ||
=== Advantages and Disadvantages === | |||
If the static type checks are performed once for a program, there is no necessity to perform the check at every execution of the program. Thus the execution can be efficient by eliminating the [http://en.wikipedia.org/wiki/Run-time run-time] checks. Certain programs may exhibit a desired behavior at run-time but since the static type checkers do not have the type information that might be available only during run-time, they might consider identify errors is such programs even though they might be resolved at run-time[1]. For example, consider the code in a program – | |||
If the static type checks are performed once for a program, there is no necessity to perform the check at every execution of the program. Thus the execution can be efficient by eliminating the run-time checks. Certain programs may exhibit a desired behavior at run-time but since the static type checkers do not have the type information that might be available only during run-time, they might consider identify errors is such programs even though they might be resolved at run-time[1]. For example, consider the code in a program – | |||
<p> | <p> | ||
Line 32: | Line 36: | ||
</pre> | </pre> | ||
</p> | </p> | ||
Though the expression might always return true at run-time, the type error will be thrown by the static type checker as expression will not be evaluated at the compile time and there is no way for the type checker to know that the execution will not perform the else part of the code. However, if the expression randomly evaluates to false, the static type checking could be of great advantage [1]. | |||
== Dynamically typed languages == | == Dynamically typed languages == | ||
In dynamically typed languages, the types are associated with the values. The run-time checker identifies the type errors at the run time [1]. Consider the code of the program below, since the type information is not known at the compile time, the addition of the character and an integer will result in run-time error. | |||
In dynamically typed languages, the types are associated with the values. The [http://en.wikipedia.org/wiki/Run-time run-time] checker identifies the type errors at the run time [1]. Consider the code of the program below, since the type information is not known at the [http://en.wikipedia.org/wiki/Compile_time compile time], the addition of the character and an integer will result in run-time error. | |||
<p> | <p> | ||
<pre> | <pre> | ||
a = [‘x’, 1, 2] | |||
b = a[0] + a[1] #Type Error | |||
</pre> | </pre> | ||
</p> | </p> | ||
=== Advantages and Disadvantages=== | === Advantages and Disadvantages=== | ||
The dynamic type-checker offers more flexibility to the programming language as types and functionality can be generated on the data obtained during run-time [1]. Also it accepts the code often invalidated by the static-type checker and executes it. Though the checker checks for the syntactic correctness of the code, it might not perform all the checks that are performed by the static-type checker. Nevertheless, it incorporates both the static-checking and dynamic-checking functionality. The checks are exclusively for the conditions which are part of the execution of the program and it holds good for all the executions of the same program [1]. The dynamic type-checking might be slower as more emphasis is on the optimization of the libraries and improvisations of the algorithms hence the efficient coding tricks might not be completely taken care of [5]. While evaluating an expression, sometimes the value might have been passed to the wrong type and computation might produce erroneous results. If this error occurs long after the mistake was made in the code, bug location might be difficult [1]. | |||
The dynamic type-checker offers more flexibility to the programming language as types and functionality can be generated on the data obtained during run-time [1]. Also it accepts the code often invalidated by the static-type checker and executes it. Though the checker checks for the syntactic correctness of the code, it might not perform all the checks that are performed by the static-type checker. Nevertheless, it incorporates both the static-checking and dynamic-checking functionality. The checks are exclusively for the conditions which are part of the execution of the program and it holds good for all the executions of the same program [1]. The dynamic type-checking might be slower as more emphasis is on the optimization of the libraries and improvisations of the [http://en.wikipedia.org/wiki/Algorithm algorithms] hence the efficient coding tricks might not be completely taken care of [5]. While evaluating an expression, sometimes the value might have been passed to the wrong type and computation might produce erroneous results. If this error occurs long after the mistake was made in the code, bug location might be difficult [1]. | |||
== Comparison of statically typed and dynamically typed languages == | == Comparison of statically typed and dynamically typed languages == | ||
One of the key features of the [http://en.wikipedia.org/wiki/Dynamically_typed_language#Dynamic_typing dynamically typed] language that the [http://en.wikipedia.org/wiki/Statically_typed_language#Static_typing statically typed] language lacks is [http://en.wikipedia.org/wiki/Duck_typing duck typing]. In duck typing, when the methods are invoked the aspects of the object invoking that method is important and not the type of the object. In languages that do not allow duck typing, if there are functions ''f1'' and ''f2'' defined in ''Class A'', then the object of type ''Class A'' can invoke the functions ''f1'' and ''f2''. However, in languages that do allow duck-typing, an object of any type (not necessarily of type ''Class A'') can call the functions ''f1'' and ''f2''. Consider the code below of an dynamically typed [http://en.wikipedia.org/wiki/Object-oriented_language object-oriented language], the function ''add'' can be invoked by the objects which do not have any related inheritance (numbers and strings). Thus, duck-typing allows [http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming polymorphism] without [http://en.wikipedia.org/wiki/Inheritance_%28computer_science%29 inheritance] [9]. | |||
<p> | |||
<pre> | |||
class A{ | |||
int add(x, y){ | |||
return (x+y); | |||
} | |||
} | |||
num = add (10, 20); | |||
str = add('A', 'B'); | |||
puts num; | |||
puts str; | |||
Output: | |||
30 | |||
AB | |||
</pre> | |||
</p> | |||
Statically typed languages are considered to be [http://en.wikipedia.org/wiki/Strong_typing strongly typed] as it prevents the program from running if it has errors in the code and dynamically typed languages are [http://en.wikipedia.org/wiki/Weakly_typed weakly typed] as programs are allowed to run even if they have errors in them and they are detected during run-time. | |||
Though the static-checking is done for the statically typed languages, many languages postpone the type checks to the [http://en.wikipedia.org/wiki/Run_time_%28computing%29 run-time]. The static checker mainly builds on the correctness of the code that the developer writes. If the type information is not provided by the developer to the type system, the static checker cannot detect some of the common errors in the code. For example, if the variable is declared to be of the type ''int'' by the programmer and as per the program logic, if it should be of type ''float'' the type checker will not detect it. In statically typed languages, since the type declaration has to be done explicitly, if the data type is changed for a variable at a single place in the code, then the corresponding changes have to be made everywhere in the code with respect to the logic and usage. Consider the [http://en.wikipedia.org/wiki/Java Java] code below - say a variable ''test'' is declared of type ''String'' and the in-built method of ''String'' class, ''toLowerCase()'' is used to convert ''test'' to lowercase. If ''test'' is changed to type ''int'', the developer has to manually change/delete ''test.toLowercase()'' in the code. | |||
<p> | |||
<pre> | |||
#Java code | |||
String test = "ABC"; | |||
#code | |||
test.toLowerCase(); | |||
</pre> | |||
</p> | |||
If the above scenario spans across multiple files, it becomes tedious and makes the code error-prone. Hence static type systems are not flexible and difficult to work with[6]. | |||
Some of the statically typed languages require lot of type declarations and in most cases, more declarations than necessary. This makes coding more time-consuming and also very difficult to make changes[7]. This could pose a problem while writing reusable software and also reduces the agility of the programming language. Thus, dynamically typed languages are best suited for the [http://en.wikipedia.org/wiki/Rapid_application_development rapid application development]. However, static-checking makes the software more reliable as they find errors at compile time[6]. | |||
Static type systems help in producing readable software and this corresponds to the self-documentation of the source code. Since the type information is present in the code, it is easier for the developer to walk through the code when compared to the dynamically typed languages where the code might get confusing [7]. Since the variables are not associated with the data type in dynamically typed languages, while reading the code it would be difficult for the developer to know the association between the variables and the data types. The static type system helps in providing the developers with intelligent development tools that have various features like auto-completion ([http://en.wikipedia.org/wiki/Intellisense intellisense]) and other analysis tools that makes programming easier [8]. | |||
If the compiler can determine the exact types that are used in the code, it can generate the machine code with optimizations. Hence the compiled code of the statically typed languages executes more quickly. For this reason, certain dynamically typed languages like Common Lisp provide the optional type declarations facility for the optimizations [1]. In dynamically typed languages the type checking is not performed until | Though the statically typed languages are much faster than the dynamically typed languages as in the latter type checking is done at run-time, currently the memory and performance are not the major issues for most of the applications (with the exception of [http://en.wikipedia.org/wiki/Embedded_systems embedded systems] on large application servers) and dynamically typed languages cannot have disadvantages due to performance concerns. Instead, dynamic type systems give the developers the boost with the reduced development time [6]. | ||
With respect to the [http://en.wikipedia.org/wiki/Software_testing testing], static type systems perform better than dynamic type systems. The static systems can many a times tell if the changes to the code has broken any other piece of code. Thus in certain cases testing might not even be required [7]. It is fairly effective to find type errors in code through static type checking than the [http://en.wikipedia.org/wiki/Code_coverage code coverage] tests with 100% code coverage [1]. | |||
If the [http://en.wikipedia.org/wiki/Compiler compiler] can determine the exact types that are used in the code, it can generate the [http://en.wikipedia.org/wiki/Machine_code machine code] with [http://en.wikipedia.org/wiki/Optimization_%28computer_science%29 optimizations]. Hence the compiled code of the statically typed languages executes more quickly. For this reason, certain dynamically typed languages like [http://en.wikipedia.org/wiki/Common_Lisp Common Lisp] provide the optional type declarations facility for the [http://en.wikipedia.org/wiki/Optimization_(computer_science) optimizations] [1]. In dynamically typed languages the type checking is not performed until [http://en.wikipedia.org/wiki/Run-time run-time] and hence it has very less code to visit and this reduces the time and effort involved in editing, compiling, testing and [http://en.wikipedia.org/wiki/Debugging debugging] the code [1]. | |||
Static typing provides effective [http://en.wikipedia.org/wiki/Code_refactoring re-factoring] of the code. For instance, the programming environment of the statically typed languages can analyze the source code and all the callers of a particular method. Using the tool, the method could be renamed throughout the source code at all of its occurrences. This is difficult to accomplish in the dynamically typed languages as the reference of name is determined only during run-time [1]. | |||
Based on the above discussions, features of [http://en.wikipedia.org/wiki/Type_system type systems] can be categorized as follows [1]: | |||
<table border="1"> | |||
<tr> | |||
<th> Features </th> <th> Statically typed language </th> <th> Dynamically typed languages </th> | |||
</tr> | |||
<tr> | |||
<td> Safety </td> <td> Type errors are found at compile time thus more reliable. </td> <td> Type errors are found at run time, less reliable. </td> | |||
</tr> | |||
<tr> | |||
<td> Optimization </td> | |||
<td> Provides compile code.Execution is faster as complied machine code is readily available. </td> | |||
<td> Optimization is option in some languages but its done at run time. Its faster.</td> | |||
</tr> | |||
<tr> | |||
<td> Documentation </td> | |||
<td> More expressive, easy to understand for the developers. </td> | |||
<td> Not structured, confuses the developer. </td> | |||
</tr> | |||
<tr> | |||
<td> Performance </td> | |||
<td> Time consuming and less flexibility.</td> | |||
<td> Reduced development time.Suitable for rapid application development. </td> | |||
</tr> | |||
</table> | |||
Statically and dynamically typed languages have their own advantages and disadvantages and which one to choose depends on the application one needs to develop and various other factors. [http://www.ferg.org/projects/python_java_side-by-side.html Java vs. Python] comparison gives us the idea of how the above languages differ in terms of type information, verbosity etc. | |||
== References == | == References == | ||
[1] http://en.wikipedia.org/wiki/Type_system | |||
[1] http://en.wikipedia.org/wiki/Type_system <br> | |||
[2] http://www.artima.com/weblogs/viewpost.jsp?thread=7590 <br> | [2] http://www.artima.com/weblogs/viewpost.jsp?thread=7590 <br> | ||
[3] http://www.cs.aau.dk/~normark/prog3-03/html/notes/fu-intr-1-show-types-and-check-1.html <br> | [3] http://www.cs.aau.dk/~normark/prog3-03/html/notes/fu-intr-1-show-types-and-check-1.html <br> | ||
Line 66: | Line 144: | ||
[7] http://www.pphsg.org/cdsmith/types.html <br> | [7] http://www.pphsg.org/cdsmith/types.html <br> | ||
[8] http://www.martinfowler.com/bliki/DynamicTyping.html | [8] http://www.martinfowler.com/bliki/DynamicTyping.html | ||
[9] http://en.wikipedia.org/wiki/Duck_typing | |||
==External Links== | |||
[1] http://en.wikipedia.org/wiki/Ruby_(programming_language)<br> | |||
[2] http://lambda-the-ultimate.org/node/100 <br> | |||
[3] http://en.wikipedia.org/wiki/Programming_language#Typed_versus_untyped_languages <br> | |||
[4] http://livedocs.adobe.com/flex/3/html/help.html?content=03_Language_and_Syntax_09.html <br> |
Latest revision as of 02:34, 29 September 2009
Advantages of statically typed vs. dynamically typed languages
Type checking can be performed in programming languages either during run-time or at compile time[1]. Based on such a behavior of the type system, the programming languages are classified as statically typed or dynamically typed. The statically typed languages like C, C++, C#, Java, FORTRAN etc., eliminate the type checks to be performed every time the program is executed [1]. The condition checked once will hold good for all the future executions of the program as well. The dynamically typed languages like PHP, Prolog, Python, Ruby, Smalltalk etc., perform the run-time checks uses the information from the compile time as well and hence could be more comprehensive[1].
Typed and Untyped languages
The languages can defined based on how the data types are associated with the variables in the programs. Based on this, the programming languages are categorized into typed and untyped languages. The “type” provides the information about the chunk of memory which holds the data and thus categorizes it such that the valid operations that can be performed on that data are defined. In object-oriented programming, the types include classes (user-defined), strings and integers (primitive types), lists/arrays/vectors/hash (containers) [2]. A variable in a program can accept a certain range of values during its execution. The upper bound value for that range marks the type for that particular variable. For example, if there is a variable num of type int in the program then num can accept only integer values during every execution of the program. Such languages where all the variables are associated with the data type are called typed languages. Certain languages do not specify separate data types for each of the variables but contains a single universal type that holds all the values. They do not define the range for the variables and are called untyped languages (assembly languages) [4]. Depending on when (compile time or run-time) the the “type” of the variable is checked, the typed languages can be classified as statically typed or dynamically typed.
Type Checking
Type checking is done only for the typed languages. All the variables and expressions in the program are associated with the data type. Errors might occur due the explicit or implicit mismatch of the type information. Type checking identifies the errors that may or may not produce wrong computations in the program [3]. In order to eliminate these errors the programming languages have the type systems. Type system is programming language specific and defines the rules of how the typed programs should behave. The programs which do not follow these rules produce error [1]. The type system performs the static check on the statically typed languages and dynamic check on the dynamically typed languages.
Statically typed languages
In statically typed languages, types are associated with the variables or the expressions and not the values. The type checking is performed at the compile time and thus many types of errors can be identified in the early phases of the development cycle [1]. Consider the code of the program below. It shows the illegal statements in the code the static-type checker will identify.
int num; num = “Assign a string”; //Type error ArrayList aList = new ArrayList(); //Collection from Java int array[] = aList; //Type Error
Advantages and Disadvantages
If the static type checks are performed once for a program, there is no necessity to perform the check at every execution of the program. Thus the execution can be efficient by eliminating the run-time checks. Certain programs may exhibit a desired behavior at run-time but since the static type checkers do not have the type information that might be available only during run-time, they might consider identify errors is such programs even though they might be resolved at run-time[1]. For example, consider the code in a program –
int x; if (expression is true) then <perform computation> else x = “Error” // int cannot take the string “Error” – type error
Though the expression might always return true at run-time, the type error will be thrown by the static type checker as expression will not be evaluated at the compile time and there is no way for the type checker to know that the execution will not perform the else part of the code. However, if the expression randomly evaluates to false, the static type checking could be of great advantage [1].
Dynamically typed languages
In dynamically typed languages, the types are associated with the values. The run-time checker identifies the type errors at the run time [1]. Consider the code of the program below, since the type information is not known at the compile time, the addition of the character and an integer will result in run-time error.
a = [‘x’, 1, 2] b = a[0] + a[1] #Type Error
Advantages and Disadvantages
The dynamic type-checker offers more flexibility to the programming language as types and functionality can be generated on the data obtained during run-time [1]. Also it accepts the code often invalidated by the static-type checker and executes it. Though the checker checks for the syntactic correctness of the code, it might not perform all the checks that are performed by the static-type checker. Nevertheless, it incorporates both the static-checking and dynamic-checking functionality. The checks are exclusively for the conditions which are part of the execution of the program and it holds good for all the executions of the same program [1]. The dynamic type-checking might be slower as more emphasis is on the optimization of the libraries and improvisations of the algorithms hence the efficient coding tricks might not be completely taken care of [5]. While evaluating an expression, sometimes the value might have been passed to the wrong type and computation might produce erroneous results. If this error occurs long after the mistake was made in the code, bug location might be difficult [1].
Comparison of statically typed and dynamically typed languages
One of the key features of the dynamically typed language that the statically typed language lacks is duck typing. In duck typing, when the methods are invoked the aspects of the object invoking that method is important and not the type of the object. In languages that do not allow duck typing, if there are functions f1 and f2 defined in Class A, then the object of type Class A can invoke the functions f1 and f2. However, in languages that do allow duck-typing, an object of any type (not necessarily of type Class A) can call the functions f1 and f2. Consider the code below of an dynamically typed object-oriented language, the function add can be invoked by the objects which do not have any related inheritance (numbers and strings). Thus, duck-typing allows polymorphism without inheritance [9].
class A{ int add(x, y){ return (x+y); } } num = add (10, 20); str = add('A', 'B'); puts num; puts str; Output: 30 AB
Statically typed languages are considered to be strongly typed as it prevents the program from running if it has errors in the code and dynamically typed languages are weakly typed as programs are allowed to run even if they have errors in them and they are detected during run-time.
Though the static-checking is done for the statically typed languages, many languages postpone the type checks to the run-time. The static checker mainly builds on the correctness of the code that the developer writes. If the type information is not provided by the developer to the type system, the static checker cannot detect some of the common errors in the code. For example, if the variable is declared to be of the type int by the programmer and as per the program logic, if it should be of type float the type checker will not detect it. In statically typed languages, since the type declaration has to be done explicitly, if the data type is changed for a variable at a single place in the code, then the corresponding changes have to be made everywhere in the code with respect to the logic and usage. Consider the Java code below - say a variable test is declared of type String and the in-built method of String class, toLowerCase() is used to convert test to lowercase. If test is changed to type int, the developer has to manually change/delete test.toLowercase() in the code.
#Java code String test = "ABC"; #code test.toLowerCase();
If the above scenario spans across multiple files, it becomes tedious and makes the code error-prone. Hence static type systems are not flexible and difficult to work with[6].
Some of the statically typed languages require lot of type declarations and in most cases, more declarations than necessary. This makes coding more time-consuming and also very difficult to make changes[7]. This could pose a problem while writing reusable software and also reduces the agility of the programming language. Thus, dynamically typed languages are best suited for the rapid application development. However, static-checking makes the software more reliable as they find errors at compile time[6].
Static type systems help in producing readable software and this corresponds to the self-documentation of the source code. Since the type information is present in the code, it is easier for the developer to walk through the code when compared to the dynamically typed languages where the code might get confusing [7]. Since the variables are not associated with the data type in dynamically typed languages, while reading the code it would be difficult for the developer to know the association between the variables and the data types. The static type system helps in providing the developers with intelligent development tools that have various features like auto-completion (intellisense) and other analysis tools that makes programming easier [8].
Though the statically typed languages are much faster than the dynamically typed languages as in the latter type checking is done at run-time, currently the memory and performance are not the major issues for most of the applications (with the exception of embedded systems on large application servers) and dynamically typed languages cannot have disadvantages due to performance concerns. Instead, dynamic type systems give the developers the boost with the reduced development time [6].
With respect to the testing, static type systems perform better than dynamic type systems. The static systems can many a times tell if the changes to the code has broken any other piece of code. Thus in certain cases testing might not even be required [7]. It is fairly effective to find type errors in code through static type checking than the code coverage tests with 100% code coverage [1].
If the compiler can determine the exact types that are used in the code, it can generate the machine code with optimizations. Hence the compiled code of the statically typed languages executes more quickly. For this reason, certain dynamically typed languages like Common Lisp provide the optional type declarations facility for the optimizations [1]. In dynamically typed languages the type checking is not performed until run-time and hence it has very less code to visit and this reduces the time and effort involved in editing, compiling, testing and debugging the code [1].
Static typing provides effective re-factoring of the code. For instance, the programming environment of the statically typed languages can analyze the source code and all the callers of a particular method. Using the tool, the method could be renamed throughout the source code at all of its occurrences. This is difficult to accomplish in the dynamically typed languages as the reference of name is determined only during run-time [1].
Based on the above discussions, features of type systems can be categorized as follows [1]:
Features | Statically typed language | Dynamically typed languages |
---|---|---|
Safety | Type errors are found at compile time thus more reliable. | Type errors are found at run time, less reliable. |
Optimization | Provides compile code.Execution is faster as complied machine code is readily available. | Optimization is option in some languages but its done at run time. Its faster. |
Documentation | More expressive, easy to understand for the developers. | Not structured, confuses the developer. |
Performance | Time consuming and less flexibility. | Reduced development time.Suitable for rapid application development. |
Statically and dynamically typed languages have their own advantages and disadvantages and which one to choose depends on the application one needs to develop and various other factors. Java vs. Python comparison gives us the idea of how the above languages differ in terms of type information, verbosity etc.
References
[1] http://en.wikipedia.org/wiki/Type_system
[2] http://www.artima.com/weblogs/viewpost.jsp?thread=7590
[3] http://www.cs.aau.dk/~normark/prog3-03/html/notes/fu-intr-1-show-types-and-check-1.html
[4] http://www.eecs.umich.edu/~bchandra/courses/papers/Cardelli_Types.pdf
[5] http://tratt.net/laurie/research/publications/papers/tratt__dynamically_typed_languages.pdf
[6] Laurence Tratt, Roel Wuyts, "Guest Editors' Introduction: Dynamically Typed Languages", IEEE Software, vol. 24, no. 5, pp. 28-30, Sep./Oct. 2007
[7] http://www.pphsg.org/cdsmith/types.html
[8] http://www.martinfowler.com/bliki/DynamicTyping.html
[9] http://en.wikipedia.org/wiki/Duck_typing
External Links
[1] http://en.wikipedia.org/wiki/Ruby_(programming_language)
[2] http://lambda-the-ultimate.org/node/100
[3] http://en.wikipedia.org/wiki/Programming_language#Typed_versus_untyped_languages
[4] http://livedocs.adobe.com/flex/3/html/help.html?content=03_Language_and_Syntax_09.html