I hate having to write a whole bunch of type information into my codebase. I know that’s what you’re thinking as you write your Python or Javascript code. But, trust me, you will start to like types if you know how they can help you solve your problems in the long-term.

As you know, I’ve been moving from dynamic programming languages to more statically typed languages, the more I code. I can assure you of one thing, it is definitely not a fad. There are good reasons why as I write more complicated software, typed languages have become my goto. Today, we’re going to talk about those reasons and whether a typed language could be for you.

Clarity With Types

Unlike with dynamically typed languages, typed languages tend to provide more code clarity with the types. When I was working with a dynamic language, one issue that always plagued me was knowing the type of the variables I was interacting within functions or in classes. In a dynamic language, the best thing you can do is provide people with an appropriate name for your function parameters. This may help with simple functions, but more complex functions become an issue, especially if the developer who wrote the code beforehand did not use a great naming scheme. I know you all remember those college days or other code bases where everyone used parameter names like l, x, y, z, etc; this just slows down writing code for me.

One thing I notice a lot of programmers who use dynamic languages, but dislike typed languages have in common is a disdain for how locked in you are with a typed system. No longer will you be able to add extra properties on to an object or variable for free. But I can understand where they’re coming from so let’s get into that.

Types Lock In & Verbosity

I remember writing Java and C# code; I hate writing Java and C# code. I’m sure many programmers have been burned by this language in various ways. Being locked into types in your codebase or the verbosity of the language.

Many times when writing code in a statically typed language like Java or C#, I have to write a lot of type information before I get anything done. Once I set up all those types, then I still have to deal with converting them between types whenever the type signature does not match(all I wanted to do was print the contents of the class — no I do not want to write a toString method). I fully understand it sucks; the other part is verbosity.

Verbosity is when a language tends to be on the side of writing more words to get things done, whereas a language like Javascript requires much less writing. When writing Javascript code, being able to put more code in the text editor feels amazing! Later on, I’ve learned that I want to write less code. Writing more code could just lead to more errors. Verbose languages, make me consider that my code could be more error-prone. So, I understand that types can be annoying to deal with along with the verbosity, and I still think they can be extremely useful to you.

Self Documentation and Removing Silly Errors

Types help add self-documentation to the code base at a small overhead cost of writing a bit more information and remove silly errors from the codebase. I hate writing comments in my code unless absolutely necessary, and the type signature helps me write comments that must stay current, making a developer’s life easy. Also, I don’t know how many times while working in Javascript, at one point I was expecting one object with certain properties only to find out later in the program it gets replaced by a string. Types remove these kinds of errors and typos as well (If they can’t find something declared with that name, most editors will warn you in a typed language). You will know what is a string, and what is that complex RPG character class you wrote without having to refer back to the code you wrote 3 months ago to remember what type of value goes into which parameter. These are not the only benefits, however.

Improved Code Compilation & Execution Speed

Dynamic languages are great because you can receive instant feedback from your code immediately due to being interpreted. However, that usually means that the code takes a lot longer to run. Javascript is one exception because it is compiled and interpreted (read up on the JIT compiler). When the code is compiled it allows us to get down to faster speeds of execution, simply because the program does not need to do any guesswork when it comes to the program you’ve typed. This is why dynamic languages like Python are slow because a lot of extra checks have to be made in order to make sure your program runs as intended.

Types Are Not So Bad

At the end of the day, using a language with types is not so bad; the benefits far outweigh the verbosity. The short term gain of writing code quickly at the start usually ends up hurting you later as the code base grows. Having some type information, or at least type inference can greatly increase your productivity in the long term.

So, if you ever decide to start a new project any time soon, try out a language like Typescript, ReasonML, C#, or Java; experience what it is like to know with clarity what each variable and class does in your larger code bases without committing all that information to memory.

Stay tuned, we’ll be talking about the best of both worlds, when we go over type inference.

%d bloggers like this: