Return code for functions that would otherwise be void?

17 views
Skip to first unread message

Dustin Tran

unread,
Oct 14, 2015, 12:48:41 PM10/14/15
to stan development mailing list
I'm refactoring some of the ADVI code. A few of the functions are currently void, in which they update a referenced vector. Occasionally the updating fails, and I want to know this so I can write something like "if update fails, then do something" in another function.

What's the correct Stan convention for the return value of this function. bool? (true if okay; false if failure) stan service error codes? (https://github.com/stan-dev/stan/blob/feature/advi-adapt-eta/src/stan/services/error_codes.hpp) I suspect the latter but I haven't seen it used very much in other Stan algorithms.

Dustin

Bob Carpenter

unread,
Oct 14, 2015, 1:31:18 PM10/14/15
to stan...@googlegroups.com
Neither. The right thing to do if something doesn't work is to
throw an exception, then let the first component in which it's
nested that can resolve the exception resolve it. That's why we
have lots of exceptions and exception types, but no error codes
anywhere. The basic pattern in a local context is this:

try {
do update;
} catch (const error_t& e) {
do something else;
}

where error_t is the error you know how to catch and resolve. But
what often happens is that a function just throws and something
non-local cleans up. That's what happens for arithmetic errors in
log prob or gradient evals --- they cause a failure which gets caught
by either the sampling or optimization algorithm.

BUT, if that "something else" is common and not really a "failure",
then you want to use a boolean, because there's no "error" to report
with a code.

- Bob
> --
> You received this message because you are subscribed to the Google Groups "stan development mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-dev+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Dustin Tran

unread,
Oct 14, 2015, 1:33:27 PM10/14/15
to stan...@googlegroups.com
Cool, boolean it is. (The case I’m thinking of is not really a failure.)
Reply all
Reply to author
Forward
0 new messages