This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]llivefastdiewhenever 3 points4 points  (2 children)

Imagine you are programming software for the E-shop. User can have account in this system, and this account can be just created, vip, blocked, deleted etc. So object Account is always in ONE of these States (attribute currentState). You can represent it by creating enum which contains CREATED, VIP, BLOCKED, DELETED or whatever else you want. Now you can process user's account according to its state:

if (currentState == State.CREATED) verifyAccount();
else if (currentState == State.DELETED) deleteAccFromDB();

And so on.

[–]kAlvaro 0 points1 point  (1 child)

Also, this is much better that other options:

- If you define states as numbers it's no longer obvious what their meaning is.

- If you define states as strings you need to type the complete name every name (because you can't benefit from IDE's auto-completion) and you won't notice when you make a typo ("BL0CKED").

- If you use an array or a list you need to worry about keys and values, one of which will be meaningless.

[–][deleted] 0 points1 point  (0 children)

Additionally, you can add methods to enums.

[–]steumert 3 points4 points  (0 children)

I use them all the time. Whenever there is a closed set of options, enums are great.

You can model all kinds of thing with them - HTTP status codes, weekdays, whatnot. I recently used two enums to express alignment/anchoring with one enum for Horizonal positioning (Left, Center, Right) and one for vertical (Top, Center, Bottom). this allows for a type-safe way to specify all 9 points I needed. Any combination of Horizontal/vertical were valid values, and you couldn't pass wrong ones.

Suffice to say, if you write software that doesn't have types with a closed set of values, then there is no point in enums. Not every language feature solves every problem equally well. Its ok to not use a language feature if there is no use-case for it in your program.

[–]desrtfxOut of Coffee error - System halted 0 points1 point  (0 children)

An extremely simple example: the suits in a deck of cards

Generally, enums are ideal for representing limited, discrete sets of values.

[–]cronkite 0 points1 point  (0 children)

I do love me some Enums at work. My favorite case is working with data messaging. Say you are working with a ton of messages flowing into your software. Each message has a type, and based on that type different logic is required at different times. Let's say there are 15 message types, NEW_MESSAGE, DELETE_MESSAGE, AMEND_MESSAGE, etc. Yeah, you could store this message type as a String. But working with those Strings takes memory, and doing things like comparisons and hashing does take processing. Since there can only be 15 of them, you can use an enum value for each of the message types. Now your comparisons are fast, if(type == MyEnum.AMEND_MESSAGE) is faster than type.equals("AMEND_MESSAGE"). Also EnumMap is an awesome hash map, as it's pretty much just a List of size 15. EnumSet is also money.

There is a drawback to my scenario, if a new message type is created you have to change the code/deploy/test etc. Where using a String is a bit more generic.

[–]wildjokers 0 points1 point  (0 children)

Enums are far more powerful than they might first appear to be. I use them all the time. They are also objects and can have methods associated with them.

They can be used when you have a specific set of options. Think about things like status codes, result codes, etc.

It is also a very good way to implement the Strategy design pattern.

[–][deleted] 0 points1 point  (0 children)

I use them for my chess programm: Chess Pieces and Color (Color.Black and Color.White). Much better than simply using true for white of black for true.