What Is an Enum?

An enum (short for enumeration) is a type that has a fixed list of named values. Instead of storing a state as 0, 1, "CREATED" or "DONE", you give every allowed value a clear name, and the type only accepts those names.

Many things in a real system have a small, known set of options:

  • Booking status in a cab app: requested, driver assigned, in progress, completed, cancelled

  • Ride type: Mini, Sedan, SUV

  • User role: rider, driver, admin

  • Payment mode: cash, card, UPI

Rule of thumb: if a value can only be one of a fixed set of options, use an enum.

In Low Level Design these values show up everywhere: the status of an order, the type of a vehicle, the role of a user. Modelling them as enums makes the design easier to read and much harder to misuse.

Why Use Enums Instead of Strings or Integers?

Without enums, a cab booking app might track the state of a ride like this:

This has three problems:

  • Numbers do not explain themselves. Nobody reading status == 3 knows what 3 means.

  • Strings are easy to mistype. "SEDN", "Sedan" and "SEDAN" are all valid strings, so the bug only shows up at runtime.

  • Renaming or finding every use of a raw value later is slow and risky.

With an enum, every possible value is a named constant, and the compiler (or your IDE) knows the full list. That gives you:

  • Readable code: BookingStatus::COMPLETED says exactly what it means.

  • Compile-time checks: a typo in a constant name is a build error, not a production bug.

  • No invalid assignment: you cannot put a random number or string into a BookingStatus variable.

  • IDE auto-complete: type BookingStatus and the editor lists every valid value.

  • Easy refactoring: renaming a constant updates every use, and "find usages" shows where each state is handled.

In a class diagram you can now write "Booking has a status of type BookingStatus", which says far more than "status: int".

Defining and Using an Enum

Here is the booking status as an enum in each language:

Using it is just as simple. The variable can only hold one of the five values:

Expected output

Looking for a driver...

How Each Language Handles Enums

The idea is the same everywhere, but each language has its own details worth knowing:

  • C++ has two kinds. The old unscoped enum puts its names in the surrounding scope and converts to int silently. The modern enum class keeps names inside the type and blocks implicit conversion, so prefer it.

  • Java enums are full classes. They always need the type name, and come with name(), ordinal(), valueOf() and values().

  • Python uses the Enum class from the enum module. Each member has a name and a value, and you can loop over the enum.

  • Go has no enum keyword. A named type plus a block of iota constants gives the same effect, but Go will still accept an untyped number like 7, so validate values that come from outside.

Enums That Carry Data

Sometimes every value needs a few facts attached to it. Each ride type in our cab app has a display name, a base fare and a rate per kilometre. Keeping that data next to the enum means there is no separate lookup table to forget when a new ride type is added.

Expected output

Mini: 8 km costs Rs 120

Sedan: 8 km costs Rs 172

SUV: 8 km costs Rs 224

Adding a new ride type, say AUTO, is one new line with its data. The fare code does not change.

Switching on Enums and Converting Them to Text

You often need to show a status to a user or write it to a log or database. A switch over the enum is the usual way to turn each value into text.

One piece of advice: list every value in the switch and do not hide missing cases behind a catch-all default. That way the compiler can warn you when someone adds a new status. g++ and clang warn with -Wall, and a Java switch expression will not compile until every constant is handled. Go and Python do not check this, so a linter or a final error branch has to do the job there.

Expected output

Status: IN_PROGRESS

Rider sees: Ride in progress

Enums for Roles and Categories

Enums are not only for lifecycle states. They work just as well for roles, types and categories. Here the cab app has three kinds of users, and the role decides what each one may do:

Expected output

Accept rides: yes

Issue refunds: no

The full list of roles is visible in one place, and a new role cannot appear by accident.

Practical Example: A Cab Booking with Two Enums

Now let us put two enums to work together. A Booking has a BookingStatus that moves through a lifecycle, and a RideType that decides the price. The rule for the status is simple:

  • A booking starts as REQUESTED, then moves to DRIVER_ASSIGNED, IN_PROGRESS and finally COMPLETED, always in that order.

  • A rider may cancel only before the ride starts, that is from REQUESTED or DRIVER_ASSIGNED.

  • A cancelled booking costs nothing. Otherwise the fare comes from the ride type and the distance.

The state diagram below shows every allowed move. Anything not drawn here is rejected by the class.

assignDriver()startRide()completeRide()cancel()cancel()REQUESTEDDRIVER_ASSIGNEDIN_PROGRESSCOMPLETEDCANCELLED

And this is how the class uses the two enums:

statusrideTypeBooking-int id-RideType rideType-BookingStatus status-int distanceKm+assignDriver() : bool+startRide() : bool+completeRide() : bool+cancel() : bool+fare() : int«enumeration»BookingStatusREQUESTEDDRIVER_ASSIGNEDIN_PROGRESSCOMPLETEDCANCELLED«enumeration»RideTypeMINISEDANSUV+fareFor(km) : int

Each method checks the current status before it changes anything, so the object can never end up in a state the diagram does not allow. Let us run two bookings through it:

Expected output

Cancel during the ride? rejected

Booking 101: COMPLETED, fare Rs 228

Booking 102: CANCELLED, fare Rs 0

Sending receipt for booking 101

Why This Design Works

  • The status can only change in the allowed order. Calling startRide() on a booking that has no driver simply returns false.

  • Business rules read like English: "cancel only if the status is REQUESTED or DRIVER_ASSIGNED".

  • Pricing lives with the ride type. Booking never needs to know that an SUV costs Rs 18 per km.

  • It is easy to extend. A new status such as DRIVER_ARRIVED is one new constant plus the transitions that use it, and the compiler points at every switch that must handle it.

Where to Declare Enums in LLD

A couple of simple guidelines keep the design tidy:

  • If an enum belongs to one class only, keep it next to that class, in the same file or as a nested type. BookingStatus can live with Booking.

  • If several classes share it, give it its own file. UserRole is used by User, AuthService and AuditLogger, so it deserves a shared place.

In an interview you can describe the design in one line each: "Booking has a status of type BookingStatus" and "User has a role of type UserRole". Interviewers like seeing enums named this clearly.

Common Mistakes with Enums

  • Still using raw integers. For example, using int status = 1; instead of an enum type. It is harder to read and easy to get wrong.

  • Using strings for state. Strings are easy to mistype and slower to compare. An enum gives you compile-time checks and a clear list of values.

  • Hiding missing cases behind default. A catch-all default in a switch silences the compiler when a new value is added. Handle each value explicitly.

  • Mixing unrelated meanings in one enum. Putting ride states and payment results in one enum (REQUESTED, PAID, FAILED, COMPLETED) makes both harder to reason about. Use two enums, BookingStatus and PaymentStatus.

  • Saving the ordinal or position. Storing 2 in the database instead of "IN_PROGRESS" breaks the moment someone reorders the constants. Store the name.

Summary

An enum is a fixed set of named values. In Low Level Design it is the natural way to model states, roles and categories, replacing unclear numbers and fragile strings with names the compiler can check.

You learned:

  • What enums are and why they beat raw integers and strings.

  • How C++, Java, Python and Go define enums, and the difference between enum and enum class in C++.

  • How an enum can carry data, like the base fare and rate of each ride type.

  • How a class uses an enum field to allow only valid state changes.

  • How the calling code checks the final status by comparing against enum constants.

  • Where to declare enums, and the common mistakes to avoid.