{"id":1622,"date":"2025-04-17T12:04:44","date_gmt":"2025-04-17T18:04:44","guid":{"rendered":"https:\/\/jaredrobinson.com\/blog\/?p=1622"},"modified":"2025-04-17T22:14:48","modified_gmt":"2025-04-18T04:14:48","slug":"enum-values-integer-or-string","status":"publish","type":"post","link":"https:\/\/jaredrobinson.com\/blog\/enum-values-integer-or-string\/","title":{"rendered":"Enum values: Integer or string?"},"content":{"rendered":"\n<p>I&#8217;ve been writing software for a few decades and historical momentum has been to define enum values as integers, probably because earlier languages supported integers, not strings. <\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<pre class=\"wp-block-code\"><code>class Color(Enum):\n  UNINITIALIZED: 0\n  RED:           1\n  GREEN:         2\n  BLUE:          3<\/code><\/pre>\n<\/blockquote>\n\n\n\n<p>So it was a surprise when a discussion among engineers went the other direction &#8212; encouraging strings as enum values &#8212; for newly introduced enums. It seems that industry momentum may have shifted this direction. Here&#8217;s a contrived example that I do not recommend:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Color(Enum):\n  UNKNOWN:  \"\"\n  RED:      \"rood\"     # Note: Dutch\n  GREEN:    \"verde\"    # Note: Spanish\n  BLUE:     \"bleu\"     # Note: French\n  LAVINDRE: \"lavndir\"  # Note: Purposefully misspelled<\/code><\/pre>\n\n\n\n<p>Benefits of using string enum values include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Clarity when viewing logs. However, it is possible to log integer enum values as strings.<\/li>\n\n\n\n<li>Clarity when debugging. However, some debuggers may already add clarity for integer-based enums.<\/li>\n\n\n\n<li>Disparate teams across the organization can more readily understand the data without having to refer to documentation. <\/li>\n\n\n\n<li>API interoperability and clarity.<\/li>\n\n\n\n<li>Ease of adding new items in the middle of the enum, whereas with a sequential integer approach, it is not possible.<\/li>\n\n\n\n<li>Default value as empty string. It has always been a pain when an engineer decided to use 0 to indicate anything other than &#8220;default&#8221;, &#8220;uninitialized&#8221; or &#8220;unknown&#8221;, because 0 is often the default value for uninitialized variables and properties. With strings, an empty string is ideal.<\/li>\n\n\n\n<li>It&#8217;s still possible to represent string-value enums as integers when persisting to a database or interoperating with other systems &#8212; although it may require additional work.<\/li>\n<\/ul>\n\n\n\n<p>Caveats and counter indications:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Legacy. Keep what works and don&#8217;t break things, especially when disparate systems depend on existing enumerated integers. Translation layers can be a solution.<\/li>\n\n\n\n<li>Lack of programming language support, although there are often alternatives including use of constants.<\/li>\n\n\n\n<li>When the enum string values are misnamed or poorly named, it can cause confusion and bugs due to misunderstandings.<\/li>\n\n\n\n<li>Who gets do decide which human language we use for the string values? Who will be consuming the values? Do they understand the language? I gave my example above with three different language values to emphasize that it&#8217;s best to stick with a single language.<\/li>\n\n\n\n<li>Misspelled values may need to remain misspelled forever to prevent breaking disparate consumers. Software engineers are human, and they make mistakes. We don&#8217;t usually misspell integer values, and when do, the software either won&#8217;t compile or won&#8217;t run. Translation layers can be a solution.<\/li>\n\n\n\n<li>Upper or lower case? Camel case or snake case? Decide on a standard.<\/li>\n\n\n\n<li>Performance critical systems and RAM\/storage constrained systems may do better with integer values.<\/li>\n\n\n\n<li>Some databases support enums &#8212; the underlying data is an integer, and the database represents it as a string, including in queries and exports &#8212; so it could be possible to get the best of both worlds &#8212; performance and clarity for humans.<\/li>\n\n\n\n<li>Isolate the churn of a ever-changing names. The integers can stay the same and the enum keys can change to reflect current needs. E.g. marketing names tend to change.<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been writing software for a few decades and historical momentum has been to define enum values as integers, probably because earlier languages supported integers, not strings. So it was a surprise when a discussion among engineers went the other direction &#8212; encouraging strings as enum values &#8212; for newly introduced enums. It seems that &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/jaredrobinson.com\/blog\/enum-values-integer-or-string\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Enum values: Integer or string?&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-1622","post","type-post","status-publish","format-standard","hentry","category-tech"],"_links":{"self":[{"href":"https:\/\/jaredrobinson.com\/blog\/wp-json\/wp\/v2\/posts\/1622","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jaredrobinson.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jaredrobinson.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jaredrobinson.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jaredrobinson.com\/blog\/wp-json\/wp\/v2\/comments?post=1622"}],"version-history":[{"count":15,"href":"https:\/\/jaredrobinson.com\/blog\/wp-json\/wp\/v2\/posts\/1622\/revisions"}],"predecessor-version":[{"id":1637,"href":"https:\/\/jaredrobinson.com\/blog\/wp-json\/wp\/v2\/posts\/1622\/revisions\/1637"}],"wp:attachment":[{"href":"https:\/\/jaredrobinson.com\/blog\/wp-json\/wp\/v2\/media?parent=1622"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jaredrobinson.com\/blog\/wp-json\/wp\/v2\/categories?post=1622"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jaredrobinson.com\/blog\/wp-json\/wp\/v2\/tags?post=1622"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}