0

I know How to name a HashMap in Java?, but what is the best name for the function that does the work of building and assigning the keys? A function that returns Map<State, County> countiesByState could be named

sortToState(myCounties)
mapByState(myCounties)
getCountiesByStateMap(myCounties)
getCountiesByState(myCounties)
getMapByState(myCounties)
Noumenon
  • 5,099
  • 4
  • 53
  • 73
  • What does the method do? Is it just a getter method? Also, what does the containing class do and how client code should be using the method? – Mick Mnemonic Jul 20 '18 at 18:55
  • It's doing complicated stuff like sorting the items into buckets or deciding which fee code a transaction belongs under. The map does not exist yet, we are creating it. Containing class might just want everything sorted out so it can print a report. – Noumenon Jul 20 '18 at 18:56
  • I appreciate the quick accept! – GhostCat Jul 21 '18 at 04:57
  • 1
    I appreciate that someone on this site still recognizes questions about best practices have real answers even if there is some opinion to them. – Noumenon Jul 21 '18 at 05:05

1 Answers1

5

To a certain degree, this is about style. Therefore you should rather ask the people working on this code base about what they consider the best name in your project. It doesn't matter what the "global best practice" is here, what matters is what works best for you!

Nonetheless, the clear winner is getCountiesByState(myCounties):

  • there is no need to specify the collection type anywhere (fooByBar already tells us that this is about a mapping!)
  • and sortToState() is a bit misleading, as it is not making clear that it will return a map!

As that sorting aspect seems to be important, why not getSortedCountiesByState(myCounties) or getCountiesSortedByState(myCounties) or getCountiesBySortedStates(myCounties)` ...

GhostCat
  • 137,827
  • 25
  • 176
  • 248
  • `sortToState` is what prompted me to make the question, as it sounds to me like it should return a sorted TreeMap. It's a non-native speaker just trying to assign things into buckets, like you would sort out coins by quarter, dime, nickel. Not producing anything ordered by their value or anything like that. – Noumenon Jul 20 '18 at 19:00
  • 1
    See my updates. The question is rather: is sorting a byproduct, or *the one essential* property of your collection?! That is for you to decide! – GhostCat Jul 20 '18 at 19:01