Some Trade-offs Regarding Typing / Types in Programming Languages

Last updated:

WIP Alert This is a work in progress. Current information is correct but more content may be added in the future.

Types are very important and permeate most work we do when programming, and also how we reason about our programs and systems. I've tried to list a few trade-offs and comparisons between them to help us choose which one suits our needs the best.

I'm thinking of these definitions as follows:

Static Typing means that every variable has just one type, and methods have strict type signatures.

Dynamic Typing (also called duck-typing) means that it doesn't matter so much of what type a variable is, as long as it does what you want. ( the expression duck-typing comes from the famous duck test.)

Timing

  • Static Typing

    • More work at the beginning (hardly any programs work compile the first time you code them), but compile type errors mean system behaviour will be more predictable at runtime.
  • Dynamic Typing

    • Easier to get a system working from the get-go (no compile-time errors or type errors), harder to keep complexity at bay and avoid runtime errors as the system grows.

Testing

  • Static Typing

    • Type and compile-time errors are easily caught but that doesn't prevent you from having to test funcionalities, i.e. not how well the implementation fits the requirements, but how well the system fits actual user expectations.
  • Dynamic Typing

    • Requires more testing than static typing. The reason is clear: much fewer potential errors are caught at compile time.

Behaviour at runtime

Have you ever wondered why you don't ever have runtime errors on SQL queries?

  • Static Typing

    • Tends to be more reliable and stable. If you run your program once and it works, it's likely that it is correct, for all cases.
  • Dynamic Typing

    • Tends to be less reliable and prone to errors. You may have a system that runs well for weeks and breaks when some new unforeseen condition is met: an object whose attributes have not been correctly set (for example, due to faulty user-interaction) will cause a runtime error;

References

Dialogue & Discussion