1

Example 1 (used at the moment)

If (BodyPart["Right Leg"].BodyStatusEffects["Active"].Active)
{
    BodyPart["Right Leg"].BodyStatusImpacts["Poisoned"].Active = false;

    BodyPart["Torso"].BodyStatusImpacts["Poisoned"] = 
        BodyPart["Right Leg"].BodyStatusImpacts["Poisoned"];

    BodyPart["Right Foot"].BodyStatusImpacts["Poisoned"] = 
        BodyPart["Right Leg"].BodyStatusImpacts["Poisoned"];
}

Example 2

BodyPart.[BodyParts.RightLegs].BodyStatusImpacts["Poisoned"].Active = false;

Which of these two examples is the preferred method? Is there a 3rd way that is even better?

Martin Maat
  • 18,218
  • 3
  • 30
  • 57
  • 2
    I haven't the slightest idea what you're asking here. What does "best" mean in this context? – Robert Harvey May 17 '17 at 13:54
  • Both your cases use string. Also, better in what sense? Readability? Performance? – Euphoric May 17 '17 at 13:56
  • 1
    This appears to be about "magic strings" in programming code. – Greg Burghardt May 17 '17 at 14:17
  • Possible duplicate of [Usage of magic strings/numbers](https://softwareengineering.stackexchange.com/questions/221034/usage-of-magic-strings-numbers) – Greg Burghardt May 17 '17 at 14:18
  • 1
    I assume you are asking whether to use enums or strings as the key type in a dictionary. Enums are nice because they are efficient and because it is harder to accidentally provide a wrong key. But it depends on the object you want to associate a value with. If that is already a string, there is little point in mapping the string to an enum first. You may not even know at compile time what strings you can expect, the user may have to provide the key. So it depends on your program, there is no straight answer. – Martin Maat May 17 '17 at 14:25
  • I don't see why this is downvoted. It lacks some details needed for easy understanding, but it is a valid and good question. – Tobias Knauss May 18 '17 at 04:56

2 Answers2

5

I would generally recommend using enums in this situation. Reasons:

  • It defines very clearly what is allowed and what is not allowed in the structures that are keyed with them, if you define the enum type as the key.
  • Your IDE will probably give you auto-complete functionality which is easier and faster than typing an open-quote, typing a string, then a close-quote.
  • If you ever need to generate a list of all keys used in your program, it is much easier to iterate over an enum than to search for all references to the underlying data structure and then find all the different string keys you used.
  • It's relatively easy to type the wrong string as a key and then waste time trying to figure out why your program won't work. Can't make that same mistake with an enum.
  • If you ever need to change an enum value, your IDE will probably let you refactor it very easily. With strings as keys, you'll be doing a lot of find-and-replace on the text and hope you got it all.

One problem with enums is that you can't add new values to an enum at runtime (at least I don't know of any way to), so if your program will need to be able to create entirely new keys based on user input at runtime, enums can't help you with that. But you could key your data structure with "soft" enums (the string values of the enums) and user-entered strings to get a sort of hybrid solution.

FrustratedWithFormsDesigner
  • 46,105
  • 7
  • 126
  • 176
  • Excellent answer. I agree in all points. Thats also what I do. About adding Enum values: Remember, an enum is just an integer. You can use any int value and cast it to an enum. It will not make it a declared value of your enum, but it will be of your enum's type. Not nice, but works. BTW: I also agree with your name! ;-) – Tobias Knauss May 17 '17 at 20:22
3

I see no code here that uses enum as a key. If you were hoping for type safety this isn't the way to find it.

Consider:

Dictionary<SomeEnum, SomeClass> aDictionary = new Dictionary<SomeEnum, SomeClass>();

aDictionary[SomeEnum.RightLeg]

The limitation here is now key's must be known statically. No sneaky taking arbitrary keys as input and using them to find things later. There are cases where using strings as keys is very useful. But you must guard against issues of uniqueness and typo's that enum's would take care of for you.

When you use code like:

BodyPart[BodyParts.RightLegs.ToString()]

you're getting a canned string. It's using enum to get a string to use as a key. Anyone is free to send a literal string into that dictionary at any time. Maybe that's good enough for you but it's not using enum as key. You aren't getting type safety.

candied_orange
  • 102,279
  • 24
  • 197
  • 315
  • you can also declare the string as a static constant somewhere – Ewan May 18 '17 at 14:04
  • @Ewan you can but that doesn't give you type safety either. That's just gives you a different way to organize constants. I'm not a big fan of using statics without good reason either. – candied_orange May 18 '17 at 14:30
  • true, but its another way of having string keys you cant misspell – Ewan May 18 '17 at 15:01