How do you name Maps?
Maps of the key-value pair variety rather than the hip Google-enabled map of
the USA.
There seems to be no
convention. Even for individual programming languages, there seems to be no
convention enshrined in the culture, although some languages naturally favor shorter names than others.
Let’s say I’m mapping
each captain to their respective team. There are several naming strategies:
·
values as in teams. Succinct, but
doesn’t tell you what the keys represent. It’s usually not apparent from the declaration
(e.g. Map) or initialization (e.g. new HashMap()), so you’d have to go and find
code that uses it or read a comment. So it would be a particularly choice if
used as a parameter for a published API.
·
mapFromKeysToValues as in mapFromCaptainsToTeams.
Says it all, if a bit verbose.
·
mapOfKeysToValues as in mapOfCaptainsToTeams.
Ditto.
·
keysToValues as in captainsToTeams. More
succinct, but the shorthand form belies that its a noun. So someone reading “print(captainsToTeams)”
will probably interpret it as “print the map of captains to teams”. I’d rather
the structure be more clear at a glance.
I used “keyToValues”
for a long time, but I’ve recently switched to this:
·
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.
In all cases, I’ve
assumed plural. There’s certainly an argument to be made for singular form, as
in captainToTeam, or any combination. I find plural works best in most
contexts.
With template-style
structures such as Java 1.5 offers, values might be the way to
go. With a name like teams, you can’t immediately tell the key
type, but you can always locate the type in the declaration. That’s different
to the pre-1.5 situation, and dynamic languages, where you have to locate
actual code. With the key type being part of the map type, simply values is
probably the right trade-off. Unless the key type tells you little about the
meaning of the key, such as String, in which case I’m still running with keysToValues.
Reference:
No comments:
Post a Comment