DEV Community

Static Typing vs Testing == Apples vs Oranges

Venkatesh-Prasad Ranganath on July 31, 2018

I have heard discussions about why static typing is superior to testing (and vice versa) as the way to lower the number of bugs in systems, and I t...
Collapse
 
samuraiseoul profile image
Sophie The Lionhart

Where have people been advocating that you don't need tests if you have a type system? As your article demonstrates very well, a type system only prevents you from having to write tests that ensure the function only works with the correct inputs types and returns the correct type. Or said perhaps said slightly more eloquently:

Tests should be testing for logic errors. The benefit of the type system is that you no longer have to write tests to validate function inputs and outputs. You still need tests for logic.

Collapse
 
rvprasad profile image
Venkatesh-Prasad Ranganath • Edited

I have been privy to such discussions on social media involving knowledgeable folks taking extreme positions on both sides.

Yes, as of today, each have their strengths and weaknesses. This may change in the future. Until then, it is best to embrace them both :)

Collapse
 
samuraiseoul profile image
Sophie The Lionhart

Do you have any links to someone taking an extreme on either side? I'd like to see their arguments.

Thread Thread
 
rvprasad profile image
Venkatesh-Prasad Ranganath

These were remarks/threads I have seen on Twitter. So, unfortunately, nothing recorded :(

Collapse
 
rvprasad profile image
Venkatesh-Prasad Ranganath

Here's a Twitter thread comparing static typing and testing -- twitter.com/Hillelogram/status/102....

Collapse
 
dmerejkowsky profile image
Dimitri Merejkowsky

By the way if you read his blog you'll also find interesting stuff on this subject and others (like formal proofs)

Thread Thread
 
rvprasad profile image
Venkatesh-Prasad Ranganath

I am not saying Hillel or any static typing proponents are wrong. Having worked on program analysis/verification and formal methods, I am saying we need to have a broader and more permissive perspective. Instead of saying "static typing is the end all", it may be better to understand why we operate the way we do and be pragmatic about what it takes to get from where we are to where we want to be.

Collapse
 
evanoman profile image
Evan Oman

I definitely agree with the overall thesis, however generic types would help further constrain your equals and less than methods. Both should take the same generic type Twhich would ideally be bounded by some Ordered or Comparable types.

Collapse
 
rvprasad profile image
Venkatesh-Prasad Ranganath • Edited

I doubt Python type hints support generics yet.

Anyways, you are right that generics would help. In this sense, richer static typing (e.g., linear types, ownership types, dependent types) might help checking properties/constraints via testing. However, I think we have long ways to go until static typing can tackle the kind of issues tackled by testing.

Collapse
 
dmerejkowsky profile image
Dimitri Merejkowsky

I doubt Python type hints support generics yet.

They do: mypy.readthedocs.io/en/stable/gene...

(e.g., linear types, ownership types, dependent types)

True dat.

Thread Thread
 
rvprasad profile image
Venkatesh-Prasad Ranganath

Good to know Python type hints supports generics :)