Rationale for "Please use string literals to identify properties."

Discussion in 'Java' started by Sigfried, Feb 4, 2009.

  1. Sigfried

    Sigfried Guest

    Hi,
    in javadoc of "javax.swing.table.TableColumn.COLUMN_WIDTH_PROPERTY",
    there is this sentence "Please use string literals to identify
    properties.". Could you explain what is the ratonale for that, plz ?
    Sigfried, Feb 4, 2009
    #1
    1. Advertising

  2. > On Wed, 04 Feb 2009 06:36:25 -0800, Sigfried <> wrote:
    >> in javadoc of "javax.swing.table.TableColumn.COLUMN_WIDTH_PROPERTY",
    >> there is this sentence "Please use string literals to identify
    >> properties.". Could you explain what is the ratonale for that, plz ?


    Perhaps, they just didn't see any other way out once the typo ("columWidth")
    slipped through. ;-)
    Andreas Leitgeb, Feb 4, 2009
    #2
    1. Advertising

  3. Sigfried

    Sigfried Guest

    Peter Duniho a écrit :
    > On Wed, 04 Feb 2009 06:36:25 -0800, Sigfried <> wrote:
    >
    >> Hi,
    >> in javadoc of "javax.swing.table.TableColumn.COLUMN_WIDTH_PROPERTY",
    >> there is this sentence "Please use string literals to identify
    >> properties.". Could you explain what is the ratonale for that, plz ?

    >
    > Just looking at the docs, and Google (Google can be very useful...you
    > might have heard of it :) ), it looks to me as though this was the
    > string you used to look for when listening for property changes on the
    > TableColumn class (see addPropertyChangeListener()). But now they say
    > to just use a string literal for the name of the actual property you're
    > interested (e.g. "width").


    I understand the sentence, i was asking for a reason, a rationale...

    This specific constant has a typo in it, but others (in TableColumn
    especialy) have the same advice. Is there a good reason for that ?
    Sigfried, Feb 5, 2009
    #3
  4. Sigfried

    blue indigo Guest

    On Thu, 5637 Sep 1993 10:57:14 -0800, Peter Duniho wrote:

    > Or if the string literal itself is where the typo is, perhaps it's as
    > Andreas suggests: Sun realized that it's better to just have the API
    > respond to names already documented elsewhere, rather than providing a
    > separate constant that has to match (and thus offering an opportunity for
    > it to be wrong).


    Isn't the normal and best-practise thing to do here to use the same
    constant where the code reads strings looking for a match as in the code
    that passes that code a string?


    public void accept (String s) {
    if (!s.equals("foo")) throw new IllegalArgumentException();
    }

    public void test () {
    accept("bar");
    }

    compiles but fails at run-time; and as you noted,


    public static final String THE_STRING = "bar"

    public void accept (String s) {
    if (!s.equals("foo")) throw new IllegalArgumentException();
    }

    public void test () {
    accept(THE_STRING);
    }

    fails at run-time because THE_STRING has gotten out of synch with the
    literal in "accept". But


    public static final String THE_STRING = "bar"

    public void accept (String s) {
    if (!s.equals(THE_STRING)) throw new IllegalArgumentException();
    }

    public void test () {
    accept(THE_STRING);
    }

    succeeds, and


    public static final String THE_STRING = "bar"

    public void accept (String s) {
    if (!s.equals(THE_STRONG)) throw new IllegalArgumentException();
    }

    public void test () {
    accept(THE_STRUNG);
    }

    fails at compile time (at both mismatches).


    --
    blue indigo
    UA Telecom since 1987
    blue indigo, Feb 5, 2009
    #4
  5. Sigfried

    Lew Guest

    Re: Rationale for "Please use string literals to identifyproperties."

    blue indigo wrote:
    > Isn't the normal and best-practise thing to do here to use the same
    > constant where the code reads strings looking for a match as in the code
    > that passes that code a string?


    That'd be why the OP asked,
    >> Could you explain what is the ratonale for [using literal Strings], plz [sic]?


    --
    Lew
    L33tspeek has no place here.
    Lew, Feb 5, 2009
    #5
  6. Sigfried

    Lew Guest

    Re: Rationale for "Please use string literals to identifyproperties."

    blue indigo wrote:
    > Isn't the normal and best-practise thing to do here to use the same
    > constant where the code reads strings looking for a match as in the code
    > that passes that code a string?


    Second-best practice. The best practice nowadays is to use an enum.

    --
    Lew
    Lew, Feb 5, 2009
    #6
  7. Sigfried

    blue indigo Guest

    Re: Rationale for "Please use string literals to identify properties."

    On Thu, 05 Feb 2009 12:46:33 -0800, Lew wrote:

    > blue indigo wrote:
    >> Isn't the normal and best-practise thing to do here to use the same
    >> constant where the code reads strings looking for a match as in the code
    >> that passes that code a string?

    >
    > Second-best practice. The best practice nowadays is to use an enum.


    Even as a key in a .properties? ;-)

    --
    blue indigo
    UA Telecom since 1987
    blue indigo, Feb 8, 2009
    #7
  8. Sigfried

    Lew Guest

    Re: Rationale for "Please use string literals to identify properties."

    blue indigo wrote:
    >>> Isn't the normal and best-practise thing to do here to use the same
    >>> constant where the code reads strings looking for a match as in the code
    >>> that passes that code a string?


    Lew wrote:
    >> Second-best practice. The best practice nowadays is to use an enum.


    blue indigo wrote:
    > Even as a key in a .properties? ;-)


    Actually, yes, IMHO. (Though, naturally and easily-inferrably, via a
    'toString()' conversion.) Type safety at compile time is worth the slight
    extra effort.

    That seemed like a serious question. Why the winkey?

    --
    Lew
    Lew, Feb 8, 2009
    #8
    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. Harald Kirsch
    Replies:
    0
    Views:
    362
    Harald Kirsch
    Jun 14, 2004
  2. Alexander Grigoriev
    Replies:
    0
    Views:
    364
    Alexander Grigoriev
    Sep 12, 2003
  3. Asfand Yar Qazi
    Replies:
    5
    Views:
    353
    Siemel Naran
    Apr 7, 2004
  4. Christopher Benson-Manica

    gets() rationale

    Christopher Benson-Manica, Dec 2, 2003, in forum: C Programming
    Replies:
    52
    Views:
    1,389
    Mark McIntyre
    Dec 15, 2003
  5. Matt Feinstein

    Rationale for core Python numeric types

    Matt Feinstein, Jun 16, 2004, in forum: Python
    Replies:
    24
    Views:
    820
    Michael Hudson
    Jul 26, 2004
Loading...

Share This Page