Official Naming Convention for Enums

19,754 views
Skip to first unread message

Austin Cummings

unread,
Feb 3, 2015, 12:43:19 PM2/3/15
to mi...@dartlang.org
Hi all,

Does anyone know what the official naming convention for enum values is? It doesn't seem to be in the style guide.

Thanks,
Austin Cummings

Bob Nystrom

unread,
Feb 3, 2015, 12:54:04 PM2/3/15
to General Dart Discussion
If you squint a bit, the style guide does cover this:

DO name types using UpperCamelCase.

PREFER using lowerCamelCase for constant names.


Enums are types, so they should be named using UpperCamelCase like classes. The enum values are constants, so they should be named using lowerCamelCase like constants, or ALL_CAPS if your code uses that legacy naming style.

But we should be more explicit. Filed a bug: https://github.com/dart-lang/www.dartlang.org/issues/1237

TL;DR:

enum BeeGees {
  barryGibb,
  mauriceGibb,
  robinGibb
}

Cheers!

- bob


--
For other discussions, see https://groups.google.com/a/dartlang.org/
 
For HOWTO questions, visit http://stackoverflow.com/tags/dart
 
To file a bug report or feature request, go to http://www.dartbug.com/new

To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.

Austin Cummings

unread,
Feb 3, 2015, 1:15:37 PM2/3/15
to General Dart Discussion
Awesome, thanks Bob!

Jos Hirth

unread,
Feb 3, 2015, 1:23:10 PM2/3/15
to mi...@dartlang.org
So, ALL_CAPS is supposed to be used for... nothing?

Style guide says: "We changed the rule because sometimes constants migrate from final to const."

I don't really see the cost here. We got refactor/rename for this.

Bob Nystrom

unread,
Feb 3, 2015, 1:48:23 PM2/3/15
to General Dart Discussion
On Tue, Feb 3, 2015 at 10:23 AM, Jos Hirth <google...@kaioa.com> wrote:
So, ALL_CAPS is supposed to be used for... nothing?

Right. Some legacy APIs have it, but if I could wave a magic wand and go back in time there would be no code using ALL_CAPS.

Style guide says: "We changed the rule because sometimes constants migrate from final to const."

I don't really see the cost here. We got refactor/rename for this.

If your IDE can refactor the entire world's Dart code, I would love to get my hands on it. :)

A lot of discussion went into changing the constant naming rules. What it basically boiled down to was:
  1. ALL_CAPS looked ugly to some users and in particular in some cases.

  2. It wasn't entirely clear what it actually represented. Some wanted it for actual const values in Dart. Others thought it should be used for other immutable values.
What it had going for it was "some people liked it" and compatibility with the DOM API which uses ALL_CAPS for constants.

At the same time, it had real negative value. Depending on how you defined "constant", some changes to an object could necessitate changing the name of the variable referring to it. A name change is a breaking API change.

The simplest solution was to just not have a separate naming convention at all.

Cheers!

- bob


Reply all
Reply to author
Forward
0 new messages