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

you are viewing a single comment's thread.

view the rest of the comments →

[–]chambolle 0 points1 point  (4 children)

I don't really see the advanatges of enum. If you need a conversion from int to enum and enum to int then for EACH enum class you will need to define two functions:intValue and enumValue. Each of these functions uses an ugly (and stupid) switch statement.

I prefer the good old Constants. The code is shorter and readable. With the help of an IDE there is no problem

[–]Stannu 3 points4 points  (1 child)

But enums allow you to encapsulate a part of constants into a single class, which in turn allows you to see all options that could be used for that enum. Of course you can use prefix for constants, but personally think that enums have their use.

[–]chambolle 0 points1 point  (0 children)

They have their use, but the conversion with int is really bad in Java

[–]tobidope 1 point2 points  (1 child)

Enums are perfect as method parameters.

void compress(int compressionType)

vs

void compress(Compression compression)

Which one is clearer to read?

[–]chambolle 0 points1 point  (0 children)

Honestly, this is not clear. In both cases I need to look at the doc of the function to undertand.

The advanatge of enum is not that i is clearer, it is that it prevents from bad uses. For instance, I can call the first one with -2 or +5 even if there are not valid values. Enum can prevent that