Coding Is Funny, Debugging Not So Much

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.

My colleague Daniele Veneroni1 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  

Measurement of code qualityMeasurement of code quality

This violates at least three golden rules:

  1. every function must do a single thing (and do it well),
  2. 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
  3. 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 ('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.

  1. Daniele Veneroni: Tumblr, Twitter, Google+


Image by smitty42 taken from Flickr licensed under the Creative Commons Attribution-NoDerivs 2.0 Generic license.

Luca Sommacal

Luca Sommacal

Italian developer (mainly in C for embedded platforms), Linux learner, addicted to rock music, history, science and few other things. Follow me on Twitter

comments powered by Disqus