Why is this "good coding practice"? For example when I was making a Rock Paper Scissors console game most people suggested the use of enums instead of strings. What is the difference?
-
2http://stackoverflow.com/questions/11575376/why-use-enums-instead-of-constants – John Aug 08 '15 at 20:50
-
[Answer by Jon Skeet](http://stackoverflow.com/a/11575421/477420) as found by user3360241 has very good explanation, but unfortunately can't be used as duplicate due to C#/Java tag mismatch. Guffa's answer below provides C# flavor. – Alexei Levenkov Aug 08 '15 at 20:53
4 Answers
This is just one of the ways that you make the compiler work for you. If you use strings, the compiler can't help you spot a typo in the value:
if (move == "rokc") { // just never matches
but if you use an enum the compiler only lets you use valid values:
if (move == Moves.Rokc) { // compilation error

- 687,336
- 108
- 737
- 1,005
-
-
@funTertain: That's the biggest one, but there is also a performance benefit. An enum value is just a number in the end, and it's faster to work with numbers than strings. – Guffa Aug 08 '15 at 21:06
-
1Performance, prevent misspellings, better auto-complete from your development IDE. If you were writing an API for other developers: better (implicit) documentation for legal values for a variable, Note: You can also do things like Move.Rock.ToString() – Mike Aug 08 '15 at 21:49
-
@Mike so won't you agree that it's better to TryParse the player input(Console.ReadLine()) into a enumType and compare them rather than converting the selection of the computer which is of enumType into a string just so you could compare to the player's input leaving more room for error. An extra benefit would also be since the TryParse method provides a boolean parameter ignoreCase there would be no need for case-checking. – Wasiim Ouro-sama Aug 09 '15 at 18:59
-
@funTertain: You might want to make a function that parses the input and turns it into an enum, for example using a `switch`. Even if the user input happens to be the same as the enum identifiers, there is a point to separating the user interface from the values used internally. If you for example want to translate the application to a different language, you don't want to change the enum identifiers. – Guffa Aug 09 '15 at 19:51
Check this detailed description
An enumeration type (also named an enumeration or an enum) provides an efficient way to define a set of named integral constants that may be assigned to a variable.

- 3,334
- 3
- 23
- 50
First enums are just labels against numbers. This means that whilst your code presents papers, scissors and rocks - the program sees 1, 2 and 3. Computers don't have problems reading or remembering but humans do, so your code will read paper beats rock, whilst the computer sees 1 beats 3. That is important from a performance point of view.
The list is also finite. Using an enum means you can only have one of those three values or it won't compile. Using strings means your code has to constantly check for equality during runtime - so it becomes bloated, slower and error prone.

- 3,423
- 2
- 16
- 28
Partly, it is a mater of conciseness of communication. Strings are general things, enums are specific. Partly, it is to prevent mistakes. Using strings you can have spelling and case differences that can cause mistakes. If you misspell/mis-case an enum you will get a compile error. If you are just representing the three simple states of rock, paper, scissors, it may not seem too important, but as the code you write becomes larger you will find that you want to take every opportunity to reduce the change of mistake and the clarity of expression. Anyone who programs long enough will have the experience of having great difficulty in trying to understand code that they wrote in the past.

- 4,668
- 2
- 22
- 41