DEV Community

Cover image for Why learn... a statically typed language?

Why learn... a statically typed language?

David Wickes on March 21, 2019

Most people's first programming language is a dynamically typed, interpreted language - JavaScript, Python or Ruby. These are excellent languages t...
Collapse
 
stojakovic99 profile image
Nikola Stojaković • Edited

Great article. I'm a big advocate of static typing as it stops many of the potential bugs even before code hits the production (right now I'm using TypeScript on my job for writing back-end of our current project). Sadly, people get too emotional about their first languages and tend not to use right tools for the job.

Collapse
 
biros profile image
Boris Jamot ✊ /

Thanks for this interesting article!

I just switched from PHP to Golang and I must admit that I feel a lot better now. In PHP, I used many linters and unit tests to ensure the quality of my code whereas in Go it's just built-in.

The developer experience is also incredibly better as IDEs can detect errors even before the compilation, without any third-party tools. The code is also easier to browse.

The only drawback is the compilation step, but in a language like Go, compilation is so fast that you can even just use go run ./... without any perceptible overhead.

Collapse
 
rhymes profile image
rhymes

In PHP, I used many linters and unit tests to ensure the quality of my code whereas in Go it's just built-in.

That's a bit of a trap, you should still write tests, but you don't need to guard against types most of the time, that's for sure. Go still has the interface{} escape route so you might have to do some type checking at runtime once in a while.

You can setup linters as well, with golangci-lint for example.

Collapse
 
bwhitelaw24 profile image
Ben W

what are the advantages of using golangci-lint over go lint and go vet?

Thread Thread
 
rhymes profile image
rhymes

They are both included in golangci-lint. It packs multiple linters for different aspects, check out the Readme in the link

Collapse
 
biros profile image
Boris Jamot ✊ /

When I said that the tools are built-in, I meant it's part of the SDK. I was not saying it's automatic or whatever. 😊

Collapse
 
gypsydave5 profile image
David Wickes

Thanks for the feedback Boris - yes, Go really is a lot of fun, isn't it?

Collapse
 
biros profile image
Boris Jamot ✊ /

I wouldn't say that. It better suits my developer experience but there are still some "pin in the ass" behaviors.

Collapse
 
mateusz__be profile image
Mateusz Bełczowski

That's exactly what I'm doing at the moment. I love Python, but I've realized that by learning statically typed language I can become better programmer in general. That's why I started to learn Rust :)

Collapse
 
aminmansuri profile image
hidden_dude

