Women in Technology

Hear us Roar



Article:
  Seven Low-Cost Ways to Improve Legacy Code
Subject:   Enums and "Constant Object"
Date:   2004-10-28 07:39:21
From:   dubwai
Normally what you call a "Constant Object" is called 'type-safe enum'. I don't really care for the way you use the term constant in relation to Java as there are no constants in Java. There are only finals. Calling them constants is confusing.


But that's not really what this post is about. While I agree with the use of enums if you have the ability to use 1.5, the example of a "Constant Object" could cause problems for an unsuspecting developer. If any of the instances of the Rainbow class you give are serialized and then deserialized, they will create new instances of the class that are not referenced by the class members.


If you are going to provide this example as a way to avoid bugs you need to mention that a good bit of extra work is required to make it serialize and deserialize properly. The example you give will likely introduce new bugs if used with serialization.

Full Threads Oldest First

Showing messages 1 through 7 of 7.

  • Enums and "Constant Object"
    2004-10-28 08:19:42  Robert Simmons, Jr. | O'Reilly Author [View]

    I address this fact in the book in the section on constant objects. Avoiding such a problem is a rather minor task with the readResolve() method. As for the term for it, that is personal preference. The improtant part is that the concept gets through. A

    s for the term "type-safe-enum" I personally think that would cause confusion since the code is not exactly declaring an enum (which has other semantics as well) but merely several objects that cannot be changed (IE constant). There is no convention that says that only integral and string and other primitive types are the only kinds of things that are constant.

    A constant is simply something that can not be changed; the antithesis of variable. As for the term "type-safe-enum pattern, I never read Blochs book until after I wrote my book. Its nto terribly suprising that more than one developer thought of it. Its a rather good idea as Bloch has said himself, no matter what you call it.
    • Enums and "Constant Object"
      2004-10-28 08:53:34  dubwai [View]

      "As for the term "type-safe-enum" I personally think that would cause confusion since the code is not exactly declaring an enum (which has other semantics as well) but merely several objects that cannot be changed (IE constant)."

      My mistake. The term is 'type-safe enumeration'. I call it a typesafe enum as an abbreviatioon and becasue there are no 'enums' in pre 1.5 Java (and arguably even in 1.5.)

      "There is no convention that says that only integral and string and other primitive types are the only kinds of things that are constant."

      String isn't a primitive type. Really what you are talking about are 'literals'. There are no constants in Java. We call static finals 'constants' because they work like them. If the 'const' keyword is ever introduced into Java (unlikely, I think) this will really be confusing.

      "A constant is simply something that can not be changed; the antithesis of variable."

      Well, not exactly but in any event using the term constant confuses developers because it implies that marking a reference constant makes the Object it references unchangeable. I can't even count how many times I've seen people confused about why Strings are Objects but act like primitives.
      • Enums and "Constant Object"
        2004-10-28 09:20:11  Robert Simmons, Jr. | O'Reilly Author [View]

        "In programming, a constant is a value that never changes. The other type of values that programs use is variables, symbols that can represent different values throughout the course of a program."[1]

        Since these objects are alocated at initialization and never change, they are constants.

        As for the terms for them, its a matter of splitting of irrelevant hairs. The importance is in the concept, not the label. Call them Freds for all I care.

        References:

        [1] http://www.webopedia.com/TERM/c/constant.html
        • Enums and "Constant Object"
          2004-10-28 09:30:40  dubwai [View]

          "As for the terms for them, its a matter of splitting of irrelevant hairs. The importance is in the concept, not the label. Call them Freds for all I care."

          That's fine for casual conversation but I don't think it's OK for technical books or articles.

          I have a book that describes certain well-known patterns improperly. The pattern described is useful but the name is normally assigned to something else entirely. This isn't just splittuing hairs. It puts the reader at a disadvantage when conversing with others. It's hard to communicate when people don't agree on the definitions of words.
          • Enums and "Constant Object"
            2004-10-28 10:02:27  Robert Simmons, Jr. | O'Reilly Author [View]

            Fine then. Id advise you to write your own articles if you feel your technical competence is so high and mine so low. You may find that its not as easy as you might think.

            Im having nothing more to do with this pointless conversation.
            • Enums and "Constant Object"
              2004-10-28 13:41:07  dubwai [View]

              A little defensive, there.

              I've never really understood that argument. If some one paints your portrait and it doesn't meet your expectations, are you not allowed to criticize the work unless you can do better?

              I may write a book some day. I don't think I'm really quite qualified to do so. I'll just leave it at that.
    • Enums and "Constant Object"
      2004-10-28 08:42:15  dubwai [View]

      "I address this fact in the book in the section on constant objects. Avoiding such a problem is a rather minor task with the readResolve() method."

      OK but I think you should mention it in the article. A developer may read this article but not the book. This can cause a nasty, hard to find bug. If the developer follows the advice in this article to change working code with static final ints to non-working code with 'constant-objects' it's not an improvement.