How to name a HashMap in Java?

后端 未结 9 1834
难免孤独
难免孤独 2021-02-01 00:55

This might be a silly question, but I have never found a satisfying way to name a variable of type HashMap in Java. For example - lets say I have a

9条回答
  •  迷失自我
    2021-02-01 01:03

    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.

提交回复
热议问题