73

This might be a silly question, but I have never found a satisfying way to name a variable of type HashMap<K,V> in Java. For example - lets say I have a HashMap where every bucket is a <K,V> pair where K is a String say representing "State" and V is an Integer representing the number of counties the state has.

Should the HashMap be named as "mapStateCounty", "stateToCountyMap", etc. ? Which one seems logically more appealing and intuitive to understand without sounding confusing and verbose?

Jens Piegsa
  • 7,399
  • 5
  • 58
  • 106
user396089
  • 1,115
  • 1
  • 12
  • 26

10 Answers10

46

I don't believe there is a hard-written rule anywhere that tells you how to name your map, so as long as you come up with a rule that makes sense to you (and your teammates), then it should be fine.

Personally, I like to call my maps keyToValue or valueByKey.

John
  • 49
  • 1
  • 10
RAY
  • 6,810
  • 6
  • 40
  • 67
40

I like this question because Java does not allow map access via an operator like []. In other languages we could say things like

numberOfCountiesIn["HI"]

or

countyCountOf["CA"]

or

numCountiesIn->{"MA"}

or (in Scala, this is cool)

numCountiesIn("WA")

and on and on. None of these work in Java, because of that silly get word!

countyCounts.get("NY")

Indeed!

EDIT: I actually think countyCounts is the best answer (IMHO); I was just making the point that the need for get limits one's choices.

Ray Toal
  • 86,166
  • 18
  • 182
  • 232
  • 2
    I think `CountryCounts` would lead to the assumption that the variable contains a simple Collection like a List but not a Map. How would you distinguish between them? – Domi W Apr 08 '19 at 19:28
  • Nice observation! When `countyCounts` is used by itself, I'd grant the ambiguity. But when used in context with a `get`, and in `countyCounts.get("NY")` to me, at least, we know we are getting the count _for_ `NY` so it is a map, while if we saw `countyCounts[3]` it's an array, and `countyCounts.get(3)` is a Java list. Personally I am willing to put up with that ambiguity, but I agree it is a matter of preference. That silly `get` makes it awkward (in English anyway)! The flexibility you get in other languages allows `countyCountsFor[state]`, `countyCountOf`, `countiesIn`, etc. is so nice! – Ray Toal May 31 '19 at 16:05
  • 2
    I think there would be some value in specifying the nature of the keyset with a name such as `countyCountsByState` – Julius Oct 08 '19 at 14:41
16

Firstly avoid putting implementation details in the name of a variable, e.g. StateToCountyMap. Today you are using a Map interface but tomorrow you can decide to store your data in another type and the name of your variable has to be refactored otherwise it will become invalid and misleading.

Your data represents association of a state to its number of counties, so my advice is to name the variable as stateToNumberOfCounties. The key point in this name is to which designates that this variable stores an association, the part before that is the key and the part after is the value or values that are associated to the corresponding key. Also it would be indistinct to name the variable as stateToCountyNumber because one could not tell if it stores relation between a state and its number of counties or it stores a single number that represents the number of state to county associations, thus you would have to go back and forth in your code to check if it is of type Map or of type int.

Dimitar Bonev
  • 3,326
  • 1
  • 15
  • 16
  • `Map` is not always an implementation detail, though, as sometimes what you want is really a map from A to B. – Fifnmar Mar 21 '23 at 08:04
10

I found a great case for countiesByState here:

valuesByKeys, as in teamsByCaptains. If you're going to include both key and value, this seems to read best. At a high level, you can read it as just "teams", so anything that's performed on it is being performed on teams. The "byCaptains" prefix reads as it should do: a less significant qualifier that follows the teams around to help someone understand the structure if they need to.

This also allows you to access a value in a JSP with the nice notation countiesByState[myState].

Noumenon
  • 5,099
  • 4
  • 53
  • 73
9

I would call it numCounties or countyCounts.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • 10
    I sometimes name maps as if they were functions taking K as a parameter and returning V. That convention leads to names like the ones in this answer. – gatkin Jul 12 '11 at 04:40
  • I think this would lead to the assumption that the variable contains a simple Collection like a List but not a Map. – Domi W Apr 08 '19 at 19:24
  • 1
    I would think `numCounties` creates the assumption that the variable holds an _integer_, but I would guess, and this is heavily qualified by personal bias and experience, that `countyCounts` would generally invoke the idea of a map as opposed to a collection. But I will warrant that we are all different and it is difficult to please everyone. If Java did not require access with `get` there are better alternatives. Of course you could put the word `map` in the variable name, too :) Gotta love programming because there is rarely every a right answer. – Ray Toal Apr 09 '19 at 03:46
1

Whatever explains it best - In this case stateToCountyMap is ok or else countiesInStateMap can be used.

Nrj
  • 6,723
  • 7
  • 46
  • 58
1

I would call it mapStatesbyCountyCount, then again its bit lengthy variable name...

Charith De Silva
  • 3,650
  • 4
  • 43
  • 47
0

My opinion would be to have it countiesCountOfStateMap or simply countiesOfStateMap since we will be getting the counties count using the State.

It would be more meaningful to have it like this, so when new person looks into your code atleast he would be aware of what is contained in it.

But at the end of the day its your decision to have the appropriate name and as @tulskiy mentioned naming the class and variable appropriately is one of the harder things.

raksja
  • 3,969
  • 5
  • 38
  • 44
0

stateToCountyNumber. More generally, keyToValue for any map.

This naming rule is deeply rooted in the essence of a map — a map is a function in disguise. A map transforms a key to the corresponding value. Who also does the transformation work? Functions! toString, toDouble, to name a few. So I believe a map variable should be named in the same way functions are named.

-1

Storing states and counties in that variable it would be confusing to name it map - call it stateCountyHash or numCountiesByState

Tudor Constantin
  • 26,330
  • 7
  • 49
  • 72
  • 2
    “it would be confusing to name it map” – why? That’s what it does: it *maps* values from an input to an output domain. “hash”, on the other hand, is a particularly bad name since it’s describing an irrelevant (and maybe even wrong – what if the type of the map is subsequently changed to `TreeMap`?) implementation detail rather than the usage of the variable. – Konrad Rudolph Jul 12 '11 at 11:31
  • 1
    Does the *map* from the name reffers to a data structure or to the representation of geographical landscape? - in this context is confusing :) – Tudor Constantin Jul 12 '11 at 12:24
  • 1
    In mathematics, [map](http://en.wikipedia.org/wiki/Map_%28mathematics%29) describes pretty exactly what a `Map` in Java, or more generally a [map in computer science](http://en.wikipedia.org/wiki/Associative_array) does. The context (e.g. if the variable is called `mapStateToCountyCount`) should make it clear that its meaning in geography is irrelevant here. – Konrad Rudolph Jul 12 '11 at 12:47
  • mapStateToCountyCount does seem very reasonable, and it also leaves out the detailed implementation semantics such as whether it is referring to a - TreeMap or LinkedHashMap. Though map, county, state (all in one variable) might be tad confusing to someone coming from Geography/History background! – user396089 Jul 12 '11 at 23:09