Using assert to establish pre/post conditions?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
togFox
Party member
Posts: 262
Joined: Sat Jan 30, 2021 9:46 am

Using assert to establish pre/post conditions?

Post by togFox »

I'm thinking of getting into the habit of using assert to test my input parameters are what I expect them to be:

Code: Select all

function AddTwoNumbers(num1,num2)
-- return the sum of two positive numbers

-- I'm not testing for nil because that would throw a runtime error and will be instantly obvious anyway.
    Assert(num1 > 0)
    Assert(num2 > 0)

    return num1 + num2
    
end
That's a silly example of course but I'm thinking it would help stop rogue errors that can be hard to track down. The idea is that an error is thrown during unit testing, debugging and alpha testing. I'm less interested in error handling (which of course is also important) and more interested in catching dumb mistakes during development.

Is this a good idea? Is there a better approach?
User avatar
ReFreezed
Party member
Posts: 270
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: Using assert to establish pre/post conditions?

Post by ReFreezed »

This is exactly what asserts are supposed to be for, so I'd say it's usually a good idea. (Maybe not for extremely trivial functions like that example, of course.) It's probably also a good idea to remove or comment out all the asserts in the final/public version of the program. (There are tools that can do this automatically too.)

As you say though, proper error handling is important too. Asserts should really only be used in cases where regular error handling isn't expected to be needed - it's just to make sure the program is doing what you expect it to, and if it's not then we just panic and exit the program (which you don't want the end user to experience ever, if possible).
Tools: Hot Particles, LuaPreprocess, (more) Games: Momento Temporis: Light from the Deep, Energize!
"If each mistake being made is a new one, then progress is being made."
togFox
Party member
Posts: 262
Joined: Sat Jan 30, 2021 9:46 am

Re: Using assert to establish pre/post conditions?

Post by togFox »

I see. I ask the question because I don't think I've ever seen it done. I guess laziness, brevity and coder confidence rules the day.

I don't think I'm going to do this every time but I think if my debugging identifies a 'type' or 'bounds' issue (for example) then I'll fix the root cause and then insert an assert to make sure I don't do that bug again. I guess it also goes somewhat towards self-documenting code in a way.

Thanks!
User avatar
ivan
Party member
Posts: 1732
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Using assert to establish pre/post conditions?

Post by ivan »

-- I'm not testing for nil because that would throw a runtime error and will be instantly obvious anyway.
You are on the right path there - that's why you should rarely assert the "type" of the parameters.

Also make sure you only assert parameters that are coming from an external source.
You shouldn't need to assert parameters that are coming from another part of your own code/library.
User avatar
ReFreezed
Party member
Posts: 270
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: Using assert to establish pre/post conditions?

Post by ReFreezed »

ivan wrote: Mon May 10, 2021 6:41 am You shouldn't need to assert parameters that are coming from another part of your own code/library.
I disagree there. For robustness and your own sanity, asserting that your program is actually doing what you think it does is always a good thing, especially when making changes in larger, or more complex, code bases.
Tools: Hot Particles, LuaPreprocess, (more) Games: Momento Temporis: Light from the Deep, Energize!
"If each mistake being made is a new one, then progress is being made."
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 11 guests