"if not x then return end" vs "assert(x)"

is there anything wrong with using assert over a simple if then statement? just wanted to know before i make a habit of doing this lol.

assert will throw an error with an optional custom message if the condition evaluates to false or nil.

4 Likes

assert will actually error and stop the code, which is great for alerting you or your players to a problem

2 Likes

“if not x then return end” and “assert(x)” aren’t comparable. Perhaps you meant error.

if not x then
    error("something")
end

is just reinventing the wheel for

assert(x, "something")

assert also returns both of its values, so you can do something like

if assert(x, "something") then

end
3 Likes