avatarRoman Elizarov

Summary

The web content discusses the trend in modern programming languages to place types to the right of variable names, a departure from the traditional left-side placement, influenced by the rise of type inference.

Abstract

The article highlights a shift in the syntax of statically-typed programming languages, moving from the conventional practice of placing types to the left of variable names, as seen in languages like C, C++, and Java, to a more contemporary approach where types are positioned to the right. This change is particularly evident in 21st-century languages such as Scala, F#, Go, Rust, Kotlin, TypeScript, and Swift. The driving force behind this evolution is the increasing prevalence of type inference, a feature that allows the compiler to deduce the type of a variable, making explicit type declarations optional in many cases. The author suggests that this syntactic shift is not only aesthetically pleasing but also aligns with the modern practice of optionally annotating types for clarity when necessary, without disrupting the flow of code readability.

Opinions

  • The author implies that the traditional type-name style, prevalent in older programming languages, is being supplanted by a more modern approach due to advancements in language design.
  • The article posits that type inference, once a minor feature, is now a significant aspect of programming language design, influencing even established languages to adopt keywords like var and auto for type inference.
  • The author seems to appreciate the visual and cognitive flow of code where types are placed to the right, especially when complex types need to be explicitly stated.
  • There is a suggestion that the trend of right-side types may become mainstream in the future, as it is a common feature in several newly popular programming languages.
  • The author expresses a personal admiration for Java, considering it one of the greatest languages of the 21st century, despite its adherence to the traditional left-side type placement.
  • The author invites discussion on the terminology used in C++ for type inference, questioning why it is referred to as "type deduction" instead of the more commonly used term in computer science.

Types are moving to the right

If you take a look at statically-typed programming languages that are still popular today but were designed in the previous century, before the turn of the millennium, you’d notice that most of them and, at the same time, the more popular and mainstream ones, like C (circa 1972), C++ (1985), and Java (1995), write types to the left of names:

Dog   fido = ...
^^^   ^^^^
Type  Name

It reads nicely when you write a lot of declarations, too:

int count = ...
double average = ...
List<String> strings = ...
Map<Warehouse, List<OrderItem>> items = ...

However, if you take a look at modern languages, designed in 21st Century, you cannot help but notice that the languages with some popularity increasingly put types to the right of names¹:

Why is it happening? It might seem strange and inconvenient to any developer who got used to the last-century type-name style, but modern language designers still do it despite the risk of breaking with tradition. Are they all Pascal fans or what?

Here is a plausible explanation. First of all, it has nothing to do with legacy of Pascal (circa 1970) or even with Visual Basic (1991) for that matter. The real answer is that we have entered the age of type inference.

Type inference, which used to be a niche feature in programming language design, is now entering mainstream. It is appearing in our old programming languages where we can now omit types using var and auto keywords, too². Even in established programming languages we start seeing code like this:

var count = ...
var average = ...
var strings = ...
var items = ...

Woot! That’s nice and aligns, pleasure for our eyes to see. But what happens when the type is too complex for type inference or when it needs to be occasionally spelled out for human reader to understand? Behold:

var count = ...
var average = ...
var strings = ...
Map<Warehouse, List<OrderItem>> items = ...

Uh… that breaks the whole code-reading flow. So, if you are designing a programming language in the age of type inference from scratch, then you solve it by putting an optional type annotation to the right of the name:

var count = ...
var average = ...
var strings = ...
var items: Map<Warehouse, List<OrderItem>> = ...

Now it looks great again. That is essentially the way it is done in Scala (2004), F# (2005), Go (2009), Rust (2010), Kotlin (2011), TypeScript (2012), and Swift (2014) programming languages. There are many syntactic differences between them, but one thing is common — name-type order:

fido   Dog
^^^^   ^^^
Name   Type

This way of writing code is on the rise now. Is it going to become mainstream in the future? That is hard to tell for sure, but the trend does look so.

More on programming languages

Despite the age, I still consider Java to be one of the greatest languages of the 21st Century. Read more in my “Tribute to Java”.

If you liked this story then you might also like my story on “Dealing with absence of value”.

¹ ^ Technically C# (circa 2000) was released in 20th Century, but even putting it into 21st Century quadrant does not change the overall impression.

² ^ I don’t know why C++ calls it “type deduction” instead of “type inference” as it is customary in computer science and in all the other programming languages I’m aware of. If you happen to know the real reason, please respond to this story or to this tweet.

Programming
Language
Static Typing
Recommended from ReadMedium