const references -- how to document intent of const qualification

Discussion in 'Java' started by Andrew R. Thomas-Cramer, Jul 9, 2003.

  1. Java fails to provide a const-qualifier for references, for compile-time
    const checking.

    In the past, I've tended to document intended const-qualified references
    (e.g., method parameters, return values, fields, local variables) with C++
    syntax: "const*". Does anyone have a better suggestion, that doesn't
    require knowledge of C++?


    andy thomas-cramer
    Andrew R. Thomas-Cramer, Jul 9, 2003
    #1
    1. Advertising

  2. Andrew R. Thomas-Cramer wrote:
    > Java fails to provide a const-qualifier for references, for compile-time
    > const checking.
    >
    > In the past, I've tended to document intended const-qualified references
    > (e.g., method parameters, return values, fields, local variables) with C++
    > syntax: "const*". Does anyone have a better suggestion, that doesn't
    > require knowledge of C++?


    Method parameters, fields, and local variables may be declared final,
    which seems to be what you are looking for if I understand you
    correctly. I don't understand how the question is relevant to return
    values, but perhaps my C++ is too rusty.


    John Bollinger
    John C. Bollinger, Jul 9, 2003
    #2
    1. Advertising

  3. Andrew R. Thomas-Cramer

    Roedy Green Guest

    On Wed, 09 Jul 2003 17:16:12 -0000, Andrew R. Thomas-Cramer
    <> wrote or quoted :

    >In the past, I've tended to document intended const-qualified references
    >(e.g., method parameters, return values, fields, local variables) with C++
    >syntax: "const*". Does anyone have a better suggestion, that doesn't
    >require knowledge of C++?


    java has final which is similar. See
    http://mindprod.com/jgloss/final.html.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Jul 9, 2003
    #3
  4. Andrew R. Thomas-Cramer <> wrote:

    > Java fails to provide a const-qualifier for references, for
    > compile-time const checking.
    >
    > In the past, I've tended to document intended const-qualified
    > references (e.g., method parameters, return values, fields, local
    > variables) with C++ syntax: "const*". Does anyone have a better
    > suggestion, that doesn't require knowledge of C++?


    I believe the convention is to place a big warning text next to
    everything where immutability isn't enforced. For example, in
    javax.swing.event.EventListenerList:

    <quote>
    public Object[] getListenerList()

    Passes back the event listener list as an array of ListenerType-listener
    pairs. Note that for performance reasons, this implementation passes
    back the actual data structure in which the listener data is stored
    internally! This method is guaranteed to pass back a non-null array, so
    that no null-checking is required in fire methods. A zero-length array
    of Object should be returned if there are currently no listeners.
    WARNING!!! Absolutely NO modification of the data contained in this
    array should be made -- if any such manipulation is necessary, it should
    be done on a copy of the array returned rather than the array itself.
    </quote>

    Personally, I would write an even stronger warning, something like:

    WARNING!!! The returned array is not immutable, but MUST be treated as
    such.

    --
    No address munging in use. I like the smell of nuked accounts in the
    morning.
    Timo Kinnunen, Jul 10, 2003
    #4
  5. Andrew R. Thomas-Cramer <> wrote in message news:<>...
    > Java fails to provide a const-qualifier for references, for compile-time
    > const checking.
    >
    > In the past, I've tended to document intended const-qualified references
    > (e.g., method parameters, return values, fields, local variables) with C++
    > syntax: "const*". Does anyone have a better suggestion, that doesn't
    > require knowledge of C++?


    Create a const interface that your class implements and return this
    rather than a class reference. I've built my own type safe collections
    library with const interfaces. It works great. Say no to array copying
    :)

    /Jesper Nordenberg
    Jesper Nordenberg, Jul 10, 2003
    #5
  6. Tor Iver Wilhelmsen, Jul 10, 2003
    #6
  7. Andrew R. Thomas-Cramer

    Tim Tyler Guest

    Andrew R. Thomas-Cramer <> wrote:

    : Java fails to provide a const-qualifier for references, for compile-time
    : const checking.

    : In the past, I've tended to document intended const-qualified references
    : (e.g., method parameters, return values, fields, local variables) with C++
    : syntax: "const*". Does anyone have a better suggestion, that doesn't
    : require knowledge of C++?

    Look at:

    http://david.tribble.com/text/javaconst.html

    ....and...

    Need a 'const' qualifier
    Closed, will not be fixed
    http://developer.java.sun.com/developer/bugParade/bugs/4069541.html

    const - qualifier for methods, parameters and return-value
    Closed, will not be fixed
    http://developer.java.sun.com/developer/bugParade/bugs/4093718.html

    Java should support const parameters (like C++) for code maintainence
    In progress, request for enhancement
    http://developer.java.sun.com/developer/bugParade/bugs/4211070.html

    ....note that some people believe this:

    RFE: Add Immutable types to Java
    In progress, request for enhancement
    http://developer.java.sun.com/developer/bugParade/bugs/4617197.html

    ....is a more important umbrella proposal...

    ....and then vote accordingly.
    --
    __________
    |im |yler http://timtyler.org/
    Tim Tyler, Jul 10, 2003
    #7
  8. Andrew R. Thomas-Cramer

    Doug Pardee Guest

    Andrew R. Thomas-Cramer <> wrote:
    > Java fails to provide a const-qualifier for references, for compile-time
    > const checking.


    That's because it doesn't need it. "const" is a C++ kludge, needed
    only because C++ can't distinguish between value types and reference
    types.

    In Java, we distinguish between value types (primitives and
    immutables) and reference types (mutable classes).

    > In the past, I've tended to document intended const-qualified references
    > (e.g., method parameters, return values, fields, local variables) with C++
    > syntax: "const*". Does anyone have a better suggestion, that doesn't
    > require knowledge of C++?


    Use value types: primitives and immutables. For formal parameters,
    fields, and local variables, also specify 'final'.
    Doug Pardee, Jul 10, 2003
    #8
  9. Andrew R. Thomas-Cramer

    Roedy Green Guest

    On Wed, 09 Jul 2003 17:16:12 -0000, Andrew R. Thomas-Cramer
    <> wrote or quoted :

    >Does anyone have a better suggestion, that doesn't
    >require knowledge of C++?


    I have four suggestions at http://mindprod.com/jgloss/immutable.html.

    None give you the fine control of C++, but then in C++ I found most
    C++ programmers did not really understand the subtleties and then
    tended to either not use, or remove const.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Jul 11, 2003
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris
    Replies:
    0
    Views:
    518
    Chris
    May 28, 2004
  2. J.T. Conklin
    Replies:
    1
    Views:
    437
    David Hilsee
    Aug 11, 2004
  3. Dave
    Replies:
    1
    Views:
    1,246
    Victor Bazarov
    Jan 10, 2005
  4. Erik Hollensbe

    X12::Parser - intent to adopt

    Erik Hollensbe, Dec 31, 2007, in forum: Perl
    Replies:
    2
    Views:
    2,431
    brian d foy
    Jan 1, 2008
  5. Richard Kilmer

    Expressing intent in method calling...

    Richard Kilmer, Jun 8, 2004, in forum: Ruby
    Replies:
    8
    Views:
    171
    Robert Klemme
    Jun 9, 2004
Loading...

Share This Page