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 the variables and expressions we are going to use are declared.
This small difference represents a major change in how programs are used, and often sparks huge debates about which approach is better to use.
Typed Languages
In a typed language (also called strictly typed), they are characterized by requiring us to define the specific data type of the variables and expressions we are going to use.
Examples of typed languages are C++, C#, and Java, among many others. Let’s look at 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 can see, in a typed language we have to indicate the variable type when declaring it, for example an integer int or a text string string.
Once a variable has a type it can only contain values compatible with that type. We could not assign 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 variables and expressions when declaring the variable.
Examples of untyped languages are Python and JavaScript. Let’s look at 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 variables. But the programming language makes their 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 followers 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 typed programming languages,
- Are more rigorous and robust, because they allow errors to be detected earlier.
- Are more suitable for large projects.
- Generally, have better performance because they allow the compiler to make optimizations.
While untyped programming languages,
- Are simpler to learn.
- Are more agile and faster to program.
- Are more flexible.
It’s actually an eternal and rather pointless debate. In fact, both types of languages evolve to incorporate features from the other type.
What’s important is to understand the characteristics and differences between them and know how to use each one when it is most appropriate.
Let’s See It with an Example
Suppose you have to make a call to a web service, which returns a user’s data. For this, we have a function already given to us makeAPIRequest(), which makes the request and returns the data.
In an untyped language like JavaScript, I only have to make the request, and the object 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 first have to define what it is going to return.
// 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 can see, the JavaScript code is much more agile, because it saves me from having to define the type, as was necessary in C#.
Furthermore, if the API changes at some point, the JavaScript code will continue to work, while in the C# one I will have to change the class.
So we see what we said: 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 trouble detecting the failure, which I wouldn’t have in the case of C#.
It is for this reason that we say that typed languages are more rigorous and less prone to failures than untyped languages.
My advice is to avoid taking a position for or against. Instead, try to learn at least one language of each type and, above all, understand all their similarities and differences.
Furthermore, ultimately, the choice between typed and untyped languages will depend on the project’s context and requirements. So knowing both will allow you to adapt to various situations.
