Static Typing vs Testing == Apples vs Oranges

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 think such discussions are futile as they are comparing apples and oranges.

Image for post
Image for post
def foo(i: int, j: int) -> bool:
return i < j
def bar(i: int, j: int) -> bool:
return i == j
foo: (int, int) -> bool
bar: (int, int) -> bool
foo("1", 3)   # first argument is not of int type
bar(3, [1,3]) # second argument is not of int type
if foo(3, 3):
print("val1 is equal to val2")
if bar(3, 5):
print("val1 is less than val2")
def equals(i: int, j: int) -> bool:
return i < j
def less_than(i: int, j: int) -> bool:
return i == j
if equals(3, 3):
print("val1 is equal to val2")
if less_than(3, 5):
print("val1 is less than val2")
  1. Returns true if the inputs are equal; false otherwise.

Static typing as available in most programming languages today are geared towards only specifying value types (and not behavioral types).

If you are still not convinced about behavioral types, then consider relying on just the function signature (List[int]) -> List[int] and picking a function that sorts a given list of integers in ascending order.

What about testing?

Unlike static typing (as it is generally available today), testing is not about value types.

Testing is about specifying and checking behavioral types.

Specifically, a test case captures a specific behavior of a function (e.g., equals(3,3) should evaluate to true, equals(3,5) should evaluate to false), a test suite captures the behavioral type (i.e., a collection of expected behaviors) of a function, and testing checks if an implementation of a function is behaviorally type correct (i.e., function exhibits the expected behaviors).

So, where does this leave us?

Until the day we have static type systems that allow us to easily and succinctly specify both value and behavioral types and use them to check for type correctness, we will need both static typing and testing to lower the number of bugs in systems.

Written by

Programming, experimenting, writing | Past: SWE, Researcher, Professor | Present: SWE

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store