Does using an enum make an application less portable?
I know this question sounds bizarre, but that's what my manager told me. When I proposed a new design that made use of an enum
, he said, "No, ...", and then went on to make an offhand comment that making use of such a construct actually makes the application "less portable".
Is this true? I suspect that开发者_运维问答 it's not.
You should ask him for me details... it does mean you wouldn't be able to compile the code in Java 1.4 or earlier, but that's not a terribly good justification.
Sounds like your manager may be confused... but you should always press for details.
Does the application interact with other programming languages, i.e., via an API? That's the only sense in which I can imagine that an enum would cause problems. Otherwise, it's just an integer/long underneath in most languages and provides an extra layer of compile-time safety checking that can only make your program more robust.
Unless your manager wants to maintain portability with JDK 1.4 and below he is wrong. There is nothing unportable in enum as it's part of Java language. Can he elaborate his point?
Two issues come to mind with enums.
First is version portability. 'enums' have been with java since version 5.0 (aka 1.5). So if he's interested in maintaining backward compatibility with older systems, this may be a good cause for concern (http://en.wikipedia.org/wiki/Java_version_history#J2SE_1.4_.28February_6.2C_2002.29).
The other issue that comes up, sometimes, is how to serialize/store enumerations. Enums are generally stored in memory as some index which gets translated by the compiler that have little to do with the actual string. This is great for program performance and is generally fine if you never need to save / store / share your enumerations. But it can cause a lot of trouble when you try to serialize / store it to databases for others to read.
精彩评论