CSC/ECE 517 Fall 2009/wiki1b 2 SD: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 43: Line 43:
To Err is human and humans write computer programs, therefore, static typing helps us write bug free programs. It is nice to know that the compiler is there to point out mistakes in the program. Static typing also helps us make modifications to existing code. If a program does not compile, we know there is a problem in the code and we need to modify it. It also helps us use the code of other programmers, because there is a protocol on how functions should be used [4].
To Err is human and humans write computer programs, therefore, static typing helps us write bug free programs. It is nice to know that the compiler is there to point out mistakes in the program. Static typing also helps us make modifications to existing code. If a program does not compile, we know there is a problem in the code and we need to modify it. It also helps us use the code of other programmers, because there is a protocol on how functions should be used [4].


 
In the statically typed programming language communities, there is a saying that “if it compiles, it works”, this the level of reliability that static typing provides.
 
 
 
 
 
 
 
 
 
 
One of the many things that can help us make sure that our programs are safe and sound is static typing. Or at least, that’s what the static typing advocates would have us believe. With such a system, all the interactions between the different functions and variables are checked during the compilation phase and any mistake is treated as a fatal error, and the compilation stops. With static typing, it is impossible to build a program that would try to apply the length function to a number. With a dynamically typed program, the compiler wouldn’t check for that possibility, and the error would happen at run-time.
 
That sure makes static typing sound like a sweet deal! After all, humans program computers, and we’re not known for our infallibility. In fact, we’re quite the opposite: anything can disrupt our concentration and this can lead to the introduction of bugs in our software.
 
Surely, it is nice to know that the compiler is there to point out our mistakes when we’re not fully focused. Static typing also helps us make modifications to existing code: if it doesn’t compile, we know there is a problem with our modification. It also helps us use the code of other programmers, because there is a protocol on how functions should be used.
 
In the Haskell and ML communities, there is a saying: “if it compiles, it works.” This is not a fact, of course, static typing cannot help us with everything that could go wrong in our program, but it shows how dependent these programmers are on the type checker to find common mistakes (and even not so common mistakes.)


== Dynamically typed languages ==
== Dynamically typed languages ==

Revision as of 23:14, 20 September 2009

Advantages of statically typed vs. dynamically typed languages

Type System

The formal definition of a type system is[1]:

“A type system is a tractable syntactic method for proving the absence of certain program behaviors by classifying phrases according to the kinds of values they compute.”

Type system is the study of types which defines the interaction between various types, the way in which a programming language assigns types. Type systems help detecting errors in the program, provide abstraction, improve the readability of the program, and improve the efficiency of the program [1],[2]. Detecting errors related to types in a program is called type checking.


Type Checking

In layman terms type checking is the process of determining errors related to types in the expressions and calculations of a program. There are a few programming languages which are typed and there are a few which are untyped. Among the typed programming languages, based on when the type checking happens, they are either dynamically typed or statically typed. There is another factor used to classify programming languages, that is, the level to which type checking is enforced. Based on the extent to which type checking is enforced, programming languages are either strongly typed or weakly typed.

Therefore, broadly we classify programming languages as:

- Statically typed: Statically typed languages are those languages in which type checking is done at the compile time as opposed to run time. A few examples of statically typed languages are C, C++, C#, Java, FORTRAN, and Pascal etc.

- Dynamically typed: Dynamically typed languages are those set of languages where type checking is done, unlike static typing, at run time. Examples of dynamically typed languages are Ruby, Prolog, Python, SNOBOL, and Smalltalk etc.

- Strongly typed: Those set of languages where type checking is strictly enforced are called strongly typed languages. Strongly typed languages guarantee type-safety [5]. Examples of strong typing are Lisp dialects, Python, Haskell etc.

- Weakly typed: Weak typing means that the language does implicit type casting. For e.g. consider the following operations:

var a = 5;

var b = “string”;

var c = a + b;

print c;

We know that 5 is an integer and the type of ‘a’ is integer, which is different from the type of ‘b’ which is a string. Therefore, the operation a + b, should not have any value. In a weakly typed programming language, the result might be ‘5string’; this is because ‘a’ is implicitly converted into a string. Examples of weakly typed languages are JavaScript, PHP, Perl etc.

Statically typed languages

The advantages of a statically-typed programming language are that they allow errors to be detected earlier at the compile time, enforce disciplined programming styles, and generate an efficient object code [3].


In the present time Information technology has become a part and parcel of everyone’s life. We cannot imagine a day without a computer. So much are we dependent on the computer that the reliability of software programs is a huge thing. There are many factors which make a program reliable, functional and efficient; one of these factors is static typing. In a statically typed system all the function calls and variables are checked at the compile time and any error detected here is treated as a serious error and the compilation stops and the system does not allow the error to propagate to run-time. With static typing, it is impossible to build a program that would assign a string value to an integer variable. Whereas, in a dynamically typed program, the compiler wouldn’t check for this possibility, and the error would happen at run-time.

To Err is human and humans write computer programs, therefore, static typing helps us write bug free programs. It is nice to know that the compiler is there to point out mistakes in the program. Static typing also helps us make modifications to existing code. If a program does not compile, we know there is a problem in the code and we need to modify it. It also helps us use the code of other programmers, because there is a protocol on how functions should be used [4].

In the statically typed programming language communities, there is a saying that “if it compiles, it works”, this the level of reliability that static typing provides.

Dynamically typed languages

References:

[1] Pierce, B. C., & NetLibrary, I. (2002). Types and programming languages. Cambridge, Mass.: MIT Press.

[2] http://www.ljosa.com/~ljosa/teaching/cs162/lectures/L7%20-%20Types.pdf

[3] Martin Abadi et. al, Dynamic Typing in a Statically Typed Language, ACM Transactions on Programming Languages and Systems, Vol. 13, No, 2, April 1991.

[4] http://gnuvince.wordpress.com/2008/02/15/static-typing-%E2%88%A7-dynamic-typing/

[5] http://www.ljosa.com/~ljosa/teaching/cs162/lectures/L7%20-%20Types.pdf