Page 1 of 1

Using assert to establish pre/post conditions?

Posted: Sun May 09, 2021 11:48 pm
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?

Re: Using assert to establish pre/post conditions?

Posted: Mon May 10, 2021 12:49 am
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).

Re: Using assert to establish pre/post conditions?

Posted: Mon May 10, 2021 3:11 am
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!

Re: Using assert to establish pre/post conditions?

Posted: Mon May 10, 2021 6:41 am
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.

Re: Using assert to establish pre/post conditions?

Posted: Mon May 10, 2021 12:37 pm
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.