Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to name a dictionary?

Are there any conventions / guidelines for naming associative arrays (e.g. Dictionary<TKey, TValue>) in .NET?

For example, is there a better way to name dict in:

var dict = new Dictionary<Foo, Bar>();

Some names I've used / seen:

foosToBars
mapFromFooToBar
barsByFoo
barsForFoos

I've also seen some types in the BCL choosing 'nicer' sounding names that don't directly reveal what the key and value are supposed to represent, such as in this method.

How about for multimaps (e.g. ILookup<TKey, TElement>)?

EDIT:

Perhaps my example was slightly poor; I didn't mean to focus on the types of the key and value.

How about if it was:

// key = Name of person, value = Age of person
var dict = new Dictionary<string, int>();

With the samples updated to:

namesToAges
mapFromNameToAge
agesByName
agesForNames
like image 981
Ani Avatar asked Mar 23 '11 10:03

Ani


People also ask

How do you name a dictionary in Python?

You declare a dictionary with a set of curly braces, {} . Inside the curly braces you have a key-value pair. Keys are separated from their associated values with colon, : .

Can we name a dictionary in Python?

Objects don't have names in Python, a name is an identifier that can be assigned to an object, and multiple names could be assigned to the same one. However, an object-oriented way to do what you want would be to subclass the built-in dict dictionary class and add a name property to it.

What data types can be used as key for dictionary?

For example, you can use an integer, float, string, or Boolean as a dictionary key. However, neither a list nor another dictionary can serve as a dictionary key, because lists and dictionaries are mutable. Values, on the other hand, can be any type and can be used more than once.

Which of the following restrictions do Python dictionaries have?

Properties of Dictionary Keys: Dictionary values have no restrictions. They can be any arbitrary Python object, either standard objects or user- defined objects. However, same is not true for the keys.


1 Answers

I'd try to follow the example you linked to, where you don't name the dictionary after what it contains but you name it for what it's for. It doesn't take long for someone to determine that "dict" is a dictionary that maps Foo keys to Bar values, but what i'd want to know is why you're doing that.

like image 156
Rob Fonseca-Ensor Avatar answered Sep 22 '22 08:09

Rob Fonseca-Ensor