如何命名java.util.Map?

时间:2022-07-18 19:37:16

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

我有一个java.util。将映射从逻辑名称映射到使用该名称的一组参数。

Map<String,Parameters> howShouldINameThee = ...;

What is the best name for this map?

这张地图最好的名字是什么?

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

我应该简单地调用这个参数或parametersMap吗?

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

我是否将关于键的信息包含在名称中,比如paramtersByName,以便如何使用字符串键更明显?

4 个解决方案

#1


69  

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.

地图把某物映射到另一物。我喜欢用uidToPerson这样的名字。“是我能想到的最短、最明确的方式来展示我有一张地图。”

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.

编辑:我要补充一点,我更喜欢以这种方式命名映射,因为“key”和“value”在名称中以这个顺序出现。而不是valueByKey。在映射操作中,首先是键。你输入(key, value)或get(key),给出一个值。

Of course this is a matter of personal preference.

当然,这是个人偏好的问题。

#2


21  

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.

在Java中,我发现不需要包含数据结构的名称(如parametersByNameMap),因为类型是显式的。

#3


7  

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.

在我看来,这应该是命名约定: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.

有了其他的建议keyToValue和valueByKey,我觉得您需要在末尾添加word Map,比如:keyToValueMap、valueByKeyMap。当你使用它时,从语言中可以看出它是一个映射。

#4


1  

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.

例如,在GAE中,当我需要将http请求参数提取到可序列化的表单时,我将映射命名为httpRequestParameters或httpReqParams。例如,sessionAttrs。

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

对于GWT RPC,客户机-服务器参数哈希,我将它命名为client2ServerParams或clnt2SrvrParms,并将其命名为对应的server2clientParams或srvr2ClntParms。

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

在openid使用者中,我将命名映射、consumerAuthRequests或redirectFormParameters及其对应的providerresponse。

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

在重新格式化输入主参数的映射中,我将它称为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.

在我的例子中,httpRequestParametersBy名称、client2ServerParamsByName、consumerAuthRequestsByName、inputargtargvaluebykey等都是多余的,而且太长了,因为我总是知道映射的键是“name”。我只要确保这个名字是复数就能让我知道它是一个集合。

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.

这种做法的一个例外是,当键不是一个名称而是一个对象时,我可以将其命名为vehicle - bydriver、projByMgr、poison foodlistbyanimal等。

#1


69  

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.

地图把某物映射到另一物。我喜欢用uidToPerson这样的名字。“是我能想到的最短、最明确的方式来展示我有一张地图。”

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.

编辑:我要补充一点,我更喜欢以这种方式命名映射,因为“key”和“value”在名称中以这个顺序出现。而不是valueByKey。在映射操作中,首先是键。你输入(key, value)或get(key),给出一个值。

Of course this is a matter of personal preference.

当然,这是个人偏好的问题。

#2


21  

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.

在Java中,我发现不需要包含数据结构的名称(如parametersByNameMap),因为类型是显式的。

#3


7  

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.

在我看来,这应该是命名约定: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.

有了其他的建议keyToValue和valueByKey,我觉得您需要在末尾添加word Map,比如:keyToValueMap、valueByKeyMap。当你使用它时,从语言中可以看出它是一个映射。

#4


1  

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.

例如,在GAE中,当我需要将http请求参数提取到可序列化的表单时,我将映射命名为httpRequestParameters或httpReqParams。例如,sessionAttrs。

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

对于GWT RPC,客户机-服务器参数哈希,我将它命名为client2ServerParams或clnt2SrvrParms,并将其命名为对应的server2clientParams或srvr2ClntParms。

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

在openid使用者中,我将命名映射、consumerAuthRequests或redirectFormParameters及其对应的providerresponse。

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

在重新格式化输入主参数的映射中,我将它称为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.

在我的例子中,httpRequestParametersBy名称、client2ServerParamsByName、consumerAuthRequestsByName、inputargtargvaluebykey等都是多余的,而且太长了,因为我总是知道映射的键是“name”。我只要确保这个名字是复数就能让我知道它是一个集合。

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.

这种做法的一个例外是,当键不是一个名称而是一个对象时,我可以将其命名为vehicle - bydriver、projByMgr、poison foodlistbyanimal等。