Pragmatic types: types vs tests
Tags: flow, typescript, javascript, pragmatictypes
Published: 2018-09-01 23:00:17

---
title: Pragmatic types: types vs tests
published: true
description: Do you need write tests if you use type system? How type systems and tests compare? Do you need to care about types if you have an extensive test suit?
tags: [flow, typescript, javascript, pragmatictypes]
cover_image: https://thepracticaldev.s3.amazonaws.com/i/wwecj5dw2wbshlx74din.png
---

Types and tests have one thing in common, they prevent bugs in software.

What are the tests? This is the application you write to make sure that your actual software doesn't have some class of bugs and to make sure no such bugs will be introduced in the future.

What are type systems? Static type checker is an application that checks that your actual software doesn't have some class of bugs and to make sure no such bugs will be introduced in the future. 

*Side note: dynamic type systems combined with error tracking software also helps to discover bugs, but generally doesn't prevent an introduction of bugs in the future. Dynamic type system rather helps to catch the error, where it actually happens instead of down the stack. Have you ever seen on the webpage "NaN something" or "Blah-blah undefined" showed to the end user? Welp this is because nothing stopped error to propagate down the stack.*

## How types and tests bug prevention compares?

With tests, you can check almost* any type of errors, but the issue here is that you check one thing at once. So tests poking universe of all possible error here and there.
On the other hand, the type system can check that the whole class of some errors are absent, but it is limited in what class of errors it can check. Depending on the power of the type system and how it is used it can check more or fewer classes of errors.

Simplified schema showed below:

![](https://thepracticaldev.s3.amazonaws.com/i/veud7eoptty10opiig5o.png)

## A way to improve type system effectiveness and other error prevention methodologies

There are ways to improve type systems effectiveness, for example:
- [Making impossible state impossible](https://dev.to/stereobooster/pragmatic-types-making-impossible-states-impossible-inh)
- [An exhaustive check of pattern matching](https://dev.to/stereobooster/exhaustive-check-1079)
- [Opaque types](https://dev.to/stereobooster/pragmatic-types-opaque-types-and-how-they-could-have-saved-mars-climate-orbiter-1551)
- [IO validation](https://dev.to/stereobooster/pragmatic-types-io-validation-or-how-to-handle-json-based-apis-in-statically-typed-language-1d9m)
- Type-checked side effects
- Dependent type systems, like Agda and Idris
- And, probably, other things about which I forgot or don't know

There are other methods to prevent software errors, like:
- Garbage collection solves issues with unsafe memory operations
- The finite state machine makes sure there are no illegal states or no illegal transitions, this is like the first step [to formal verification of state logic](https://www.hillelwayne.com/post/formally-specifying-uis/)
- With [borrow checker](https://doc.rust-lang.org/1.8.0/book/references-and-borrowing.html) in Rust you can go for [fearless concurrency](https://blog.rust-lang.org/2015/04/10/Fearless-Concurrency.html)
- Thanks to [reference capabilities](https://tutorial.ponylang.org/capabilities/reference-capabilities.html) in Pony language it can do non-stop garbage collection, zero-copy messages in the actor model. It is kind of borrow checker on the steroids.
- Immutability prevents race conditions
- QuickCheck - a library for random testing of program properties
- Unidirectional data flow

But all the above is nothing compared to formal verification, like TLA+, Dafny, F star and others.

![map](https://thepracticaldev.s3.amazonaws.com/i/yyjie9qhewoy00nxkcqp.png)

Conclusions:
- Types don't replace tests - you still need to write tests, even if you use type checker
- Types are more effective at eliminating some classes of errors
- Error prevention not limited by type checking and tests, there are other ways to make sure there are no errors in your application

## Speed of adoption of tests vs types

Tests are easy to write for libraries (where the scope is limited) or for projects with established conventions, like Ruby on Rails. Tests are harder to write for the fresh code when you just started and doing some prototyping. 

 Types are easy to start with unless you have to deal with unreadable error messages and missing type signatures (Flow).

If you have experience with types, types should be much easier to adopt than tests, but keep in mind you still need to write tests for your code unless you are using something advanced as formal verification.

## PS

TDD means Test Driven Development, but also can mean [Type Driven Development](https://www.youtube.com/watch?v=X36ye-1x_HQ).

[This post is part of the series](https://dev.to/t/pragmatictypes). Follow me on [twitter](https://twitter.com/stereobooster) and [github](https://github.com/stereobooster).