In programming, there are two types of languages: typed languages and untyped languages.
The fundamental difference between them lies in how they handle data types. In particular, how variables and expressions that we are going to use are declared.
This small difference implies a significant change in the use of programs, and often gives rise to huge debates about which approach is better to use.
Typed Languages
In a typed language (also called strictly typed), they are characterized by the fact that they require us to define the specific data type of the variables and expressions that we are going to use.
Examples of typed languages include C++, C#, and Java, among many others. Let’s see some examples.
For example, this is how variables are declared in C# or Java.
int number = 5;
int result = number + 10;
string myText = "hello";
Which is very similar to what we find, for example, in C++. With the caveat that to define the text we must use the std
namespace.
int number = 5;
int result = number + 10;
std::string myText = "hello";
As we see, in a typed language we have to indicate the type of variable when declaring it, for example, an integer int
or a string string
.
Once a variable has a type, it can only contain values compatible with that type. We could not assign a text to an int
variable.
int a;
a = "hello"; // this would result in an error
Untyped Languages
On the other hand, untyped languages (also called dynamically typed) do not require us to indicate the type of the variables and expressions when declaring the variable.
Examples of untyped languages include Python and JavaScript. Let’s see some examples.
For example, in JavaScript it is not necessary to indicate the type of a variable when declaring it. We only have to use the reserved word let
to indicate that we want to create a variable.
let number = 5;
let result = number + 10;
let myText = "hello";
In Python, it is not even necessary to use a reserved word to create a variable. The variable is simply created the first time we use it.
number = 5
result = number + 10
myText = "hello"
In the vast majority of untyped languages, it is even possible to change the type of a variable once created.
let a = 5; // here 'a' contains the number 5
a = "hello"; // now 'a' contains the text "hello"
In reality, internally, untyped languages do have types. Your program needs them to know how to manipulate the values of the variables. But the programming language makes its use transparent to the user most of the time.
Advantages and Disadvantages of Each Type
The debate “typed languages” vs “untyped languages” is, almost, as old as the world of programming. Each approach has fervent supporters and passionate detractors.
The truth is that both types have their advantages and disadvantages (keep in mind that if one of them were clearly superior, the other would have disappeared long ago).
Speaking very generally, we can say that untyped programming languages,
- Are easier to learn
- Are more agile and faster to program
- Are more flexible
While typed programming languages,
- Are more rigorous and robust, because they allow for earlier error detection
- Are more suitable for large projects
- Generally have better performance because they allow the compiler to make optimizations
In reality, it is an eternal debate without much sense. In fact, both types of languages evolve to incorporate features of the other type.
The important thing is to understand the characteristics and differences between both and know how to use each one when it is most appropriate.
Let’s see it with an example
Suppose that you have to make a call to a web service, that returns user data. For this, we have a function that has already been provided to us makeAPIRequest()
, which performs the request and returns the data.
In an untyped language like JavaScript, I only have to make the request, and the object that it returns “magically” has all the properties I need.
const response = makeAPIRequest(); // function that makes the API request
console.log(response.name); // Prints the user's name
console.log(response.age); // Prints the user's age
However, in a strongly typed language like C# I would have to define what it is going to return beforehand.
// You need to define a class that matches the structure of the object returned by the API
public class UserResponse
{
public bool IsValid { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
UserResponse response = MakeAPIRequest(); // function that makes the API request
Console.WriteLine(response.Name); // Prints the user's name
Console.WriteLine(response.Age); // Prints the user's age
As we see, the code in JavaScript is much more agile, because it avoids me having to define the type, as was necessary in C#.
Moreover, if at any point the API changes, the code in JavaScript will continue to work, while in C# I will have to change the class.
So we see what we said that the untyped language is much more flexible and agile than the strongly typed language.
However, it also has its disadvantages. Imagine that the fictional change we mentioned is not compatible with part of my program. In the case of JavaScript, I would have problems detecting the failure, which I would not have in the case of C#.
It is for this reason that we say that typed languages are more rigorous and less prone to errors than untyped languages.
My advice is to avoid taking a position for or against either. Instead, try to learn at least one language of each type and, above all, understand all their similarities and differences.
Moreover, ultimately, the choice between typed and untyped languages will depend on the context and requirements of the project. So knowing both will allow you to adapt to various situations.