DEV Community

Ben Halpern
Ben Halpern Subscriber

Posted on

Convince me that types are awesome

Suppose I've mostly used untyped programming languages and don't have a great appreciation for types.

Let's do some convincing!

Latest comments (60)

Collapse
 
vdeolali profile image
Vikas Deolaliker

You can catch malicious attacks on your input forms of your web apps. Debugging is lot easier and code is easier to read.

Collapse
 
jasman7799 profile image
Jarod Smith

Depends on the project imo. Types are useful when building out enterprise applications with lots of inherent complexity and are meant to be read by many different developers. This is because it is easier to quickly deduce what a function expects, and what kinds of data you are working with. Additionally many editors like typescript can use the types to provide intellisense, and static type checking.

However types can get in the way and be overbearing in small projects maintained by one person where the complexity is small. It's just faster to code without types.

Collapse
 
arschles profile image
Aaron Schlesinger

This is gonna be a really crappy persuasive argument, but I wanted to give pros and cons of types, and you can decide for yourself 😄

Pros:

  • More self-documenting
    • what does this function expect?
    • how is it gonna use it?
    • what is it gonna return?
  • IDEs can provide richer & faster intellisense / code completion / go to definition / inline docs \
    • Check out XCode for Swift and Visual Studio for C#. Both are 🔥 at this stuff for their languages
  • Usually less boilerplate-ey tests to write
  • Some groups of runtime errors are impossible. Some examples:
    • In some languages, there's a type of array that cannot be empty, so myarray[0] will never throw
    • Others have literally no exceptions. Functions that might fail will always return a type that can either be a successful value, or an error. You have to deal with both or your code won't compile

Cons:

  • Some typed languages have horrific compiler error messages if you mess up. Good luck figuring out what you did wrong, let alone fixing it
  • Typed languages often have a steeper learning curve
    • There are exceptions on either side, of course!
    • Go is easy to pick up for lots of folks
    • ... and Ruby has magic in it that can trip folks up pretty early on in their learning
  • There's math behind type systems (no joke! "Category Theory" on wikipedia). Some languages will force you to learn some of that, even if they don't mean to
  • Some typed languages have "generics" - being able to write functions that can handle any type. It's handy and powerful, but these functions can be super confusing for the person calling the function

That's all I can think of for now. I think typed languages pay off after you get to a medium sized codebase. I usually reach for them on day 1, because I've been burned a lot by errors in dynamic languages, that typed languages don't have.

Hope this helps!

Collapse
 
danielw profile image
Daniel Waller (he/him)

I just like that it decreases mental load for me.

I do not have to think about what the inputs to my functions are, what the outputs are, whether I have to think about null checks, etc.

It just helps me write safer code faster.
Also it's very self-documenting. I can always ask my IDE what other functions I can call on the output of another.

Collapse
 
vasilvestre profile image
Valentin Silvestre

Oh so you're not only a grafikart follower lol

Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

I can see why people like them, but personally I find they slow down development and restrict creativity somewhat. All depends what you're building of course.

I always find my ideas flow into working code much, much faster using untyped vars. Doing it this way also allows for more flexible code, and encourages a better mental understanding of the code. Admittedly this works better for solo coders than for sharing code with others.

Collapse
 
ferricoxide profile image
Thomas H Jones II

Something that bit me, recently: ended up having code-squirreliness because I wasn't paying attention and kept re-typing a structure. The resulting (unintentional) transformations to the stored data made for Good Times™ debugging. Wasn't till I found an excessively-pedantic linter that I figured out where the mangling was happening.

 
gklijs profile image
Gerard Klijs

I should have explained better. Clojure is a dynamic language with optional typing. Also it's not an additional tool, it's baked into the language, and only possible because it's dynamic. The program is literally made from interpreting each statement instead of statically compiling everything at once.
There is also some power in not having types, or only optional types.
I really think it's a spectrum. Like in Java I sometimes miss the feature to make type aliases to make strings especially more safe. But 'cheating' with var in Java in selected places feels like a releave. And with Rust you are repeating some types a lot in some cases (but then you could use an alias). And typescript can be tricky when coming from Java, since it only checks compile time.

Collapse
 
bradtaniguchi profile image
Brad • Edited

When I first started programming I thought dynamic languages were faster to program in. That was until I had a decently sized project and had no idea what the hell was going on.

  1. Developing with types gives you better documentation at your fingertips since types provide better context, and auto-completion.
    • Its easier to write code
  2. Developing with types gives you more fail-safes in your code
    • Your code gets harder to screw up
    • Etc. Typos/invalid arguments/invalid syntax all explodes at the time of writing, rather than at run-time.
  3. Developing with types gives you more fail-safes when using external code
    • More reliable interactions between your code and libraries
    • Ex. Version 3 of the package had breaking changes, all you code now explodes at compile time, rather than at run-time.
  4. Refactoring massive projects becomes stupidly easy to handle.
    • Cleaner, safer, faster code refactors
    • Ex. Rename a User object's name property to fullName is as easy.
  5. Projects don't fail because "you don't write code fast enough" they may fail because you can't update your code easily enough, or your code is a mess
    • Typescript allows you to iterate on existing code faster, and safer, resulting in saved $$$
    • (see refactor example)
  6. Your probably already using some typechecking/typescript via VSCode's built in typechecking features

Make the code work for you, don't work for the code!

Collapse
 
gklijs profile image
Gerard Klijs

But what if can directly verify if the code is good, like with a Clojure Repl. It's Scenario 3, as you write is function it's detectly evaluated and you get the result back.