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?
I tend toward something like
parametersByName
to leave no confusion about what the contents of theMap
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.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. Youput(key, value)
orget(key)
that gives a value.Of course this is a matter of personal preference.
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.
You actually answer the question yourself.
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.