Tuesday, 16 February 2010

Dynamic vs Static


I'm not much keen on watching "Programming Videos". Sure there are some very interesting ones, but they tend to bore me terribly, among other things because they tend to be too long, so I prefer the old way, a nice written article that you even can print if you find it really useful (of course both sides and two pages per side, I try to waste as little paper - and the energy and chemicals needed to recycle it also count - as possible).

Well, to the point, the thing is that the other day I found a video in Infoq with a good topic, differences between dynamic and static languages, and when I read the Bio of the lady giving the talk I can't deny that my interest grew up (yes, still today is rather unfrequent to find a lady with a passion for programming, technology...), so I decided to give it a try, and it proved to be one the best examinations of Static vs. Dynamic that I've came across.

Warning, what follows is a mix of what is stated in the presentation with my own ideas, so please, watch the video from that smart lady and give little credit to my musings below.

First and pretty obvious, but more than once in discussions with colleages I had missed it, Dynamic means done at runtime, Static means done at compile time.

Second, we have several terms with dark and mixed meaning:

  • Static Typing vs Dynamic Typing.

  • Strong Typing vs Weak Typing (vs Duck Typing)


For me, Strong typing means that types exist and type checks are done, either at compile time (Static Typing) or at runtime (Dynamic Typing). This article considers Python a Dynamic, Strong typed language, well, I don't agree with that, sure types exist in Python, but I think not real (runtime) type checks are done, just Duck typing checks (it quacks like a duck, walks like a duck... so it's a duck), and if the field, method does not exist in the Internal Dictionary, error...
So, I would better classify it as Duck typed (as Wikipedia does).
About Weak typing, I don't have a clue of what it really means...

We also have Implicit Typing, that has nothing to do with Dynamic Typing (C# var is very convenient, but it's just implicit typing, that is, compiler magic saving you keystrokes or even declaring just one time classes).

I think that when we talk nowadays about Dynamic languages we're talking about much more than the classification above.
Some of those things that to a greater or lesser extent conform what we call a Dynamic Language are:

  • Introspection. Programs can query their own internal structure at runtime. Static languages like C++, Java or classic C# (System.Reflection) have this feature.

  • Structural Reflection. Programs can query and modify their own internal structure. This means that we can add or remove methods and data fields to an Object (instance object or class object, this is sometimes called expandos), modify the inheritance chain... This is possible in the almighty JavaScript, Python, Ruby. Don't confuse C# Extension Methods with this, these ones are just a compiler trick.

  • Dynamic compilation. The program is able to add and run new code. We have two tastes here, the more traditional API style (available in languages like C# under the System.Reflection.Emit namespace), that allows a more limited form of code addition, and the almighty and straightforward "string eval" style (Javascript's eval(), Python's compile() and exec()) where the new code is able to run in the local scope!

  • Classes as first-order objects. I mean with this that Classes, that construct used for templating new objects, are objects themselves, that can be treated as other objects. This means that we have Instance Objects and Class Objects. It's a step forward from the C# Type class.

  • Support for "trendy" functional style features
    • Functions as first order objects. That means you can pass functions as parameters, return functions (that is, you have metafunctions)... C#'s delegates are good step in this direction

    • Closures. This is a long topic, not much to say in this post...

    • Partial function evaluation, binding object to functions

    • Generators (aka iterator blocks)


There's one interesting note by the speaker that I try to apply to many other aspects in life, "it's not a matter of whether a language is dynamic or not, but of how dynamic it is". These things are not digital, but purely analog, gradients are the important part here. It's the same I think when we talk about whether animals are intelligent or not, whether they have a language or not... there are many degrees in between...



No comments:

Post a Comment