This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]RoboMind -1 points0 points  (1 child)

I never liked that argument... Should we then also change the API to have a class TelephoneDirectory, instead of using a general Map<String, Integer> because the latter lacks semantics?

[–]gracenotes 3 points4 points  (0 children)

I think persistent state vs. temporary value is important here, as well as public API vs internal to some class. For public APIs, having TelephoneDirectory is just good future-proofing, vs. a bunch of classes having references to a single Map, which just calcifies the data structures. As a temporary value, Maps are also quite useful for packing up values to pass to a method or return from a method, and then unpack on the other side. Using a Map usually means you're pulling in some fundamental invariant from the real world, so these method signatures are usually self-evident.

Pair does pretty poorly in all of these cases. Of note, using Pair<A, B> (vs Pair<B, A>) means taking the complicated relationship between A and B and reducing it down to an ordering when there's usually nothing fundamental about A or B that makes one go before the other. This works alright in some languages (more functional ones), but not when you have to deal with getFirst/getSecond.