Is it bad to include the "name" of an object in a map?
When you have collections of object like a person, street, Irish dance troupe or etc. that has some sort of name identifier is it considered ok to store it in a map or is it considered a bad practice and should I just store it as a list and query the list each time?
Ex.
class Person:
def __init__(self,name,age):
开发者_如何学Go self.name=name
self.age=age
a = [Person("karl",50),Person("abe",20)]
vs
b = {"karl" : Person("karl",50), "abe" : Person("abe",20)}
edit: assume uniqueness, speed concerns might be of concern but general clarity is the goal.
I think it's rather risky, especially if you deal with multiple encodings, such as names that can be written in the Roman alphabet as well as another character set, such as Japanese, Russian, etc.
In addition, as @cdhowie mentioned, the notion of uniqueness is very important. If there are aliases used, which one is canonical?
Finally, how will you handle changes, such as correcting for typos?
I don't see a lot of upside and I see a lot of risks for future use.
The map gives you constant lookup time (good), but you're duplicating data (bad). If you really need the constant lookup time for performance reasons then use a map but be careful to keep the key synchronised with the value in the object. If it's not critical to performance, then just keep them in an array.
精彩评论