Stop saying "type safe"

AndyArmstrong on 2007-05-20T12:35:48

I want to stop C, C++ and Java people referring to their languages as 'type safe'. They're not 'type safe' - they're type constrained.

C has static types for two completely distinct reasons:

  1. a partial attempt at Pascal / ADA style type bondage
  2. as an efficiency - so the compiler need not generate code to handle dynamic types at runtime

In practice it's really hard to graft dynamic types onto a statically typed language. In fact once you've implemented dynamic types you've done so much work that you might as well implement the rest of the dynamic language. And if you're going to do that you might as well just use a dynamic language in the first place. The debatable virtue of type bondage is used to mask the fact that C-like languages can't get all funky and dynamic - even if they want to.

Type bondage sounds good on paper - "if we constrain this value really tightly at compile time we'll catch all sorts of runtime problems". That certainly sounds like it'd be a good thing.

Unfortunately it mitigates against a better thing: being able to write correct code that remains correct even when asked to process kinds of things that doesn't know about.

Dynamic typing? Don't need it!

You'll sometimes read something like dynamic typing offers no advantage in a properly designed program. The implication being that dynamic typing is for sloppy thinkers who can't plan ahead. That may be true. The problem is that at the limit we're all "sloppy thinkers who can't plan ahead".

The design of C does, in fact, acknowledge the need for dynamic typing. The sort() standard function achieves type-agnosticism by allowing the programmer to pass in

  • the address of the things being sorted
  • the number of things to being sorted
  • the size of the things being sorted
  • a function to compare the things being sorted

Whoever implemented sort understood that you'd want to able to sort just about anything. So, fortunately, they decided to make it type-unsafe, type-dangerous, type-scary. Its interface is necessary clunky - because C doesn't do dynamic types - but at least you can use it to sort your own data types.

We're invited to believe that sort is a special case, that in general you won't be needing that kind of flexibility. The necessity of type-inflexibility is spun as the virtue of type-safety. Safety is good! How can you oppose safety? Think of the children!