0

I have this example (in Java) where there are models, each with the same set of types. I defined the types as enumerations. When I went to present this program to the advisor (it's a project), he stated it wasn't wise to use enumerations as it makes the program inflexible for changes because enumerations are not extendable. So if I want to add another type at some point, I cannot do this without recompiling. Is there ever a situation where you'd prefer enumeration over creating objects instead? It seems to me that if you can choose between a flexible class with objects and a non flexible class with enums, you might as well use objects every time, even when you're '99%' sure the types will never change.

Babyburger
  • 1,730
  • 3
  • 19
  • 32
  • 2
    possible duplicate of [Enumerations: Why? When?](http://stackoverflow.com/questions/3363120/enumerations-why-when) – deb_rider Mar 30 '14 at 09:41
  • @deb_rider I saw that topic before. The issue I have is that enums are being compared to objects, rather than static variables (constants). If you create a class of objects, you can have objects of a class Month and objects of a class Week. Then you don' have problems like trying to compare Monday with January or doing things like Monday + 1 == February. – Babyburger Mar 30 '14 at 09:46
  • If your library has a built in assumption there is exactly 12 months in a year, providing the ability to add months is not going to work without a recompilation anyway. You want to limit users to options which will work. – Peter Lawrey Mar 30 '14 at 09:50

3 Answers3

2

Using enums improves static/compile time analysis. You can only use the enums you specified.

Whether this is good or not depends on whether the library is for internal or external consumption.

For internal consumption, re-compiling is not an issue. You have access to the source you can add another value whenever you need. Part of the value of an enum is the compiler time checking it gives you so re-compiling is a good thing in this case.

For external consumption, you have to be much more careful. You have to be more confident there is no supported reason to add another value. (There might be reasons to extend the values but if these are not supported, they can't do it anyway) A user of your library will not want to be/possibly won't be able to recompile your library just to add some functionality.

What I do is try to get the best of both worlds.

  • I define and use an interface
  • I have implementations which are enums

This allows you to use enums, but you allow a user of the library to define their own implementations which could be their own enums, or dynamically created instances.


Here is an example of an enum for an interface which could be extended but AFAIK, no one has. StopCharTesters

public enum StopCharTesters implements StopCharTester {
    COMMA_STOP {
        @Override
        public boolean isStopChar(int ch) {
            return ch < ' ' || ch == ',';
        }
    }, CONTROL_STOP {
        @Override
        public boolean isStopChar(int ch) {
            return ch < ' ';
        }
    },
    SPACE_STOP {
        @Override
        public boolean isStopChar(int ch) {
            return Character.isWhitespace(ch) || ch == 0;
        }
    },
    XML_TEXT {
        @Override
        public boolean isStopChar(int ch) {
            return ch == '"' || ch == '<' || ch == '>' || ch == 0;
        }
    },
    FIX_TEXT {
        @Override
        public boolean isStopChar(int ch) {
            return ch <= 1;
        }
    };

etc.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 1
    Thank you for your answer! I think using an interface is the best way to go too now that you mention it. Even if you're sure the enums you've decided on are never going to change, the unexpected might still happen somewhere in the future; so having the option to expand is always handy to have imo. – Babyburger Mar 30 '14 at 09:59
  • @Babyburger I have done this in many cases where the enums have never been "extended" AFAIK. There is little downside. – Peter Lawrey Mar 30 '14 at 10:12
  • @Babyburger I added an example. – Peter Lawrey Mar 30 '14 at 10:15
0

You'd use enumerations every time that there is a finite amount of possible states for an object. Every time, regardless of what your supervisor is saying. And yeah, take a look at this:

Enumerations: Why? When?

Community
  • 1
  • 1
Nikola Yovchev
  • 9,498
  • 4
  • 46
  • 72
0
  1. You prefer enumeration when its members can never be changed. For example if you are developing chess program you can be absolutely sure that the list of chess pieces will never be changed.
  2. when changing the members of enum requires other changes in code. For example if your enum presents encryption algorithm you cannot just add new algorithm. You have to implement it. This mean that your program has to be re-compiled anyway. Please note that this rule is not absolute: you can create program that discovers available implementation of specific interface dynamically.
AlexR
  • 114,158
  • 16
  • 130
  • 208
  • Say I made a chess game and got the regular pawns (king, queen, ...) and at some point, someone decides to extend the boardgame by adding two more columns on each side. It's no longer a chess game by the traditional standard, but an extension on a concept that exists; it wouldn't be possible to add a new type of chess piece if we were to use enums. I've seen examples where the directions NORTH,WEST,EAST,SOUTH are stated as enums. Then you can't go NORTHWEST. It seems to me you can never predict how your program will be used years down the line. (though irrelevant to the scope of my project) – Babyburger Mar 30 '14 at 09:57
  • I can predict that chess will never get new pieces. I can also predict that if you call your enum `BasicGeoDirections` it will never be extended because `NORTHWEST` is not a basic direction. – AlexR Mar 30 '14 at 10:00