6

With most mentions of JAVA enums on the internet, it is everywhere mentioned that enums should be all uppercase (ex: ACTIVE).

Like here: Coding Conventions - Naming Enums

But when it comes to Rails, in all there examples and docs they use lowercase enum value (ex: 'active'), like here: http://edgeapi.rubyonrails.org/classes/ActiveRecord/Enum.html

which makes sense since rails also provides instance methods by the name of these enums (eg: obj.active?). Is this the only reason why enums in Rails are used as lowercase, or is there more to it? Also we differ from the convention when we use enums as lowercase, should this be the case? or shall we use uppercase enums in Rails as well?

So for example I have a status enum in my model, which can either be active, draft or inactive as per convention, should it be:

enum status: {active: 1, draft: 2, inactive: 3}

or:

enum status: {ACTIVE: 1, DRAFT: 2, INACTIVE: 3}

Which one and why?

Community
  • 1
  • 1
Sambhav Sharma
  • 5,277
  • 8
  • 50
  • 84
  • 1
    Can you link to a couple of examples? – Sergio Tulentsev Oct 20 '16 at 10:57
  • Though I have not used uppercase yet, I have created capitalized enums before like `Active` and method created like `obj.Active?` . – Sajan Oct 20 '16 at 10:58
  • There, I just added examples. Also, yes obj.Active? would work, so will obj.ACTIVE? and obj.active? Just wanted to know what is the convention. And is it just as such for RAILS, and if it is different from that of JAVA, why doesn't it follow general coding convention. – Sambhav Sharma Oct 20 '16 at 11:03
  • @SambhavSharma: what is this "general coding convention" that you speak of? :) – Sergio Tulentsev Oct 20 '16 at 11:05
  • General, as in I've read mostly languages have convention of keeping enums as all uppercase (java, c, c++). Though, I guess JavaScript keeps it first letter uppercase and rest downcase. So to say, ACTIVE -> java, c, c++ etc. Active -> JavaScript, C# etc. – Sambhav Sharma Oct 20 '16 at 11:12
  • @SambhavSharma Soooo, this means there's no one "general" convention then? :) – Sergio Tulentsev Oct 20 '16 at 11:14
  • Right, I guess so, I just thought about Java and C.. but more research led me to the fact that there actually isn't a convention between languages maybe. – Sambhav Sharma Oct 20 '16 at 11:28
  • PS. if you set the mapping manually then use a zero based index instead of 1 due to the principle of least surprise or the principle of least WTF!?! – max Oct 20 '16 at 14:36

2 Answers2

10

In Rails enums should be snake_case.

Why? Because enums are used to construct method names. And method names in Ruby should be snake_case according to the community convention. Using ALLCAPS or CamelCase can lead to bugs and confusion as Ruby treats such identifiers as constants.

ActiveRecord::Enum is not comparable to a language level enumeration construct such as in Java.

Declare an enum attribute where the values map to integers in the database, but can be queried by name.
http://api.rubyonrails.org/classes/ActiveRecord/Enum.html

The keys mappings in an ActiveRecord::Enum are not constants. Rather it's just a list which is used with Ruby style metaprogramming that adds methods to make bitmask columns easier (and more fun) to work with.

In your example its actually comparable to:

enum status: [ :ACTIVE, :DRAFT, :INACTIVE ] 

An enum type is a special data type that enables for a variable to be a set of predefined constants. The variable must be equal to one of the values that have been predefined for it. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.
https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html

Enums in Java are basically glorified switch statements where the class constants denote the possible values.

max
  • 76,662
  • 13
  • 84
  • 137
6

which makes sense since rails also provides instance methods by the name of these enums (eg: obj.active?). Is this the only reason why enums in RAILS are used as lowercase

I think it's the main reason, yes. It's highly unidiomatic in ruby to have methods like obj.Active? and obj.ACTIVE?. One doesn't need any more reason. :)

Sergio Tulentsev
  • 210,238
  • 40
  • 347
  • 343