Booleans are rare in business & industry
In my experience building custom apps for business, an apparent duality such as "Female/Male" or "On/Off" nearly always evolve or morph into multiple values.
- "Female/Male" becomes "Unknown/Female/Male"
- "On/Off" becomes "On/WarmingUp/CoolingDown/Off"
- "Incomplete/Completed" becomes "Incomplete/Processing/Completed"
Business rules are often not fully known early-on, or change over time.
Like many of my colleagues, I learned the hard way to never define such values as booleans. Changing from boolean to multiple values later is quite expensive and risky.
Java enums
Enums have other benefits over a boolean.
Java enums are flexible & powerful
The Enum
facility in Java is much more flexible, powerful, and useful than enums in most other languages. See Oracle Tutorial.
Within your enum definition you can house representations of the values for presentation to the user and for storage in databases or files. This makes a handy one-stop-shop for a programmer to read all about this enum and how it used in your app.
Here is a complete example. This one class shows what values we use in the user-interface and what values we persist.
package com.basilbourque.example;
public enum Status {
ACTIVE( "Active" , "active" ),
INACTIVE( "Inactive" , "inactive" ),
UNKNOWN( "Unknown" , "unknown" );
private String displayName, codeName;
Status ( String displayName , String codeName ) {
this.displayName = displayName;
this.codeName = codeName;
}
public String getDisplayName () { return this.displayName; } // Or even add a `Locale` argument to support localization.
public String getCodeName () { return this.codeName; }
// To find a `Status` enum object matching input retrieved from a database.
static public Status ofCodeName ( String codeName ) {
// Loop each of the enum objects to find a match.
for ( Status s : Status.values() ) {
if ( s.getCodeName().equals( codeName ) ) { return s; }
}
throw new IllegalArgumentException( "No such Status code name as: " + codeName );
}
@Override
public String toString() { return "app-status-" + this.getCodeName() ; }
}
Translating persisted values back to enum object
Notice the static method that can return a Status
object matching a persisted value retrieved from a database:
Status s = Status.ofCodeName( myResultSet.getString( "code" ) ) ;
Flexible sets/maps
In Java, enums have their own implementation of Set
and Map
that are highly optimized both in terms of very little memory used and very fast execution.
Set< Status > auditApprovedStatuses = EnumSet.of( Status.ACTIVE , Status.INACTIVE ) ;
Set< Status > auditAlertStatuses = EnumSet.of( Status.UNKNOWN ) ;
…
if( auditAlertStatuses.contains( application.getStatus() ) ) {
this.alertAuditor( application ) ;
}
Notice how easy to update these set definitions if your definition of application-status changes.
Informative toString
value
Whereas a boolean appears as true
or false
as a string, in your enum you can override toString
to generate a much more informative value such as app-status-inactive
.
Such values may be quite useful when logging, tracing, or debugging.