Error Checking with Lua

error_bawl
Where there is code, there will be bugs… Not the winged variety (though many developers do get wings from all of the energy drink they might consume). Here is a simple technique on using error checking with parameters passed to a function
Before we read on about this function, copy it or bookmark it, a little blurb that might be of interest to some. There is nothing new in this function, it is not the next thing after sliced bread, when programming or writing code, it is the little moments that make you write code to resolve little problems. This solution is similar and involves a very simple technique that you might already be using somewhere else.

What is a parameter

A parameter is a value that is passed to a function. So for example a function called add might require two parameters number1 and number2 this function would then return the result of adding the two numbers.

What if we do not pass a parameter

Well if you have worked with Lua, you will know that Lua will initialize the parameters value as nil and thereby the code will throw up an error. This happens because Lua cannot add a numeric type of variable with nil.

What if we pass a string or a table

Lua has internal conversions, so if the string contains a number, it can be added to a number, eg

local a = "5"
local b = 3
print(a+b)

This code will result in the number 8 being printed, however in most other languages the result would have been 53. Lua converts the string value of a as numeric 5 and adds to it the numeric value of 3 and that thereby results in the answer being 8

If you pass a table, then definitely Lua will throw an error as it cannot add a number with a table. Note: That most display objects are Lua tables, classes and objects are tables too.

So what are we going to do?

The solution in a single word was already provided above, when we said type numeric.
In Lua we have a function called type this returns a string that contains the type of the variable. These values are either numeric, string, function or table. So this forms the base of our function quite well.

Show me the code

So without more words and explanations, here is the code that you were most probably waiting for.

 function checkIsNumber(value, paramName)
   if type(value) ~= "number" then 
     error("The value in " .. paramName .. " should be a number but was (" .. tostring(value)..")")
   end
 end

and we can test it with

 a = 3
 b = "bee"
 checkIsNumber(a, "a")
 checkIsNumber(b, "b")

This can be extended to check for strings, tables or functions. The error would break the code which would be important in development phase to ensure that there are no auto conversions and errors that might creep in, however if you do not want the validation to break the code, replace those error with print.

The functions can be extended further to incorporate functions like

isZero
isPositive
isNegative

while these can be used as inline code, the advantage of creating a function is Readability, one would know while reading the code on what the logic is trying to do, while it might seem insignificant, it does add a great deal

if a > 0 then 
  print("Positive Number")
end

versus

if isPositive(a) then
  print("Positive Number")
end

In conclusion

Hope this article has helped you crush some more bugs and write better code. Another thing in this article is the graphic which is created by me, it feels nice to illustrate for my own article.

UPDATE: We got a comment from a reader that an alternative would be to use assert, while assert is widely used in many languages, it is not used for error checking, it is used to ensure the validity of some variables before the next line is executed. In the context of the example above, we could simply test for something and skip, but if we were to use assert, it would halt further code execution. So, you have choices…

Comments are closed.