How should I name a java.util.Map?

JavaMapNaming Conventions

Java Problem Overview


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> howShouldINameThee = ...;

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?

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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.

Solution 3 - Java

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.

Solution 4 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlex BView Question on Stackoverflow
Solution 1 - JavaMark BolusmjakView Answer on Stackoverflow
Solution 2 - JavadanbenView Answer on Stackoverflow
Solution 3 - JavaeigilView Answer on Stackoverflow
Solution 4 - JavaBlessed GeekView Answer on Stackoverflow