Static typing still rules in many parts of the industry. It was interesting to see an article advocating for static typing vs one advocating for dynamic typing (which is what I've seen more often).

I believe the world is moving towards a major change. In the past all introductory languages in most universities was statically typed (with the honorable exception of MIT and friends that went the Scheme/Racket first route). Most universities used languages such as Pascal, then C/C++, then Java or even C# (others include Ada even). So a great amount of developers learned this first and it was part of their natural thought process.

However, these days for the first time many many developers are learning Python as their first language, then using other dynamic languages like Javascript so contact with static typing is a bit more limited.

I seriously wonder what the long term effect of that will be. Are we looking at a future where dynamic typing becomes a new norm? (as this generation begins to have more powers to decide?)

I don't really know but its an interesting question. Smalltalkers and Lispers of the past had often argued that static typing was limiting and too verbose. Most of the industry disagreed. But lets see where we go in the years to come.

Collapse
 
gypsydave5 profile image
David Wickes

Oddly enough, I think that we're seeing more of a return to static typing at the moment. The best evidence I have for this is the turn that's happened in the last few years from Ruby towards Rust / Go.

A few years back the Ruby community was flooded with Java developers 'escaping' to a simpler world. And it was simpler and more powerful - Ruby's reflection and type coercion makes a lot of dumb tasks a lot quicker. And you never have to compile it! What could be better?

Fast forward a few years and now all those Ruby developers are 'escaping' to Go (and Rust). All the benefits of static typing that they've been missing in Ruby are available, but now the compiler is fast and the tooling is amazing.

I think the future is going to be languages with ways of checking correctness built in, and explicit typing (and type checking) is going to be a part of that. But who knows.

Collapse
 
swfisher profile image
Sam Fisher

I think the folks that start off with dynamically typed languages will likely naturally come upon on the need for a type system as they try to more carefully design their production systems. This has been my experience.

I think the dynamic languages are simply a better introduction to programming because they subtract a layer of complexity and allow you to focus on the basics.

Collapse
 
n4bb12 profile image
Abraham Schilling

Which language(s) do you think make the best introduction to static typing?

I think TypeScript is really a great choice for this.

It has smart inference, can be configured to be less strict and you can always opt-out with "any" in case you need to.

It has many features that reduce verbosity, like "typeof" or "infer" to derive types from existing code.

Take for instance this snippet from above, I'd shorten it to:

const five = 5
const banana = "banana"

function add(n1: number, n2: number) {
    return n1 + n2
}

add(five, banana)

Now it looks almost like plain JS and produces the same error.

Collapse
 
bchhun profile image
Bernard Chhun

Typings is also useful for our coworkers to jump into a project or future us when we dive back into an old project.

I find it much easier to re-figure out how everything is tied together when there are typings around.

Collapse
 
rhymes profile image
rhymes

Loved the article David, and agree with the conclusion. Go is more or less becoming a general purpose statically typed language with a really fast compilation step. The syntax is quite simple, it has type inference so it's not required to be verbose about types all the time.

Collapse
 
swfisher profile image
Sam Fisher • Edited

Thanks for the article! I liked your look at the tradeoffs.

I write node.js applications and try to keep a very airtight standard of correctness; so my first statically typed language is typescript!

The funny thing about the “world” part you noted is that in dynamically typed languages (JavaScript) implementing input validation for API endpoints still takes a great deal of work. However, with io-ts I’m looking forward to using the types as validators. Of course this won’t check everything my custom validator would, but it seems like a useful start! lorefnon.tech/2018/03/25/typescrip...

Collapse
 
cesee profile image
Cesar Maroun • Edited

Nice article.

I think it should be very obvious to any programmer that the benefits of static typing and compilation outweigh greatly the inconveniences resulting from the laziness we all have.

After all, how often do you ever want to assign a number to a variable and then assign a list of strings to the same variable? Or why wouldn't want to get an error before it happens live ? Then again, it must be for the same reason some developers do strange things like not throw an exception when something unexpected happens (I don't understand that logic either).

TypeScript - for example - gave super powers to javascript and coding is suddenly fun and productive.

TypeScript gives back the security that you lost in javascript where every tiny mistake can make you waste many hours if your application is more than a few hundred lines of code. It is a great example of why static typing and compilation is a must for every good code base.
Of course TypeScript added many other things like easier inheritance which contributed to much better code, but enforcement of type checking and compilation were a huge improvement for the whole team.

Collapse
 
eanx profile image
Zack Z. • Edited

Really great article!

My first exposure to typed programming languages was C++ in college. Afterward, I did a lot of work with PHP, Python, JavaScript and Ruby. It wasn't until I had to learn Go last year that I really embraced statically-typed languages.

I found that the code I ended up writing had far fewer runtime errors because I didn't have to worry about an incorrect type being passed into a function. Although this could also be because I've gotten a lot better at testing as well. I echo your recommendation for Go for the reasons you mentioned as well as some of the other benefits it has around distributing your finished product and the ease with which it supports other platforms and processors, like the Raspberry Pi for instance.

Collapse
 
juliang profile image
Julian Garamendy

Great article! Thanks for writing this.

Perhaps this helps: repl.it/languages

Collapse
 
vinceramces profile image
Vince Ramces Oliveros

Great article. I cant think of a graph which dynamically typed languages have learning curve to be easy, but it goes down to complexity, and vice versa to statically typed languages