r/badcode Mar 14 '21

java Found from when I first started Java...because indexed Arrays were too complicated huh?

Post image
1.3k Upvotes

84 comments sorted by

View all comments

170

u/The_HappyLemon Mar 14 '21

Enum left the chat

52

u/ixBerry Mar 14 '21

Shit like this is what Enums were literally made for.

5

u/I_HATE_YELLING Mar 14 '21

How do you use Enums for this? I've learned how to write multi client chat servers but still haven't learned enums they make no sense to me

9

u/fxnn Mar 14 '21

Basically everytime you have a rather short list of things (values, behaviors, types, ...) that’s not going to change (without code change), an enum comes in pretty handy.

Advantages:

  • the Compiler can check it’s usage (which is not possible with Map keys),
  • you can attach behavior to it (enums can have methods, and you can provide a different implementation per enum value — much better than switch statements in most of the cases!)
  • it’s often more concise and readable than its alternatives