As promised, this is the first in a series of posts that are designed to talk about the important differences in languages. In my first post I made the point that we often get caught up on the lack of semi-colons or how one languages uses curly braces and one uses “
If/End If” (and friends), but we often neglect the interesting differences. So, to start off this series discussing some of the interesting differences in programming languages, I figured I’d start with a class of languages that has been gaining popularity recently: “Dynamic Languages”
What are dynamic languages? Basically, instead of relying on the compiler to statically type-check your code (to ensure you aren’t trying to assign a string to an integer variable, etc.), the language run-time is responsible for doing these checks. A common misconception is that this is the same as a language like JavaScript, which has no real type system. However, dynamic languages are still strongly-typed languages, unlike "type-less" languages such as JavaScript which are not. This means that once you create an object, you can’t change its type. For example, in ‘traditional’ languages like C#, the following statement would be illegal:
i = 1 + "Hello"
In a type-less language, this statement would be perfectly legal, but it
is illegal in a dynamic-language. The trick to what makes dynamic languages different is that the variables can hold objects of any type (think Variant in VB6, or Object in .Net).
So, in a dynamic language, you can execute the following code:
myInt = 1
myInt = "Hello
Now, those familiar with statically typed languages such as C# or VB.net will notice that ‘myInt’ was never defined. In dynamic languages, you don’t need to declare variables before you use them, the first time you use it, it is automatically declared. So, how does the compiler know what type of variable to create (
Integer,
String, etc.)? It doesn’t, variables can hold objects of any type (those familiar with VB can imagine a line at the top saying: “
Dim myInt as Variant”). This means you can put a
String into the ‘myInt’ variable because the variable isn’t restricted to holding
Integers.
So what happens if I try to call a method that’s defined on an
Integer on my
String object? In C#, I would get a compiler error because the compiler can detect that the method doesn’t exist. In a dynamic-language, the compiler doesn’t know what type you’re using so it won’t raise an error. It isn’t until run-time that the error will occur, because the run-time does know what type the object is and that the method doesn’t exist.
Dynamic languages often tend to scare those of us who are familiar with static languages. It feels like a backwards step. We've spend the past years moving away from dynamic languages (
Variant was removed from VB.net), why are we going back? We can use dynamic languages because we have much better software development processes now. Unit Testing, Test Driven Development, all these techniques make dynamic languages safer. Also, dynamic languages are usually easier to write in, and give you cool features like “
Duck Typing” [Wikipedia] ("If it walks like a duck and quacks like a duck, it must be a duck"). In the end, it all depends on what you are trying to achieve and what you are familiar with.
That’s it for this intro to dynamic languages, in the next couple of weeks (hopefully), I’ll talk a bit about Python and Ruby (particular dynamic languages). If you want more info, check out the
Wikipedia entry for Dynamic Languages [Wikipedia] or if you just want to jump in, download Python (
http://www.python.org/) or Ruby (
http://www.ruby-lang.org/en/) and try it out!