Zwart
Just stumbled upon this post: Are Dynamic Languages the New Black? In it, the author questions the benefits of dynamic programming languages, and thinks that fashion is to blame for their recent rise in popularity.
In my experience, dynamic languages take a while to “grok”, especially if you come from mainstream statically typed languages like Java, Pascal and C++. (Which is what I will be talking about here, as opposed to static typing in languages like Haskell and OCaml.)
At first, the code you write in a dynamic language will probably look much like how you would write it in your previous language. For example, you can still write Python code that looks like Java, using getters and setters and AbstractFooHandlerFactoryFactory classes, and so on. If you do that, you are not leveraging Python’s flexibility, and you might wonder what the benefit is, other than the lack of type declarations. (Which probably feels very “unsafe”, and you might start writing unit tests or assertions to check the types, which then seems like extra work.)
In order to enjoy the full capacities of a dynamic language, you have to embrace its dynamic features and not work against it. While that may sound spacy, what it really comes down to is: if a language gives you certain features, consider using them rather than working around them! Of course it’s always possible to abuse such features and make an unmaintainable mess of your code, much like in *any* language; but that’s why there are guidelines and good coding practices, which, by the way, will be different in some areas from what is considered “good” in static typing land.
This is an example of what I consider the wrong approach:
Type safety, [proponents of dynamic languages] argue, can be achieved through unit testing. Write a test for a.b(c), and if a or c are of the wrong type in that scenario, the test will fail when the interpreter gets around to checking what’s in those slots.
Aside from what “type safety” means here, the issue is that when using a dynamic language, we’re often *not interested* in explicitly checking types. What we are interested in, and what we write tests for, is whether a.b(c) runs without errors. It should return the expected result and not raise exceptions or fail otherwise. The exact types of a and c often *don’t matter* in this context.
This is a feature rather than a bug, and can often be put to good use. Let’s say that in a.b(c), a is usually an instance of class A, but I want to replace it with a mock object M, then that can be done without much hassle. No need to worry if M implements the same interface as A, or is a subclass of it, etc… all that’s necessary is that M has a method b that can handle c, and returns a reasonable result.
This is just one example, but dynamic polymorphism can be used in many ways. It takes time to figure out everything that is possible, to learn new coding and design habits (and unlearn old ones), and to learn to trust your tests even if they don’t explicitly check for types. I think you would need tests anyway — static typing doesn’t test if your code does what it should do, just that it takes and returns the “right” types.
Anyway, about the fashion issue:
I put it to you that the current fashion for dynamic languages is excatly that – a fashion. And many people are using them because they’ve seen other people using them, and they want to be in with the in crowd.
Quite possibly. Fashion and hype tend to run strong in programming communities. However, you could argue that the proliferation of languages like Java, C# and C++ happened due to fashion and hype as well.
