Does TDD replace static types need ?steemCreated with Sketch.

in ocaml •  4 years ago 

The usage of unit testing through TDD deeply impact my view of how to design and think architecture, and honestly for the good. I practiced it with Python, PHP and Java and now in OCaml.

I read in many articles that a proper usage of TDD makes unnecessary the usage of static languages.

Some definitions first

types

Types give you the possibility to designate a set of values sharing a common characteristic. Types can be about an input or an output.

The function or method you call take parameters, each one having a type and return an output with specific type.

Unit tests

Unit tests gives the possibility to check in the context of a function or a method that a value of the input result in the good output.

Two tools, two different added value

In the context of Ocaml I use both tools heavily. The design of the architecture is mainly driven typing of data. But the design of the algorithms are driven by TDD.

Static typing in OCaml give me the guarantee that user input is never used without being checked. This is interesting for security and also data consistency checking at runtime. I remember of an algorithm implementation which gave the good result, but data consistency checks at runtime triggered an exception showing that my design was missing a business entity. How? By an out of range error showing that I was mixing different kind of data that wasn't encapsulated in the good type.

Unit testing will test another kind of thing, does your algorithm return the good value. Is an input linked to the good result value?

a golden hammer again...?

Unit tests and types in programming are so distinct, that it is hard to see a link between both.

But why are we regularly tempted to use only one of them and not both?

This two tools ar very powerful, and when you gather a quite big experience, you can try to do every kind of checking with them. And so we start to have the Golden Hammer syndrom (feel free to search on Google what is the Golden Hammer).

To summarize, imagine you implement a balanced binary tree. Unit test will check that you have the good numbers in the tree whatever the operation you are making on it. Types check that the tree contain the good kind of data, and sometimes like in OCaml, will check that your algorithms produce always a balanced tree for all possible values statically.

In my projects I usually have regrets if I don't use both tools from the start.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

To the question in your title, my Magic 8-Ball says:

Very doubtful

Hi! I'm a bot, and this answer was posted automatically. Check this post out for more information.