DEV Community

David Haley
David Haley

Posted on

Language types for integration safety

In my post on web dev I extolled the virtues of types. Here's an example where static typing would help.

The current implementation of an algorithm I'm refactoring returns a new 64-bit float array for its results. For performance reasons I want to modify the array in-place. This runs awry of type assertions made in the tests. If I modify an int array in-place, I still have ints; the tests assert float.

https://github.com/dchaley/deepcell-imaging/issues/104

If I "just shipped it" – the new type contract maintaining ints – it could break downstream code surprisingly. For instance, somebody probably multiplies the result against an int matrix, expecting to still have floats (float * int = float). And you wouldn't know until it ran… or perhaps, until you noticed it ran incorrectly…

With static types, the compiler tells you, before you ship a thing, that something's gone awry. If you tell it you expect an array of floats, and give it ints, that's an error. Now, there's still plenty of room for generic behavior where types are passed through. Still: the mere existence of this layer provides automatic protection against errors.

In my case I think I can address with parameterization. I need to parameterize editing in-place anyhow. So, people using that new mode, would get the new contract. People using the current mode (return a modified copy) would keep that.

So I get to sidestep the problem in this case and avoid the breaking change. 😎

Top comments (0)