Skip to content

Enum Design Issue: Avoid External Maps — Use Enum Values Instead #15

Description

@ajaxspace

Problem:
enum ButtonSize is defined separately from its associated values (e.g., font sizes), using an external Map<ButtonSize, double>. This results in:

enum ButtonSize {
  small,
  medium,
  large,
}

static const Map<ButtonSize, double> buttonFontSize = {
  ButtonSize.small: 13.6,
  ButtonSize.medium: 15.2,
  ButtonSize.large: 16.0,
};

Issues:

  • Indirect and non-extensible design
  • Harder to track and maintain associated data
  • No autocomplete or inline access to values

Recommendation:
Use enhanced enums with associated values, which are more idiomatic and maintainable in Dart:

✅ Suggested fix:

enum ButtonSize {
  small(13.6),
  medium(15.2),
  large(16.0);

  final double fontSize;
  const ButtonSize(this.fontSize);
}

Usage:

Text(
  'Label',
  style: TextStyle(fontSize: buttonSize.fontSize),
);

Impact:

  • Stronger type safety
  • Self-contained and scalable enum definitions
  • Cleaner code and better IDE support

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions