Where do you benefit from dynamic typing?

Viewed 2118

How often do you take advantage of dynamic typing in a way that really wouldn't be feasible in a statically typed language?

What I'm interested in is, how often these are used within real world (rather than demonstration) code?

4 Answers

It's called "expressiveness". As simple as it seems, the benefits are reaped every single time it is not necessary to add further complexity to the code just to extend it.

Suppose some hypothetical JavaScript code looks like this:

function somefunction(options) {
    // ...
}

someFunction({
    a: 1,
    b: 2,
})

And then a new key, e.g. c becomes necessary for a new use case. With dynamic languages, such as JavaScript, all that needs to be done is something along the lines of:

function somefunction(options) {
    // ...
    doSomethingWith(options.c) // use the new key-value pair
}

someFunction({
    a: 1,
    b: 2,
    c: 3, // add the new key-value pair
})

In a non-dynamic language, it would be necessary to also do further refactoring, e.g. changing an intermediate interface / protocol / struct to achieve the same results. So the benefit is in keeping the code lean, less verbose and straight to the point.

Related