From: Werner S. <ws...@se...> - 2007-12-14 12:26:42
|
Hi Nil, using assert macros is partly a matter of programming style and taste. My personal experience over many years of c++ programming show that they do not buy much. Your example shows were not to use an assert. The program will crash with and without the assert and you need a debugger to find out what happend anyway. Another tip to discover hard to find bugs is to use the "valgrind" tool. It can find the use of uninitialized variables which saved me _lots_ of time. Regards, Werner On Freitag 14 Dezember 2007, nilitonilito nilitonilito wrote: > > Hi all, > > The list being active again lately I've decided to start my own thread. > > For now 9 months I've been working as a C++ developer for a software company (that's why my involvement as MusE developer has significantly dropped, after programming all day long I need to get away from my PC more than before). But the good point is that I'm learning how to produce healthier programs. > > The little guy I'd like to talk about today is assert. At first I dislike to use it but I forced myself so my colleagues would think I'm a good programmer ;). Guess what? I can't ever program again without using assert, this little guy is fantastic! I use it all the time. > > For instance assume you need to access pointer ptr and in the current context ptr should be pointing towards a non null instance otherwise it does make sense in the program and it could even crash it. > > then you just put an assert before using the pointer. Like : > > assert(ptr); //check that the pointer is non null > ptr->method(blabla); //access the methods > > You can use assert for any properties you'd like to check in the course of the program, like : > assert(total_notes = added_notes - deleted_notes); > > Basically I ALWAYS use assert when the program could crash (like in my example with ptr), this prevents myself from hours of debugging. Besides preventing more crashing it also just gives hint to a reviewer of the code on the assumptions that part of the code makes. > > assert is part of the C++ standard so you don't need to include any header, you can use it right away. > > Of course it takes resources to treat all these assert but a flag to the compiler makes it simply ignore them -DNDEBUG . > > As far as I remember there is no use at all of assert in the code of MusE. > Hope my rumination would help at some point and thanks for reading it! > > Cheers > Nil > > > _________________________________________________________________ > Microsoft vous offre un logiciel pour classer, retoucher et partager vos photos ! > http://www.windowslive.fr/galerie/ |