Java Magazine, May/June 2018
ORACLE COM JAVAMAGAZINE MAY JUNE 2018 71 java 10 List City cities citiesInCountry getValue However if you had used the var keyword and type inference you would need to alter the first line of code as follows but you would not need to alter the other lines var countryToCity new HashMap String List City for var citiesInCountry countryToCity entrySet var cities citiesInCountry getValue This example illustrates a key principle to follow when using var Dont optimize for ease of writing or for ease of reading optimize for ease of maintenance If you optimize for ease of maintenance that should balance readability with the amount of code that needs to be changed as your program evolves over time It would be foolhardy to claim that adding type inference is always a positive for your code sometimes having explicit types in code can help readability This is particularly the case when the type isnt obvious from the expression that generates it In the following code it would be better to have explicit types because you dont know from just reading the getCities method call what it is returning Map String List City countryToCity getCities var countryToCity getCities This discussion leads to one final recommendation when it comes to readability and var variable names matter Because var removes the ability of the reader of your code to guess at the
You must have JavaScript enabled to view digital editions.