Bird
0
0
LLDsystem_design~3 mins

Why Enum usage (VehicleType, SpotType) in LLD? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if a tiny typo could crash your whole parking system? Enums save you from that nightmare!

The Scenario

Imagine managing a parking lot system where you have to track different types of vehicles and parking spots manually using plain text or numbers.

Every time you add a new vehicle or spot type, you must remember the exact string or number and ensure consistency everywhere.

The Problem

This manual approach is slow and error-prone because typos or mismatched values can cause bugs.

For example, writing "car" sometimes and "Car" other times leads to confusion and wrong behavior.

It also makes the code hard to read and maintain, especially as the system grows.

The Solution

Using enums for VehicleType and SpotType gives a fixed set of named values that the system understands clearly.

This prevents mistakes, makes the code easier to read, and helps the system handle only valid types.

It acts like a checklist that the program follows strictly, avoiding guesswork.

Before vs After
Before
vehicleType = "car"
if vehicleType == "car":
    parkCar()
After
enum VehicleType { CAR, BIKE, TRUCK }
vehicleType = VehicleType.CAR
if vehicleType == VehicleType.CAR:
    parkCar()
What It Enables

It enables building reliable and clear systems that handle specific categories without confusion or errors.

Real Life Example

In a parking management app, enums ensure that only valid vehicle types like CAR, BIKE, or TRUCK are accepted, preventing wrong inputs and simplifying spot allocation.

Key Takeaways

Manual string or number codes cause errors and confusion.

Enums provide a fixed, clear set of valid types.

This leads to safer, easier-to-maintain system design.