开发者

How should I name a java.util.Map? [closed]

开发者 https://www.devze.com 2022-12-20 12:58 出处:网络
Closed. This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing
Closed. This question is opinion-based. It is not currently accepting answers.

Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.

Closed 3 years ago.

Improve this question

I have a java.util.Map that maps from a logical name to a set of parameters to use with that name.

Map<String,Parameters> ho开发者_C百科wShouldINameThee = ...;

What is the best name for this map?

Should I go simple and just call this parameters or parametersMap?

Do I include information about the key in the name like paramtersByName so that how to use the String key is more obvious?


A Map maps something to something else.
I like to use names like uidToPerson. "To" being the shortest unambiguous way I can think of to show that I have a map.

Edit:
I'll add that I prefer to have the map named this way, because "key" and "value" appear in that order in the name. As opposed to valueByKey. In mapping operations, the key comes first. You put(key, value) or get(key) that gives a value.

Of course this is a matter of personal preference.


I tend toward something like parametersByName to leave no confusion about what the contents of the Map are. You never know when you're going to have to revisit code that you haven't looked at in a long time.

In Java, I find it unnecessary to include the name of the data structure (like parametersByNameMap) since the typing is explicit.


You actually answer the question yourself.

What is the best name for this map?

You want to map your map to a name, so you say 'name for map' !!

That should be the naming convention, in my opinion: valueForKey.

With the other suggestions keyToValue and valueByKey, I feel you need to add the word Map at the end, like this: keyToValueMap, valueByKeyMap. When you use For it's apparent from the language that it is a mapping.


In my apps there would be quite many types of parameters.

For example, in GAE, when I need to extract the http request parameters into a serializable form, I name the map httpRequestParameters or httpReqParams. sessionAttrs, for example.

For GWT RPC, client-to-server parameter hash, I would name it client2ServerParams or clnt2SrvrParms and name the counterpart server2clientParams or srvr2ClntParms.

In openid consumer, I would name the map, consumerAuthRequests or redirectFormParameters and its counterpart providerResponses.

In the map of reformatted input Main arguments, I would call it inputArgs.

In my cases, httpRequestParametersBy name, client2ServerParamsByName, consumerAuthRequestsByName, inputArgsByName, or inputArgValueByKey, etc would be redundant and too long because I would always know that the key of the map is a "name" anyway. I just make sure the name is plural to give me an inkling that it is a collection.

Exception to this practice is when the key is not a name but an object than I would name the map like vehicleByDriver, projByMgr, toxicFoodListByAnimal.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号