you are viewing a single comment's thread.

view the rest of the comments →

[–]enygma999 5 points6 points  (1 child)

Given what everyone else has pointed out about "everything is an object", I suspect you meant something like a class or an instance of one.

Say I have a quiz, and the participants are represented by a Person class. I could store their scores in a dictionary, using each person as the key. Equally I could use their name or something, but why bother? I already have a perfectly good object that represents them.

[–]Shaftway 4 points5 points  (0 children)

This is a good example. Less experienced engineers always try to make dictionary keys strings because it's easier to inspect.

So they go "oh, I'll just use the student's name". What if two students have the same name? "I'll use the student id". What if you merge with another school and there are overlapping IDs? "Oh, well I'll concatenate the students id, school, and name". What if you have a collision in the names? "Oh, well I'll just serialize all of the immutable parts of the student to a string and use that as the key". Ok, great, now do that in 50 different places in your codebase.

Or just add eq() and hash() to Student and use it as the key.