如何命名一个 returns 一个 HashMap in Java 的函数?

How to name a function that returns a HashMap in Java?

我知道 How to name a HashMap in Java?,但是构建和分配键的函数的最佳名称是什么? returns Map<State, County> countiesByState 可以命名的函数

sortToState(myCounties)
mapByState(myCounties)
getCountiesByStateMap(myCounties)
getCountiesByState(myCounties)
getMapByState(myCounties)

在某种程度上,这就是风格。因此,您应该向从事此代码库工作的人员询问他们认为您项目中的 best 名称是什么。这里的 "global best practice" 是什么并不重要,重要的是 最适合您的

尽管如此,明确 获胜者是 getCountiesByState(myCounties):

  • 不需要在任何地方指定集合类型(fooByBar已经告诉我们这是关于映射的!)
  • sortToState()有点误导,因为它不是明确表示它将return一张地图!

由于 排序 方面似乎很重要,为什么不 getSortedCountiesByState(myCounties)getCountiesSortedByState(myCounties) 或 getCountiesBySortedStates(myCounties)` ...