Static vs. Dynamic Typing

I don't mean to start the dynamically typed vs. statically typed flame-wars of yore all over again, but Todd Huss bought the issue up, and prompted me to look at my own thoughts on the issue. (Thanks to Brian Duff and Todd's earlier witticism)

For me, some of the benefits of static typing that Todd mentions are pointless - my IDE of choice is trusty old Vi, readability comes from comments and documentation and most environments I work in have no compile time at which to catch errors. However, I do agree with what Todd is saying.

Coming from PHP (a dynamically typed language), I actually tend to find dynamic typing to be a pain. Why? Because every time I enter a function, I cast all of the arguments into the type I want them as, before every mathematical operation I cast the arguments to float or int as I see fit. When you're working on the web, you can't afford to trust any piece of data - and type is a pretty important bit of metadata!

The Pragmatic Programmers put it best - a good developer doesn't just distrust other developers - they don't even trust themselves. Although the discussion (if I recall correctly) was on Design by Contract, the point is the same. Effectively, the type of your arguments IS a contract. Dynamic typing puts the responsibility of checking these agreements in your hands, rather than the interpreters (or compilers). If your compiler, or interpreter actually has hands, please seek medical advice

In some respects, my Nazi-like casting of variables is not so much due to the typed-ness of the language, but rather on the environment. For knocking together quick scripts, PHP beats any statically typed language hands down. In situations where I want security I want static types, so it's casting the whole way down.

That said, I don't notice the problem as much in Ruby As if I'd admit it anyway!!. I am yet to decide whether this is because I don't need to, or whether I just don't notice it. Even still, I tend to cast religiously, and just about every argument to puts or << or the such gets a .to_s tacked on the end - it's just being thorough.

As a final note, I certainly don't believe statically typing is a silver bullet - there's trade-offs to be made on both sides. Ask me next week, my opinion is bound to be different again...