Dynamic vs Statically typed languages

Salvador Becerra
4 min readJul 4, 2019

--

https://android.jlelse.eu/

Type checking is all about ensuring that the program is type-safe, minimizing the possibility of type errors. For eg — type error could be a situation where an operation is performed on a data of type integer with the intent that it is a float, or even something such as adding a string and an integer together (source)

Dynamic vs Statically typed languages. I’ve had some issues really understanding what these terms mean. The definitions for these terms are below

Static typing is when the data type of the variable is known at compile time.(C#, Java, Scala, F#, Haskell)

Dynamic typing is when the data type of the variable is determined at run time. (Javascript, Perl, Python, Ruby, Groovy)

But what do those definitions mean? What are the benefits / drawbacks of each typing. Lets compare 2 languages; Python, a dynamically typed language and Java, a statically typed language.

Lets walk through an example that will make this whole concept more clear.

Python is dynamic so the following code is allowed. In dynamically typed languages, variables are bound to objects at run-time by means of assignment statements, and it is possible to bind the same variables to objects of different types during the execution of the program. (source)

school = "MakeSchool"
school = 1997

In a statically typed language like Java this sequence of code is not allowed. We declared the school variable as a string first, and we cannot just change the type of that variable to an integer. In Java we would have to write the code, declaring the type first

String word;
word = 'Hello';
word = 43110; this would cause an error
http://clipart-library.com/clipart/n1194376.htm

Following this example lets talk about the difference between strongly typed languages and weakly typed languages because it relates to this subject.

In a weakly typed language we can combine variables with different types without a problem. In a strongly typed language this would not be allowed, before we are able to combine the variables we would need to convert them into the same type and then the language would allow us to combine them

#weakly-typedvariable1 = 3.14
variable2 = '3.14'
variable3 = variable1 + variable2 #this would return '3.143.14'
#strongly-typedvariable1 = 3.14
variable2 = '3.14'
variable3 = variable1 + int(variable2) #this would return 6.28

Lets talk about the advantages of each type. Since statically typed languages have you declare the type of the variable it allows a large amount of errors to be caught early in the development process. Statically typed languages also have the benefit of running faster because when its compiled it knows all the types beforehand. (source)

Dynamically typed languages have the advantage of being able to change the type of a variable on the fly which means you have to write less code, your code is less verbose and you don’t have to wait for a compiler to execute your code, which makes debugging much easier. It also makes the code more flexible compared to statically typed languages, statically typed languages tend to have more rigid objects, which might have you write a lot more changes to make sure they interact with other objects correctly. (source)

The benefits of strongly typed languages are similar to the benefits of statically typed languages. They provide constraints which help with errors during compile. Its easier to understand because you can see the type easily and helps create more optimized code.

The benefits of a weakly typed language are again similar to that of dynamically typed languages. They require less code writing because the language is automatically doing some type converting.

I hope that makes everything more clear, I certainly have a preference for Dynamic, weakly typed languages. They are easier to write for me, I don’t have to wait for code to compile and it looks cleaner to me. On the other hand, I know a lot of older devs prefer static strongly typed languages. There is more planning involved in writing code in one of those languages and everything is explicit. I personally think dynamic, weakly typed languages have made coding easier and more accessible and allows people to jump in quickly and debug quickly.

*code written by me

--

--