Design Tip: String vs. Enum
Sometimes developers need to choose proper data type for their application business entities properties. Many times it is obvious decision - for example choosing String for Name, GUID for Id, etc.
However sometimes it is more complicated to make a right choice. For example - Country and Gender. What data type to choose - String or Enum (where Enum should contain a list of countries, genders, etc) ? How to store it in the database ? What data type to use ? Either decision would be controversial.
The correct answer to these questions should be - it depends (on the app).
Despite this - the general advice should be - keep it simple. Let's take for example Country. Country should be a String and not Enum. Because String is the simplest and therefore more flexible, scalable, interoperable. Modern systems have to provide interoperability. If your choice is Enum you have to be sure that the 3rd party which uses your API is able to interpret it correctly - meaning the other party should maintain the exactly same Enum as you are maintaining. And in our world countries list is changed often. In addition some systems interpret countries from economical perspective and not political. Countries lists may vary.
The same is correct about Gender. For example Facebook has "it's complicated" as Gender (or is it Marital Status ? whatever :)).
Database type should be dumb string as well as soon as we choose a string for a class data type. The conclusion is - choose strings data types for business entities properties as a general guideline.
Wednesday, September 3, 2008 2:17 PM