Handle unions of string constants

Use enums, unions of singletons, or unions of constants. Enum seems to be a better option out of those three.

Let's say we have a string variable named department, which can only be Finance, Engineering, or HR.

Bad Code

type Employee record {|
   string name;
   string department;
|};
 
Employee e = {name: "John Doe", department: "HR"};
  • No idea what are the possible department types
  • Users can specify any string value as the department. It is syntactically correct but incorrect from the scenario point of view.

Good Code

Option 1: Usage of enums
enum Department {
   Finance,
   Engineering,
   HR
}
 
type Employee record {|
   string name;
   Department department;
|};
 
Employee e = {name: "John Doe", department: HR};

Note: Ballerina doesn't allow numeric enums.

Option 2: Unions of singletons
type Department "Finance"|"Engineering"|"HR";
 
type Employee record {|
   string name;
   Department department;
|};
 
Employee e = {name: "John Doe", department: "HR"};
Option 3: Unions of constants
const Eng = "Engineering";
const Fin = "Finance";
const HR = "HR";
 
type Department Eng|Fin|HR;
 
type Employee record {|
   string name;
   Department department;
|};
 
Employee e = {name: "John Doe", department: HR};

Conclusion

  • In all three approaches, the User can't specify anything other than specified values which is good.
  • Option 1: Enums is a better choice if we consider the readability and simplicity of the code.

See Also