# Guard Clauses - Improving Readability

## What are Guard Clauses?

Guard clauses are a fancy programming trick that let you check for specific conditions early in your code, and handle them before getting into the meat of the logic. It's like a bouncer at a fancy club, who checks your ID and makes sure you're on the guest list before letting you in. Guard clauses can make your code simpler and easier to understand, by getting rid of a bunch of if-else statements and error-handling.

## How You Can Use Them

In Lua, you can use the if statement to create guard clauses. Instead of putting a bunch of nested if-else statements, you can use the return keyword to exit the function early and handle any errors or special cases. That way, you don't have to keep checking the same thing over and over again.

## Examples of How They Can Improve Readability

Let's say you have a function that calculates the square root of a number. You want to make sure the input is a number, and that it's not negative. Here's how you can use guard clauses to make the code cleaner and more readable:

local function calculateSquareRoot(number)
if type(number) ~= "number" then
return "Error: input must be a number"
end

if number < 0 then
return "Error: input must be non-negative"
end

return math.sqrt(number)
end

In this example, we check if the number parameter is a number using the type() function. If it's not, we return an error message immediately without continuing with the rest of the function. Next, we check if the number parameter is non-negative. If it's negative, we again return an error message without continuing with the rest of the function. If both guard clauses pass, we calculate the square root of the input using the math.sqrt() function and return the result.

Here's another example. Let's say you have a function that finds a key in a table. You want to make sure the input is a table, and that the key exists in the table. Here's how you can use guard clauses to make the code cleaner and more readable:

local function findInTable(inputTable, key)
if type(inputTable) ~= "table" then
return "Error: first parameter must be a table"
end

if type(key) ~= "string" and type(key) ~= "number" then
return "Error: second parameter must be a string or number"
end

if not inputTable[key] then
end

return inputTable[key]
end

In this example, we check if the inputTable parameter is actually a table using the type() function. If it's not, we return an error message immediately without continuing with the rest of the function. Next, we check if the key parameter is either a string or a number. If it's not, we again return an error message without continuing with the rest of the function. Finally, we check if the key exists in the inputTable using the inputTable[key] syntax. If it doesn't, we return an error message without continuing with the rest of the function. If all guard clauses pass, we return the value of the key in the inputTable.

## Conclusion

So, that's guard clauses in Lua! They're a powerful technique that can make your code simpler and easier to understand. You just add a check at the beginning of your code block to handle error cases early on. It's like putting on a seatbelt before you start driving – it's just a simple step that can help keep you safe (and your code bug-free).

And the best part is that you can use guard clauses in if statements, while and for loops using break and continue. So, you can simplify your code in many different ways.

Another helpful function in Lua is assert(), which you can use to double-check your assumptions and catch errors early. It works by checking if a value is true, and if it's not, it throws an error with a message you provide. assert() is often used with guard clauses to help make your code more readable and catch errors early on.

If you want to learn more about guard clauses, assert(), or Lua programming in general, there are lots of resources available online, like Lua's official documentation, Roblox's Luau documentation, and tutorials on almost every platform. So, give it a try in your own code and see how it can help make your programming experience better!