Why strong typing is important

I am always in favor of strong typing (as opposed to “string typing” ;–)).

Right now I was testing some CLI tool that I'm writing and I was wondering why my --something was not considered by the implementation. Turned out, the CLI specification (which was done with the awesome clap crate) specified the name of the argument as some_thing and the code that parsed the argument and turned it into an action was using something.

And that's why we need strong typing. This error (or rather: bug) wouldn't have happened if the compiler was able to enforce types. But because this was merely a String, the compiler did not know anything about it and so the bug was introduced to the code.

You might say “Well, yes... you just have to know what you're doing and not mess up these things”. True. But consider this: two people working on the codebase, each one preparing a PR. The first PR changes the string, because it was non-optimal before. This could be a completely valid change. The second PR from the second person implements a new feature and relies on what is there: the string as it is on the master branch right now.

Both of these PRs are completely fine, the merge fine with master and they pass CI without any warnings. If they get merged (in any order), they do not result in conflicts and everything is fine – except that the second merge does break the tool.

Note that it does not even break the build! The code still builds fine, but because the strings do not match anymore, the tool just won't do the right thing in the case of the newly introduced feature!

This wouldn't happen if there was strong typing plus some nice bot-backened CI (for example bors).


Please note that I think clap is an awesome crate and working with it is always a pleasure. The case of the stringly-typed API is a known issue and there's work to improve the situation.