12

I've been reading through the details of the System libraries set and get methods yet the parameters are usually Strings.

Would you consider the use of String as parameters bad practise since the inclusion of enum?

A better alternative at minimum might be public final String, No?

0

7 Answers 7

27

I would consider Enums to be a better approach than Strings. They are type safe and comparing them is faster than comparing Strings.

As a pre Java 1.5 alternative you could use the type-safe enum pattern suggested by Joshua Bloch in his book Effective Java. For type-safe enums see also http://www.javacamp.org/designPattern/enum.html

Sign up to request clarification or add additional context in comments.

Comments

21

If your set of parameters is limited and known at compile time, use enum.

If your set of parameters is open and unkown at compile time, use strings.

Comments

7

Just because you declare a public final String as something you expect to have passed into a method as a parameter, there's nothing stopping me from passing in anything I like.

Using enums means that I can't create my own object to pass in, protecting both sides of the issue. The only time I can think you should be using constant Strings in lieu of enums is if you need to allow room for the user to extend your method to enable custom functionality...

1 Comment

@Martin: I wss thinking of comparing the inputs to the String const otherwise throwing an invalid input exception, not nice. I asked the question because of the suggestion of open keys, this seems to reduce the WORA concept within Java.
4

If you're referring to System.setProperty(), System.getProperty(), or System.getenv(), I think Strings are appropriate here since the set of possible keys is open. The key parameter corresponds to an actual text/string type value in some file or store somewhere.

If you have a closed set of keys, I think enums would be much preferred.

2 Comments

My thought on this was that an enum can have mapping details for non platform specific settings, that both should be used not just strings, and that string methods should be deprecated to encourage the adoption of enums
I think the distinction between "closed" keysets and "open" keysets is the most important factor here. If you look at an API with dynamically loaded plugins, the plugins may contribute additional functionality which can be accessed via a String parameter, but would be more difficult with an enum.
4

Although using enums is type safe, the need of converting enum to string and vice versa is quite frequent. And there is no built-in feature to do that in Java. And you'll eventually end up with using valueOf() and toString(). And using that approach will be no much different from using just strings. Because you'll need to handle situations where string can not be converted to Enum.

So just using static final strings is easy and is a common practice, AFAIK.

For example, you'll want to interact with a server using some API. You'll need to define every method and a response as Enum. And then you'll need to add toString and valueOf methods. Why just not use String?

Comments

4

I've learned the "method of least surprise". Instinctively, using enum is the right thing. So i would go for it. I'm sure the Java makers think alike.

Edit: Excellent explanation of POLS: http://benpryor.com/blog/2006/06/29/api-design-the-principle-of-least-surprise/

Comments

0

Usage of strings in existing APIs is not bad practice; it is bad practice to change the APIs just because Java has now support for enums. For new APIs, I agree with what everybody else said.

1 Comment

Overloading? Copy of my comment to lycono: "My thought on this was that an enum can have mapping details for non platform specific settings, that both should be used not just strings, and that string methods should be deprecated to encourage the adoption of enums"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.