An argument for types

2015-03-12

This post is a quick explanation of why I prefer working in language with specified declared type signatures, I want to concentrate on what I feel is the mostly overlooked human aspect to this problem.

I want to compare

int foo(String bar)

to

def foo(bar)

I will specifically only be talking about typing at the interface / abstraction level, that is to say I will only be covering function signatures rather than typing in general.

The problem

I have the following piece of code

def get_address(person)

and I would like to get the address of a person I know

I now have 2 sub-problems:

I am often told to just check the implementation of 'get_address' and see what it wants, but this implies a poor abstraction (having to look inside something to know how to use it).

When talking to people who usually work in languages lacking type declarations I am usually presented with the proposed solution....

The usual proposed solution

"just document it"

So going with this, ideal documentation would be something like:

# takes an instance of the Person class
# and returns an instance of the Address class
def get_address(person)

My issues with this are:

Which can be summed up as "we are implementing an ad-hoc and non-standardised type system in our languages comments".

The real solution

Use an actual type system

Address get_address(Person p)

Now this interface is formally specified in a standardised, consistent, easy to parse, and statically verifiable way.

As a human it is far less effort for me now to use this function, I can grep for both 'Person' and 'Address' and find how to use the classes.

Summary

Declared types are not the devil.