From: Tim <ter...@us...> - 2007-12-15 01:13:02
|
If I may offer my opinion: I agree that we don't gain much by using assert, and I avoid it completely, because nothing pisses off a user more than having the program stop, whether due to an assert, or some other unpredicted cause. My style is that I keep the program running at all costs ! Only when the program could not possibly continue, do I quit with an assert or exit. It's called "mission critical". My 'golden rule' is this: I check if a pointer is null, and if it is, then I simply return from the function (with an error code, if allowed) AND do a 'printf()' so I can see on the command line that something went wrong. But NEVER just quit the program. "Mission critical" means you plan for such situations thoroughly, consistently, and gracefully throughout the program. Also, I would like to point out that if a function is passed a null pointer, well OK, that's easy to check, but most of the time it will be passed a BOGUS nonsense non-null pointer. You can't check such pointers. In some cases, yes, you can check if it's a valid pointer if there is a list of valid pointers somewhere and the pointer is not in the list. For example in Muse there is a list of 'track pointers'. If a function is passed a 'track' pointer, you can see if the pointer is in the list. But beyond that, there's no way to check such pointers by themselves, simply because attempting to de-reference them might crash the program. This is why asserting for null pointers is rarely effective ! All you need to do is start the program under a debugger, and the debugger will tell you where the violation occurred. No valgrind etc. is needed. ***----------------------------------------------------------*** While we are on the subject of programming style, I would like to alert the programmers out there to watch out for a particular Standard Template Library no-no: I call it the 'delete while iterating' bug: In Muse I had to fix several places where someone iterated an STL 'map' or 'list', and would conditionally delete items from the map or list, while still iterating the map or list. This is a no-no! The iterator becomes invalid after deleting an item, so the whole iteration has to be started over again. *** Take a look in Muse 0.9, in ./muse/seqmsg.cpp: around line 525: In the function Audio::msgRemoveTracks() Read the comments I placed in there. Learn it, use it ! Comments welcome! Cheers. Tim. |