locked
Why use an Enum RRS feed

  • Question

  • User1445074224 posted

    Hi.

    Here's what I understand about enums (to save unnecessary responses):

    • enums can group together related constants, such as days of the week under a new type which can be accessed like a static member of a class
    • enums are by default integers
    • enums can associate a series of integers with a "more meaningful" series of words: so we could assign days of the week to the numbers 1 - 7 or months of the year to numbers 1 - 12, etc.
    • I understand how to creat and access them in code

    I have Googled until I can't focus my eyes anymore a range of phrases which include the word "enum" (including pretty much the same question as mine posted earlier on this site).

    ALAS...

    Although I "get" the above, what I do NOT get is why we would want to do this. For example, I am reading again and again tutorials which say, "Look, you can declare an enum containing days of the week the write each day to the console with Console.WriteLine(WeekDays.Monday), Console.WriteLine(WeekDays.Tuesday)" And, " You can do this: Console.WriteLine((int)WeekDays.Monday) and get the number 1, wow, isn't that cool!

    And I'm thinking... no! It's a shedload of code just to output Monday and 1 to the console.

    But, as usual, I have no doubt that my negative response is not created by the tutorial offering me something useless and is, instead, created by me missing the whole point.

    So...

    Could I ask somebody to please offer a response to the question "Why should we use enums?" in a "beginner's" way:

    • here's a scenario where enums would be most appropriate
    • here's how we'd do it WITHOUT enums
    • here's what's wrong with this approach
    • here's how we'd do it WITH enums
    • here's why this approach is better.

    It would be useful if, instead of discussing only the "code block" of the example you use, that:

    • some indication is given of the surrounding code, i.e. the context in which the scenario is working (even if that is just pseudo code), and
    • discussing the linking of the integer values to a database (which seems to be the only practical use I can think of to add the integer values manually, rather than using the auto-generated zero-based numbers)

    I'm just not getting this from the many sites I have looked at but, judging by the sheer number of sites offering some form of answer to this same question, I'm certainly one of hundreds or even thousands who are missing the point.

    Thanks in advance.

    Wednesday, September 14, 2016 9:14 AM

Answers

  • User303363814 posted

    The main thing for me is ease of understanding

    if (currentDay == WeekDays.Tuesday) {// blah, blah blah

    I think is pretty clear about what the original programmer was trying to do

    if (currentDay == 3) { // ...

    My brain goes into meltdown.  "Is the first day of the week on this platform Sunday or Monday?", "Is the first day of the week 0 or 1?", "therefore 3 could mean Tuesday, Wednesday, Thursday or Friday - where is that *(&^*&% documentation about weekday numbering."

    There goes another 10 minutes of my life that I will never get back.

    The person trying to debug your code at 3am could be an axe murderer who knows where you live.  Make life easy for them.

    The person trying to debug your code could be you.  Be nice to yourself.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 14, 2016 10:20 PM

All replies

  • User-821857111 posted

    I tend to use enums to represent relatively fixed lists of a limited number of options. If the options are subject to fairly frequent change or there are a lot of them, I use a lookup table in a database instead.

    Advantages: they give me strong typing e.g:

    var campingHolidays = context.Bookings.Where(b => b.BookingType == BookingType.Camping);

    as opposed to 

    var campingHolidays = context.Bookings.Where(b => b.BookingType == 3); // or is it 4??

    Disadvantages: you have to check the source code to remember what value to query for if you are writing SQL (which is why I use them for smallish options lists), and you have to recompile the app if you want to add new options.

    Wednesday, September 14, 2016 12:01 PM
  • User1445074224 posted

    Thanks for your response Mike.

    So... let me understand you...

    Advantages: they give me strong typing e.g:

    var campingHolidays = context.Bookings.Where(b => b.BookingType == BookingType.Camping);

    as opposed to 

    var campingHolidays = context.Bookings.Where(b => b.BookingType == 3); // or is it 4??

    Are you saying that, if you had a database table, like so:

    bookingType

    ID                              Accommodation

    1                                Lodge

    2                                Caravan

    3                                Camping

    which stores the "unique"different types of accommodation as ID's that it is easier to know (when you want "Camping") that BookingType.Camping is what you need as you have the word camping in the code, whereas you'd have to remember which ID's went with which accommodation types in your table (or at least have to look it up)?

    ... and ...

    when you say

    you have to recompile the app if you want to add new options

    does this mean that you wouldn't even think of using enums to allow the website user to (let' say she owns the travel operation and has a CMS) modify her accommodation types by adding a new one, i.e. you'd just use a database for this functionality?

    Am I thinking along the right lines here?

    Wednesday, September 14, 2016 3:59 PM
  • User303363814 posted

    The main thing for me is ease of understanding

    if (currentDay == WeekDays.Tuesday) {// blah, blah blah

    I think is pretty clear about what the original programmer was trying to do

    if (currentDay == 3) { // ...

    My brain goes into meltdown.  "Is the first day of the week on this platform Sunday or Monday?", "Is the first day of the week 0 or 1?", "therefore 3 could mean Tuesday, Wednesday, Thursday or Friday - where is that *(&^*&% documentation about weekday numbering."

    There goes another 10 minutes of my life that I will never get back.

    The person trying to debug your code at 3am could be an axe murderer who knows where you live.  Make life easy for them.

    The person trying to debug your code could be you.  Be nice to yourself.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 14, 2016 10:20 PM
  • User36583972 posted

    Hi banksidepoet,

    When we have a fixed type, we'll set it to enumerate. I think it usually has the following advantages.

    1: Enumeration as the return type is easy operation

    2: The judgment will be more efficient

    3: Improve code maintainability and ensure legal variables.

    4: Improve code readability and make the program safer.

    Best Regards,

    Yohann Lu

    Thursday, September 15, 2016 5:25 AM
  • User1445074224 posted

    PaulTheSmith,

    Cheers, buddy. We have the same sense of humour (and let's face it, anyone coding for a living who doesn't have a great sense of humour probably is, or will soon be, an axe murderer!)

    To all Responders,

    Between you all, I think I have this. I can see how much more readable enums are and I can also appreciate the code safety and maintainability involved. I think I've come from a place where enums were mystical wizards whose purpose was known only to those who spoke "wizard" to a place where enums are actually nothing that special, just a clearer and safer way of handling a kind of "drop down list" of possibilities.

    Thanks to you all...

    Thursday, September 15, 2016 11:07 AM
  • User-821857111 posted

    bookingType

    ID                              Accommodation

    1                                Lodge

    2                                Caravan

    3                                Camping

    which stores the "unique"different types of accommodation as ID's that it is easier to know (when you want "Camping") that BookingType.Camping is what you need as you have the word camping in the code, whereas you'd have to remember which ID's went with which accommodation types in your table (or at least have to look it up)?

    I would either have a database lookup table like above or an enum. I wouldn't have both. That will result in a problem in that I would now have two places to maintain the same value. I will forget to update one of them and the the users will complain when the app develops run time errors. But otherwise, yes - the options are easier to remember if they are presented as text, and if IntelliSense automatically provides me with all of the options to pick from.

    does this mean that you wouldn't even think of using enums to allow the website user to (let' say she owns the travel operation and has a CMS) modify her accommodation types by adding a new one, i.e. you'd just use a database for this functionality?
    Correct. If the user needs to  update values, you can't really expect them to do so in source code. 

    Thursday, September 15, 2016 11:51 AM
  • User1445074224 posted

    Mikesdotnetting,

    Thanks buddy,

    I've got this now. As usual, you've been very understanding and patient with a newbie.

    Thursday, September 15, 2016 12:09 PM