3

In some programming API's I see a list of methods to call, like getBoolean(String key, getDouble(String key), and getString(String key). Some other API's use a general get(String key) method, and return an Object which you are supposed to cast to an appropriate type yourself.

Now I am writing my own data accessing point, and I am wondering which approach to use. What are the advantages and disadvantages of each approach? When would you choose one over the other?

4 Answers 4

2

Advantage: getBoolean(), getDouble(), etc. allow you to return the respective primitive types. As far as I've seen, that's the primary reason anyone writes methods like that.

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

2 Comments

Why would this be an advantage? Modern Java versions use autoboxing, so the difference between Integer and int can be largely ignored (according to Java documentation). Is it still worth the trouble?
Primitive types should always be preferred to wrappers. Maybe the most compelling reason: what is the result of new Long(1).equals(1)?
1

Provide getters for the types that are most likely to be used. There's no real right or wrong way.

Comments

1

It depends on the purpose of the library. When the output is a predictable set of items, then toy can have specific names. As in ResultSet. If it is generic, then you will need generic get methods. Like ObjectOutputStream

In a very high level sense, you might need both: getBoolean, getDouble, getIngeter for the primitives (or their respective wrappers), a getString, for Strings and a generic get or a getObject for getting out objects.

However, this is a very generic answer for a very generic questions. What your tries to do very much decided such stuff.

Comments

1

Two questions: 1) Why don't you use general Properties instead, à là:

String getName() Address getAddress() Date getDateOfBirth() 

etc?

2) If you want to use methods like:

String getString(String key) Double getDouble(String key) Address getAddress(String key) 

How on earth would I, as the user, know, which key's are associated with objects of type String, which are associated with objects of type Double, etc?

I would recommend going with a solution similar to 1). If I didn't misunderstand your question, that is.

2 Comments

Answer to question 1: most of my methods do the same thing internally (communicating to a web API, handling exceptions), but are slightly different. Combining them in one method decreases code duplication. Answer to question 2: Well, if you (as the user), wouldn't know, how am I to use the ResultSet Java class then? I am wondering why API's are using this at all.. :)
To answer your question under option 2, just look at the JDBC ResultSet API. Quite often you, as the user, know more about the data you're requesting than a generic data holder does. Aside from that, the data could be representable in multiple formats. All other things being equal, I would choose an API that does those things for me over one that doesn't.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.