Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using enum vs Boolean?

Tags:

This may initially seem generic, but in fact, I am actually making the decision on which I need to use.

What I am currently working on involves Employment Applications, those in which will need to be marked at some point Active or Inactive. When an application is submitted, it will default to Active. For certain reasons, it might later be set to Inactive. It can only be one of these and never null(In case this changes anything).

I am using this with Java + Hibernate + PostgresSQL, in case this also makes any difference. My first instinct is to use Boolean as my solution so that it truly acts as a flag, but I have coworkers who have suggested using enums or ints as more of a status rather then a flag.

I have solved problems such as this using all of the above solutions, and they all seem somewhat transparent to eachother.

Is there one way that is better for this situation?

like image 542
TheJediCowboy Avatar asked Dec 02 '10 17:12

TheJediCowboy


People also ask

Why is enum not good?

It's hard to voice what your actual needs are to a potential partner for fear that they may write you off. Transparency requires over communication. If your communication skills are already not that great, ENM is going to be more of a headache than an opportunity to be your most authentic self.

Why are enums better?

Advantages of enum: enum improves type safety at compile-time checking to avoid errors at run-time. enum can be easily used in switch. enum can be traversed. enum can have fields, constructors and methods.

Are enums are type-safe True or false?

The enums are type-safe means that an enum has its own namespace, we can't assign any other value other than specified in enum constants. Typesafe enums are introduced in Java 1.5 Version. Additionally, an enum is a reference type, which means that it behaves more like a class or an interface.

Are enums better 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 . If your set of parameters is open and unkown at compile time, use strings.


1 Answers

Even ignoring the possibility of adding more status types in the future (which is certainly one good argument for an enum), I think an enum is absolutely the right way to go. You are not modelling a boolean condition, you are modelling the status of an application. Think about it: the application's status is not true or false, it's active or inactive! A status enum will represent this in the most natural way.

You also get a lot of built in advantages from using an enum, such as having a text description of each status tied directly to it, so you don't have to do things like

String text = application.isActive() ? "Active" : "Inactive"; 

You can just do

String text = application.getStatus().toString(); 

Additionally, you can tie specific behavior directly to each status with abstract methods that each enum implements differently, associate specific data with each status, etc.

You can also easily allow a boolean isActive check that is based on the status... you can't easily do that the other way around if you just store a boolean.

public boolean isActive() {   return status == Status.ACTIVE; } 

And the fact that null shouldn't be a valid status is irrelevant... just ensure that any classes that store the status (say, your EmploymentApplication class or whatever) throw a NullPointerException if anyone tries to set a null status on it.

like image 119
ColinD Avatar answered Oct 11 '22 14:10

ColinD