Re: asserts

From: Dom Lachowicz (doml@appligent.com)
Date: Thu Apr 18 2002 - 10:03:10 EDT

  • Next message: Michael D. Pritchett: "Re: [Fwd: Re:Tree Closed for 24 hours]"

    On Wed, 2002-04-17 at 22:52, Mike Nordell wrote:
    > A discussion on IRC made me realize this hasn't been thouroughly enough used
    > in the current code.

    I'm very much in favor of using tools to track memory usage, pre&post
    conditions, variable intializations, and so forth.

    Asserts are a very good way to check pre & post conditions or to alert
    you to an "impossible state" that your program has been thrown into.
    Asserts are good for this sort of thing, and I encourage their use in
    these areas.

    Asserts are poor memory error checkers - there's no two ways about it.
    Tools like Verify and Valgrind (and to a lesser extent, Memprof) are
    very good at tracking memory allocations, deallocations (or lack
    thereof), double-frees, heap corruptions, etc... I encourage the use of
    these tools in these areas.

    Variable initialization is just a good thing to do. Java and C# do this
    for you. While I think this is good, I think it also leads to laziness
    and forgetfullness all too often, and might not be so much of a time
    saver if you want all of your class' bools initialized to true (for
    example). In C++ it's a bit more work, but it's still easy. int i = 0;
    Not so hard. Class variables should be initialized in constructors (or
    at least a common init() method). Function variables should be
    initialized when they are declared or immediately afterwords:

    int i, j, k ;
    i = j = k = 0 ;

    The idea is that through variable intialization we don't throw ourselves
    into an "impossible state" and thus trigger fewer asserts.

    Exceptions are good tools, but C++'s exception checking and handling is
    piss-poor, IMNSHO. In Java, you can at least declare that your
    method/ctor throws an exception or error and have the COMPILER make sure
    that you, the programmer, catch that exception or propegate it to
    someone who will. That said, we do have an exception throwing mechanism
    in place and I encourage its use. If you throw an exception, DOCUMENT
    THAT YOUR METHOD THROWS and declare the method as UT_THROWS((types))

    my_iconv_class::my_iconv_class (string in_charset, string out_charset)
    UT_THROWS((int))

    Every one of our platforms and compilers support exceptions. Even OS/390
    running ZOS does, and its compiler doesn't understand bool (or anything
    else introduced in C++-land before 1992, for that matter). For those
    places where exceptions are a bad idea, return a WELL DOCUMENTED error
    condition and make sure that the calling function checks that return
    condition for success or failure.

    Code defensively. These are all tools. Use the correct tool for the
    situation, and use the tool correctly.

    Dom



    This archive was generated by hypermail 2.1.4 : Thu Apr 18 2002 - 10:05:03 EDT