Microsoft Dev Blogs

API design note: Beware of adding an “Other” enum value

thumbnail

API Design Note: Beware of Adding an "Other" Enum Value

  • Issue: Adding an "Other" enum value in APIs to cover any future options may lead to confusion and unexpected behavior.
  • Recommendation: Instead of adding an "Other" enum value, document that the enumeration is open-ended and treat any unrecognized values as "Other" by the application.

Example Scenario

  • Scenario: Adding a new flavor to the Widgets enum.
  • Problem: Confusion arises when determining the appropriate response for the new flavor, e.g., "Mint", in existing code.
  • Solution: Avoid using an "Other" enum value and handle unrecognized values as "Other" based on the application's logic.

Future Expansion and Compatibility

  • Expansion: Introduction of new flavors, such as "Mint", in the Widgets enum.
  • Compatibility: New code categorizes "Mint" as a specific flavor, while existing code treats it as an app-defined "Other" flavor.

Summary

Ensure API design clarity by avoiding the use of an "Other" enum value and instead relying on open-ended enumerations with clear documentation on handling unrecognized values. This approach ensures compatibility between old and new code when introducing future additions to the enum.