reference initialization

From: Hongwei Xi (hwxi@ececs.uc.edu)
Date: Wed May 10 2000 - 06:50:10 MET DST

  • Next message: Nicolas GEORGE: "Re: ncurses interface for caml?"

    > Wrong. You have references, which are quite better than pointers
    > (they are typed, and necessarily initialized)

    I have given some thoughts on this one.

    I find that it is really not a good design choice to initialize
    every reference in all ML-like languages (yes, I well realize
    the difficulty in not doing so). Here is my argument.

    The common wisdom is that if we initialize every reference
    immediately after it is created then we shall never read from
    an uninitialized reference. But this is a lame argument.

    Suppose I use a reference 'x'. If I know what the initial value
    of 'x' should be, I'll of course prefer to initialize it with that
    value. Now suppose I don't, that is, I intend to assign a value
    to 'v' later (maybe in a loop or in a conditional branch)

    (1) ML strategy: initialize x with any value 'v' of an appropriate
    type (sometimes, such a 'v' is difficult to find or takes time to
    construct (consider 'x' to be a large array)).

    Now suppose I make a mistake, forgetting to assign a value to 'x'
    later. What happens is that the computation now happily use the
    *wrong* initial value and sings the song "well-typed-program-can-
    never-go-wrong" until a (most likely) wrong answer or exception is
    returned.

    (2) Java Strategy: let us say we have the same scenario as before
    but 'x' is not initialized. If I read from x before assigning a value
    to 'x', the execution abnormally stops (yes, we need run-time checking).
    I think this is much better than to carry the execution further until a
    wrong answer or exception is returned.

    Can you still say that the ML strategy is better than the Java
    strategy? I thus argue that it is better using dynamic checking
    to detect reading from uninitialized reference than simply
    assigning a value to every reference upon its creation.

    To summarize, my bottom line question is: what is really achieved
    by assigning a reference a (wrong) initial value? Isn't this just
    like an ostrich solving its problem by burying its head in sand?

    Of course, another problem with the ML strategy is efficiency loss
    (which, though, is often negligible as discussed here before)

    --Hongwei Xi

    \~~~~/ \\ // \\ // @ Mail: hwxi@ececs.uc.edu
    C-o^o, ))__|| \\__//_ // \\ Url: http://www.ececs.uc.edu/~hwxi
    ( ^ ) ))__|| \--/-\\ \\
    / \V\ )) || // \\ \\ Tel: +1 513 556 4762 (office)
    ------ // || o // \\ \\//Fax: +1 513 556 7326 (department)



    This archive was generated by hypermail 2b29 : Thu May 11 2000 - 15:00:06 MET DST