Language: EN

lenguajes-tipados-no-tipados

Typed and Untyped Languages

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 big change in the use of programs, and often gives rise to huge debates about which approach is better to use.

In reality, it is an eternal and somewhat meaningless debate. Both ways of working have advantages and disadvantages. 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 of them when it is more appropriate.

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 that we are going to use.

Examples of typed languages are 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 exception 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 this 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 specify the type of the variables and expressions when declaring the variable.

Examples of untyped languages are 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 variable type 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 it 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 “typed languages” vs “untyped languages” debate is almost as old as the programming world. 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 faster and more agile to program
  • Are more flexible

While typed programming languages,

  • Are more rigorous and robust, because they allow errors to be detected earlier
  • Are more suitable for large projects
  • In general, have better performance because they allow the compiler to make optimizations

Let’s see it with an example

Suppose you have to make a call to a web service, which returns the data of a user. For this, we have a function that has already been made for 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 that is “magically” returned has all the properties I need.

const response = makeAPIRequest(); // function that makes the request to the API

console.log(response.name); // Print the user's name
console.log(response.age); // Print the user's age

However, in a strongly typed language like C#, I would have to define what it will 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 request to the API

Console.WriteLine(response.Name); // Print the user's name
Console.WriteLine(response.Age); // Print the user's age

As we can see, the JavaScript code is much more agile, because it avoids having to define the type, as it was necessary in C#.

Furthermore, if at any time the API changes, the JavaScript code will still work, while in the C# code I would have to change the class.

So we see what we were saying 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#.

This is why we say that typed languages are more rigorous and less prone to failures than untyped languages.

My advice is to avoid taking sides in favor 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 context and requirements of the project. So knowing both will allow you to adapt to various situations.