I have to admit it. I have some problems with dynamically typed languages. Even if I love Python, I'm always worried about using one of those languages in a production environment. It's not because I absolutely want to define the type of variables but because I don't trust other developers. And with "other developers" I mean also myself in the past or in the future.
My colleague Daniele Veneroni provided me a great example with his funny post a couple of weeks ago. I'm sure that he is smart enough to never write such a function, but out there there are lots of bad programmers.
Let's take a close look to the function.
local function isStaticPlugin(pluginName) if staticPlugins[pluginName] then return true elseif dynamicPlugins[pluginName] then return false else return 'WTF' end end
This violates at least three golden rules:
- Every function must do a single thing (and do it well),
- The name do not match what the function does (in fact it does also check if the plugin is dynamically loaded or it's not present), and
- The name suggests that the possible returned values are just true and false.
You may object that it is just a matter of style. No, Sir, it isn't. Now I'll show you why.
Just to be clear, these issues are not specifically related to Lua or to dynamically typed languages. You can do the same mistakes in C by returning an int. Someone using this function may check only for the true value and then put an
else to handle the condition of dynamic plugins. This obviously will lead to unexpected situations or an error whether the plugin does not exist.
Dynamically typed languages aggravates the situation. If the value returned by the function is stored in a variable, every time that variable will be used in the code, there will be the risk of type mismatch errors.
In addition, in Lua
('WTF' == true) evaluates to false but
'WTF' evaluates to true in a
if condition. So, depending on how the checks is made you may have false positives both on static and dynamic plugins.
Many dynamically typed languages are interpreted, not compiled, so I hope you are using a really good linter.