我应该如何命名 java.util.Map?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2253453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:16:03  来源:igfitidea点击:

How should I name a java.util.Map?

javamapnaming-conventions

提问by Alex B

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

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

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

What is the best name for this map?

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

Should I go simple and just call this parametersor parametersMap?

我应该简单点,就这样称呼parameters还是parametersMap

Do I include information about the key in the name like paramtersByNameso that how to use the Stringkey is more obvious?

我是否在名称中包含有关密钥的信息,paramtersByName以便如何使用String密钥更明显?

回答by danben

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

我倾向于parametersByName不混淆内容是什么Map。你永远不知道什么时候你必须重新访问你很久没有看过的代码。

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

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

回答by z5h

A Map maps something tosomething 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.

编辑:
我要补充一点,我更喜欢以这种方式命名地图,因为“键”和“值”在名称中以该顺序出现。与valueByKey. 在映射操作中,关键是第一位的。你put(key, value)get(key)那给出了一个价值。

Of course this is a matter of personal preference.

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

回答by Blessed Geek

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 及其对应的 providerResponses。

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

在重新格式化的输入 Main 参数的映射中,我将其称为 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 name、client2ServerParamsByName、consumerAuthRequestsByName、inputArgsByName 或 inputArgValueByKey 等将是多余的且太长,因为我总是知道地图的键无论如何都是“名称”。我只是确保名称是复数,以便让我暗示它是一个集合。

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.

这种做法的例外情况是,当键不是名称而是对象时,我会将地图命名为 VehicleByDriver、projByMgr、toxicFoodListByAnimal。

回答by eigil

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 Forit's apparent from the language that it is a mapping.

与其他建议keyToValue和valueByKey,我觉得你需要在最后加上Map这个词,像这样:keyToValueMap,valueByKeyMap。当您使用For 时,从语言中可以看出它是一个映射。