An argument for types
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:
what is the type of 'person': how do I specify a 'person' to 'get_address'.
what is the return type of 'get_address': how does 'get_address ' specify the resulting 'address' to me.
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:
the comment syntax is non-standardised: it takes more effort for me as a human to parse it
lack of user verification: more difficult for tools to catch errors in my usage of this function (user's life is harder)
lack of implementation verification: more difficult for tools to catch when the implementation drifts away from the comments (the implementor's life is harder)
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.