Sunday, January 21, 2007

Static vs. Dynamic Languages

In static typing all expressions have their types determined prior to the program being run (typically at compile-time).
Dynamic typing, also called latent typing, determines the type-safety of operations at runtime; in other words, types are associated with runtime values rather than textual expressions.

There's a lot of debate around dynamic vs. static typing.
The advantage to static typing is said to be increased safety, because types are checked before running the program and so many bugs are thought to be revealed.
The advantage to dynamic typing is that you do not waste all your time thinking at type problems and you can focus more on the programming itself. And about safety, even if you do not have compile time type checking, experience showed that dynamic languages (Lisp and Python) are surprisingly bug-free. Another good think with dynamic languages is that they do not restrain you from expressing naturally your thoughts directly. Dynamic languages are very good at prototyping and experiencing new ideas, because the delay between idea and runnable program is much more shorter. And even so it is showed that dynamic languages can scale; there are a lot of big projects using dynamic languages.

Even Bruce Eckel, author of "Thinking in C++" and "Thinking in Java", admitted that he prefers Python for expressing more naturally his thoughts in his article Static vs Dynamic: "I think that statically typed languages give the illusion of program correctness... In my own experience, it's very helpful to create models in a dynamic language, because there is a very low barrier to redesigning as you learn. Possibly more important, you're able to quickly try out your ideas to see how they work with actual data, to get some real feedback about the veracity of the model, and change the model rapidly to conform to your new understanding... By developing the model in a language that encourages change, my experience is that you end up with a better model, and this produces a distinct benefit when that model is translated to your implementation language... This last point is a major puzzle – we believe that static type checking prevents bugs, and yet a dynamically-typed language produces very good results anyway... My guess is that Python allows me to think more clearly about the concepts of the problem that I'm trying to solve. It is less distracting because it doesn't force me to think so much about the rules imposed by the language – rules that are basically arbitrary when I'm trying to produce an effective model of my problem space. By getting out of the way, Python and similar dynamic languages allow me to spend more of my brain's "seven plus or minus two" items on the problem itself, and less on the details of the language implementation... These are a couple of examples where too much static type checking, no matter how well-intentioned, gets in the way of both the creation and the examination of code."

So the conclusion: static typed languages are thought to be safer but dynamic ones turned to be more bug-free; static typed languages "stay in your way" of expressing your thoughts in programming while dynamic ones are more naturally at expressing ideas and experimenting. So why everybody insists on static typed languages?

No comments: