Measurement of code quality |
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 endThis 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's just a matter of style. No, sir, it isn't. Now I'll show you why.
Bugs Hard To Find
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
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.Bottom Line
Many dynamically typed languages are interpreted, not compiled, so I hope you are using a really good linter.
Image by smitty42 licensed under CC BY-ND 2.0
Post a Comment