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?
I would call it mapStatesbyCountyCount, then again its bit lengthy variable name...
My opinion would be to have it
countiesCountOfStateMap
or simplycountiesOfStateMap
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.
Firstly avoid putting implementation details in the name of a variable, e.g.
StateToCountyMap
. Today you are using aMap
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 isto
which designates that this variable stores an association, the part before that is thekey
and the part after is thevalue
orvalues
that are associated to the correspondingkey
. Also it would be indistinct to name the variable asstateToCountyNumber
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 typeMap
or of typeint
.