I'm trying to check if a users account type matches one of several Strings
.
There's debate in the office as to whether this should be represented as an enum
with each entry containing a different string, or as a Set
of Strings
. Whilst the Set
may be more efficient, an enum may be stylistically superior as it is clearer it is being used for logic flow.
What are the advantages of these two approaches?
The benefits of using enumerations include: Reduces errors caused by transposing or mistyping numbers. Makes it easy to change values in the future. Makes code easier to read, which means it is less likely that errors will creep into it.
You can declare a function argument, return type, class member or local variable to be a particular Enum type and the compiler will enforce type safety; Enums are basically classes. They can implement interfaces, have behaviour and so on.
The only difference is that enum constants are public , static and final (unchangeable - cannot be overridden). An enum cannot be used to create objects, and it cannot extend other classes (but it can implement interfaces).
I would consider Enums to be a better approach than Strings. They are type safe and comparing them is faster than comparing Strings. Show activity on this post. If your set of parameters is limited and known at compile time, use enum .
Indeed, a Set<String>
is more efficient in terms of performance when searching. However, I wouldn't expect that you have thousands of account types, but several, so you won't actually feel the difference when searching. There's one problem with this approach, though - you will be able to add any String
to the Set
, which is brittle.
My personal prefer would be to use an enum
, especially if you don't expect that more account types will be introduced. And if you have a Set<AccountType>
you'll be restricted with the values you can add (i.e. you will be able to add only account types, but not anything, like the approach with a Set<String>
). The problem with this approach is the Open/Closed Principle - consider you have a switch
statement over a AccountType
variable with all the corresponding case
s. Then, if you introduce a new AccountType
constant, you must change the switch
statement (with adding a new case
), which breaks the "Open/Closed principle". In this case the neatest design would be to have an abstract class
/interface
, called AccountType
which has all the specific account types as sub-classes.
So, there are several approaches you can follow, but before picking one, you should try answer yourselves the question of "How are we going to use it?"
An enum would be better since account types (typically) do not change dynamically. Furthermore, using an enum makes the types more precise - e.g. there's no way to mix up "Hello, World!"
with an account type.
Enums are great because you get compile time checking. Invalid values simply won't compile so it 'fails fast'.
A collection of strings is great when you want to add another option without compiling/releasing a new version of your application. If, for instance, the valid options were configured in a database table.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With