Saturday, December 6, 2014

Static Typing vs. Dynamic Typing

In programming, you'll hear the terms static and dynamic typing come up from time to time. These terms have a lot to do with when values are checked, whether during compile time, runtime, or when the value is accessed at runtime. Before we go into exactly what these terms mean, we have to first understand strong vs weak typing.

Strong Typing vs. Weak Typing

Strongly typed languages will throw errors in the event you try to use an object against its type. So an int must always be treated as an int and not, for example, as a string. Java is an example of a Strongly typed language. Python is also a strongly typed language. Unlike a weakly typed language like JavaScript, where you can add a string to a number and return a string, in Python, attempting this will cause your program to fail, preventing your variables from having wrong and unexpected values. Another advantage with Python, is that the exception is raised early on, at the point you violate this rule, rather than at a later point in the program.

# strong typing
# python
x = 50
y = "x"
x + y
Traceback (most recent call last):
  File "", line 1, in 
TypeError: unsupported operand type(s) for +: 'int' and 'str'
# weak typing
# javascript
var x = 50;
var y = "x";
console.log(x + y); // '50x'

Static Typing vs Dynamic Typing

A static type system involves a compilation step where the source code is examined for types, using the information to infer something about the program's behavior. Programs based on languages that are statically typed must know the variable type at the point at which it's used and sometimes require the programmer to explicitly define the variable's type during declaration.

# static typing
# java
private int speed;
public float gravity;

In a dynamically typed language like Python, the types are checked at runtime, when the object is used. With Python, variable types are inferred based on the value being assigned. This context-sensitive approach is the underlying concept of duck typing. So based on our definitions, Python is both strongly typed and dynamically typed. While dynamic typing can shorten development time, it can also provoke runtime errors that would have been caught at compile time in a statically typed language.

In Python, there is no way to declare a value without assigning it a value. In JavaScript, you're encouraged to explicitly assign values, but this is not required. Although a common practice, declaring variables without values in JavaScript comes at a cost. JavaScript will initially assign undefined to your variable and then have to do extra work down the road when you finally decide to store something into the variable.

However, JavaScript is both weakly and dynamically typed and designed for this sort of heavy lifting, which is part of why its so common to see one line variable declarations such as var x, y, radius, fillStyle; in its syntax. The other reason this is common has more to do with establishing a variable's scope outside of a given function where that value will be allocated, but that's a topic for another article.

No comments:

Post a Comment