What are static and dynamic typing?

Jose Salvatierra
School of Code
Published in
2 min readFeb 9, 2016

--

Static and dynamic typed languages are concepts we come across when learning a new programming language. We might be left wondering “what is this?”, but really the meaning is quite simple.

Variables are just boxes, each with a name and some data it contains. We can then refer to the data using the name. But the data can be anything! It can be strings, or numbers, floating point numbers, or many more.

Static typing

Java is an example of a static typed language. What this means is that when defining a new variable, we must specify the type of data that the variable will hold. For example:

int x = 5;
String my_string = "Hello, world!";

Dynamic typing

Python is an example of a dynamic typed language, which means we don’t need to specify the data type of the variable when defining it; it is implied from the data we put in the box. For example:

x = 5;  # type 'int' assumed
my_string = "hello, world!" # type 'string' assumed

Problems

One of the problems with a dynamic typed language is the fact that it is easy to pass the wrong type to a method, for example. Say we have the following method in Python:

def date_formatter(date):
return datetime.datetime.strptime(date, "%m%d%Y").strftime("%d-%m-%Y")

After a few weeks, we are writing code in a different part of the program, and we call our method like so:

date_formatter(datetime.datetime.utcnow())

Python was expecting a string to parse and then format in a different way, and we gave it a datetime object.

It would give us an error, but not while we are writing it. We would have to wait until we are running the program before we know that we’ve made a mistake (a better option is to write tests and run them periodically, but that’s a topic for a later date).

So which one is better?

There is no better one. Some people prefer one over the other, but both have their place and it will all depend on where you are using it, and whether you are accustomed to using a language with that typing.

Some people would say static typed languages are better because, although slightly more time-consuming, leads you down a better path for the future and helps prevent some silly mistakes.

Others would say dynamic typed languages are better because they save time and give you more freedom.

--

--