Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

assert.h

Name: Anonymous 2010-01-03 18:25

What does /prog/ think about assertions?

By example, suppose you are developing a library, and you have a function like this:

/** Serves the foo.
 *
 * @param request The request id, which must be an odd number.
 */
int foo (int request);


Obviously you should implement some kind of checking in order to avoid bad jokes during execution, so you may be tempted to do something like

#include <assert.h>

int foo (int request)
{
    assert(request % 2);
    ...


I think this is a good design, since it makes the API more simple and it doesn't need an error management procedure. Obviously this is not the case when the error depends on - say - the content of a file, which may be non-deterministic.

The drawback is of course the crash of the whole application, but this is responsability of the developer who's using the library, since the documentation prohibits to insert even values as parameter of the foo function.

What do you think?

Name: Anonymous 2010-01-03 18:41

Assert macros are usually good ideas regardless of language, but they should not be confused with continuable errors (exceptions, conditions, whatever your language, environment or platform provides). An assertion is most likely not a continuable error, so it should only be used in fatal situations; for the rest of the cases, use something milder which can be continued (such as an error code, or setting a last error parameter, or throwing an exception, using signals, conditions - depending on the language and platform in use).

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List