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.
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).
>>1
Compile-time assertions are even more useful for those types of things.
Name:
Anonymous2010-01-03 18:59
>>4
But not always possible. How can you... wait, IHBT.
Name:
Anonymous2010-01-03 19:17
I've found little use for them, since in my case, foo would crash anyway if it was given an even parameter.
Name:
Anonymous2010-01-03 19:31
>>6
Is there a way to make the whole stuff a little more informative? Passing an even parameter might be a documentation misread or an invariant not verified!
In this case finding the error may be troublesome. By using the assertion this can be easily localized instead.
Name:
Anonymous2010-01-03 19:44
Assertions are just good coding practice. From what I've seen, Design by Contract is just the logical extension of this and PLT makes it pretty usable. Presumably the Nazi-typed languages like Haskell just use guards and have their type-checker do the work.
Name:
Anonymous2010-01-03 20:24
I usually use assert to verify the status of internal variables, that no user of the library could ever modify - class invariants and the like. For function call parameters, I just throw WTFAreYouDoingExceptions or something along those lines (something recoverable, anyway), unless the code is performance critical in which case asserts that compile away are a better choice.
I try not to use assertions and, instead, I try to build a response condition. I determine a range of valid values and write statements to correct or re-rail that variable should it veer out of scope due to calculation exuberance or human error (I name the least destructive value in range as the default).
For functions and their ilk (methods, etc..) that means checking every essential parameter as it comes in. The importance of the variable determines how it is handled; but, I try to avoid stopping the program (or, worse, the program coming to a stop) until the user wants it to at all costs.
Some people call this wasteful, but I think it's very thorough and careful as long as you have enough wriggle room.
Name:
Anonymous2010-01-04 10:20
>>10
Why? Class and namespace names start with a capital, function and variable names don't.
>>20
not the way you kopiped it, asshole. that perl code seeks to the beginning of itself and skips the first 3 lines, expecting them to be the shebang and use strict, plus a blank newline.
not the mention you fucked up the top of the hump. you're worthless.