Why Do Some Developers Hate Using Enums in API Design?

The article examines why enums, while type‑safe and convenient for input parameters, are often criticized in API return values due to their lack of extensibility and the risk of deserialization errors when newer enum constants appear after version upgrades.

Programmer DD
Programmer DD
Programmer DD
Why Do Some Developers Hate Using Enums in API Design?

Question

A colleague used an enum in an interface definition and was criticized for being hard to extend. Why is this practice disliked?

Author's Explanation

Version upgrades can cause enum classes on both sides to differ, leading to exceptions during interface parsing and class deserialization.

Enums have clear benefits: they are type‑safe, straightforward, can be compared with equality operators, and are usable in switch statements.

However, their main drawback is the inability to extend them.

Parameters are safe because they are decided locally; you only send values that exist in your own enum, ensuring forward compatibility. Return values are problematic because the remote side may introduce new enum constants that your local code does not recognize, causing serialization exceptions.

Example: a local WeatherEnum defines SUNNY, RAINY, CLOUDY. A method guess(WeatherEnum) can accept these values. If the remote service later returns SNOWY, which is not defined locally, deserialization fails.

Brian's Answer

Enums enumerate all known values. As a library provider, you decide which values are allowed, making enums a safe guarantee for inputs.

When used as return values, the provider decides the set, and you may receive unknown values after an upgrade, leading to a high chance of exceptions if you haven't read every detail of the specification.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaBackend Developmentserializationenumapi-design
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.