33

I was reading some code here and saw that an enum is used to store names of html tags. Why do we ever need to do this? What benefit do I get using this strategy?

I know that how useful enums are in compiled or statically typed languages but when I see enums in dynamically typed languages I get curious, like the example code I showed above. So, the question basically boils down to why do we need enums in dynamically typed language or do we need them at all?

Alex
  • 1,233
CodeYogi
  • 2,186

5 Answers5

57

A benefit is that the compiler can let you know if you accidentally type "ADRESS" or "FEILDSET", and letting you fix it immediately instead of behaving in a nonsensical way at runtime.

While the benefit is much more useful in statically typed languages than dynamic, it is still useful even if it is a runtime error as you will get message indicating a problem with your case statement rather than your data.

whatsisname
  • 27,703
22

Enums are useful for situations where you have a fixed set of values/entities that are sensible. They are self-documenting and allow the compiler validate things that would otherwise be left to run-time. They should never be used if the set of meaningful values is not known or not strictly limited.

A more useful example would be something like HTTP response codes. Instead of having a method that takes a number and provides the name and/or description of the error, you can have a set of enums with meaningful names, a code and a description etc. in one clean package that is authoritative in what values are allowed and need to be handled.

JimmyJames supports Canada
  • 30,578
  • 3
  • 59
  • 108
11

Enums have nothing to do with OOP, and JavaScript doesn't have enums. Instead, enums are used whenever there is a choice between a fixed set of values. For example, a boolean is a choice between true and false, which could be implemented as enum Bool { False, True }. In a GUI library, we might have an enum for alignments: enum HAlignment { LEFT = -1, CENTER = 0, RIGHT = 1 }.

It is usually irrelevant how the enum is implemented, the important part is that each possible value is distinct. Many languages use integers for enums, though some like Java support arbitrary objects.

Until now, we could just as well have used constants, e.g. const int LEFT = -1, CENTER = 0, RIGHT = 1. However, a compiler knows that the enum values belong together. So when I switch over the enum values switch(value) {case LEFT: ...; case RIGHT: ...;}, the compiler can warn me that I have forgotten the CENTER case. This can be a substantial time saver. In languages without enums or without a switch-case construct, this can be simulated with the Visitor Pattern, though that is more helpful in the presence of static typing.

The other advantage is that enums can be treated as a separate type. E.g. I can declare that a method takes an HAlignment parameter, rather than any integer. The code will then fail to compile if I provide anything but one of the three possible HAlignment values. However, C's enums aren't well encapsulated and the enum constants can be used interchangeably with integers. Other languages are stricter here.

In JavaScript, we get none of these benefits. The given example declares an object that is treated as an enum. This does have some advantages for the programmer, e.g. it makes documentation easier, groups all the “constants” into a single object, …. However, it is just a convention that such an object is enum-like.

The point here is that HTML only has a finite and known set of tags. You can look at the HTML5 specification and put those element names as an enum into your code, and therefore make it more difficult to sneak a <blink> tag into your program. It is better to encode this knowledge in one place that to litter your code with special string literals (or worse, magic numbers).

amon
  • 135,795
3

Even if your language doesn't require compilation, you'll probably use some kind of IDE or development tools, that can give much better support for something like an enum than just for strings.

If you use an enum like object literal in javascript for example, your editor will give you code completion and your code checker like JSHint or JSLint will warn you, if you accidentally use the wrong value.

hansmaad
  • 221
2

The point of such enum might be to provide Js Api (goog) a set/bundle of allowed tags. Which ones? The ones defined by W3C HTML 4.01 (check out enum's documentation). So it's settings boundaries.

Might or might not be this the real goal, however it would works fine for such purpose.

If you know how Javascript works, what code is doing is defining an array indexed by strings :-). Which value is a string, but it could be any other component with attributes, functions, etc... Let your imagination run free and you will see benefits everywhere.

Javascript aside, I use enums a lot for modeling and managing state machines.

START > IN_PROGRESS > CONFIRMED > FINISHED > ...

In java switch allows enums so is quite easy to validate states into a state machine, to loop all over the enum, to define priorities by doing complexed enums, ...

I also use them to define typed and unmodoficable constants:

  • Yes, No

Also complex enums (which allows to do secure transforms/parsers)

  • Yes(1,true), No(0,false)

Due to enums often belongs to my model layer (core), its features are accessible to all over the system, so it becomes a functional model and I keep a low coupling.

What enums gives (among other things) is boundaries and typification

Laiv
  • 14,990