Coding style: Making the `e` prefix for enum variants not mandatory?

119 views
Skip to first unread message

Emilio Cobos Álvarez

unread,
Jun 25, 2018, 4:42:32 PM6/25/18
to dev-pl...@lists.mozilla.org
Our coding style states that we should use an `e` prefix for enum
variants, that is:

enum class Foo { eBar, eBaz };

We're not really consistent about it: looking at layout/, we mostly use
CamelCase, though we do have some prefixed enums. Looking at other
modules, enum classes almost never use it either. DOM bindings also
don't use that prefix.

I think that with enum classes the usefulness of the prefix is less
justified. Plus removing them would allow us to match the Rust coding
style as well, which is nice IMO.

Would anybody object to making the prefix non-mandatory, removing that
line from the coding style doc? Maybe only making it non-mandatory for
enum classes?

Thanks,

-- Emilio

Emilio Cobos Álvarez

unread,
Jun 25, 2018, 5:09:11 PM6/25/18
to dev-pl...@lists.mozilla.org
And Kris pointed out that we already had another huge thread on this:


https://groups.google.com/d/msg/mozilla.dev.platform/WAuySoTfq_w/-DggRotpBQAJ

Looks like there wasn't agreement on that one... But oh well, don't want
to repeat a lot of that discussion.

I think the argument for consistency with the other systems language we
have in-tree, the fact that it's not predominant (at least for enum
classes) even though it is in the coding style, and that there wasn't
agreement in the previous thread are good reasons for not enforcing it,
but...

-- Emilio
> _______________________________________________
> dev-platform mailing list
> dev-pl...@lists.mozilla.org
> https://lists.mozilla.org/listinfo/dev-platform

Peter Saint-Andre

unread,
Jun 25, 2018, 5:18:05 PM6/25/18
to Emilio Cobos Álvarez, dev-pl...@lists.mozilla.org
And perhaps good reason for removing it from the style guide? ;-)

Jeff Gilbert

unread,
Jun 25, 2018, 8:46:27 PM6/25/18
to Peter Saint-Andre, Emilio Cobos Álvarez, dev-pl...@lists.mozilla.org
If we can't agree, it shouldn't be in the style guide.

Emilio Cobos Álvarez

unread,
Jun 28, 2018, 11:28:03 AM6/28/18
to Jeff Gilbert, Peter Saint-Andre, dev-pl...@lists.mozilla.org
I asked kats@ (which added the list item regarding enums) and he was
fine removing it from the coding style, so given that and the rest of
the thread I've edited the page accordingly.

Cheers,

-- Emilio

Mike Hommey

unread,
Jun 28, 2018, 6:16:26 PM6/28/18
to Emilio Cobos Álvarez, Jeff Gilbert, dev-pl...@lists.mozilla.org, Peter Saint-Andre
On Thu, Jun 28, 2018 at 05:27:23PM +0200, Emilio Cobos Álvarez wrote:
> I asked kats@ (which added the list item regarding enums) and he was fine
> removing it from the coding style, so given that and the rest of the thread
> I've edited the page accordingly.

Not that I'd oppose, but I'll note that neither kats, nor participants to
this thread so far are peers of the "C++/Rust usage, tools, and style"
module.

Mike

Emilio Cobos Álvarez

unread,
Jun 28, 2018, 7:36:21 PM6/28/18
to Mike Hommey, Jeff Gilbert, dev-pl...@lists.mozilla.org, Peter Saint-Andre
Oh, I didn't realize that those peers were the only ones to be able to
update the style guide, sorry. I guess it makes sense.

I can revert the change if needed and try to get sign-off from some of
those.

Apologies again, I just followed the procedure that was followed in the
previous thread to add the rule. Let me know if you want that change
reverted and I'll happily do so.

-- Emilio

>
> Mike
>

Nathan Froyd

unread,
Jun 29, 2018, 10:30:56 AM6/29/18
to Emilio Cobos Álvarez, Mike Hommey, Jeff Gilbert, dev-pl...@lists.mozilla.org, Peter Saint-Andre
On Thu, Jun 28, 2018 at 7:35 PM, Emilio Cobos Álvarez <emi...@crisal.io> wrote:
> Oh, I didn't realize that those peers were the only ones to be able to
> update the style guide, sorry. I guess it makes sense.
>
> I can revert the change if needed and try to get sign-off from some of
> those.
>
> Apologies again, I just followed the procedure that was followed in the
> previous thread to add the rule. Let me know if you want that change
> reverted and I'll happily do so.

My sense, after grepping through the code for "enum [A-Z].* \{" (that
is, ignoring `enum class`, since those are effectively prefixed by the
language) and eyeballing the results is that ALL_CAPS and e-prefixed
enums are more common than CamelCase. Commands:

# rough approximations only!
# enums defined on a single line
git grep -E 'enum [A-Z].* \{[^}]+\}' -- '*.[ch]*' |grep -v -e
'gfx/skia' -e 'media/webrtc' |less
# multi-line enums; -A 1 so we can see the style of the first enum
git grep -A 1 -E 'enum [A-Z].* \{' -- '*.[ch]*' |grep -v $(for p in
$(cat tools/rewriting/ThirdPartyPaths.txt); do echo -e \\x2de $p;
done) |less

This exercise was not an exhaustive analysis by any means. (If
somebody was going to be exhaustive about this, I think it'd be
interesting to try and consider whether the enums are at global scope
or at class scope, since using class scope enums outside the class
naturally requires qualifying them with the class name.)

Based on this, I think it's reasonable to say that e-prefixing or
ALL_CAPS for (non-`enum class`) enums is the preferred style. For
`enum class`, we (of course) do everything, but I think CamelCase is
*slightly* more common. Given the language-required qualification for
`enum class` and a more Rust-alike syntax, I would feel comfortable
with saying CamelCase `enum class` is the way to go.

Objections? (Almost certainly? ;)

-Nathan

Boris Zbarsky

unread,
Jun 29, 2018, 10:58:53 AM6/29/18
to
On 6/29/18 10:30 AM, Nathan Froyd wrote:
> Given the language-required qualification for
> `enum class` and a more Rust-alike syntax, I would feel comfortable
> with saying CamelCase `enum class` is the way to go.

For what it's worth, I agree. The point of the "e" prefix is to
highlight that you have an enumeration and add some poor-man's
namespacing for a potentially large number of common-looking names, and
the language-required qualification already handles both of those.

-Boris

Jean-Yves Avenard

unread,
Jun 29, 2018, 12:10:44 PM6/29/18
to dev-pl...@lists.mozilla.org
+1..

Would certainly be disappointed with an ALL_CAPS (being myself a user of
kCamelCase)

smaug

unread,
Jun 29, 2018, 2:36:48 PM6/29/18
to Boris Zbarsky
That works if we're consistently naming static variables and such using s-prefix etc, so that
class Foo1
{
static int sBar;
};
Foo1::sBar

is clearly different to
enum class Foo2
{
Bar
};

Foo2::Bar


(personally I'd still prefer using e-prefix always with enums, class or not. Foo1::sBar vs Foo2::eBar is easier to distinguish than Foo1::sBar vs
Foo2::Bar)

-Olli



> -Boris

Jeff Gilbert

unread,
Jun 29, 2018, 3:57:52 PM6/29/18
to smaug, dev-platform
I don't believe e-prefix adds sufficient value.
Mutable statics *must* always be s-prefixed. If this is not the case I
will gladly bring the codebase into compliance.

Ehsan Akhgari

unread,
Jul 6, 2018, 2:21:57 PM7/6/18
to smaug, Emilio Cobos Álvarez, dev-pl...@lists.mozilla.org
Looking at other popular style guidelines, the Google C++ style requires
the 'k' prefix on both enum and enum class <
https://google.github.io/styleguide/cppguide.html#Enumerator_Names>
presumably because it doesn't require any special prefix for static
members. But given that ours does, it seems that in the case of enum
classes, not using a prefix should be acceptable.

Based on Nathan's analysis (thanks, Nathan!) and the previous thread, it
seems like Emilio's edit should be reverted and a note should be added
about the usage of CamelCase for enum classes. Emilio, do you mind doing
that, please?

Thanks,
--
Ehsan

Emilio Cobos Álvarez

unread,
Jul 7, 2018, 3:51:39 AM7/7/18
to dev-pl...@lists.mozilla.org


On 7/6/18 8:21 PM, Ehsan Akhgari wrote:
> On Fri, Jun 29, 2018 at 2:36 PM, smaug <sm...@welho.com> wrote:
>
> Looking at other popular style guidelines, the Google C++ style requires
> the 'k' prefix on both enum and enum class <
> https://google.github.io/styleguide/cppguide.html#Enumerator_Names>
> presumably because it doesn't require any special prefix for static
> members. But given that ours does, it seems that in the case of enum
> classes, not using a prefix should be acceptable.
>
> Based on Nathan's analysis (thanks, Nathan!) and the previous thread, it
> seems like Emilio's edit should be reverted and a note should be added
> about the usage of CamelCase for enum classes. Emilio, do you mind doing
> that, please?

Sure, just did:


https://developer.mozilla.org/en-US/docs/Mozilla/Developer_guide/Coding_Style$compare?locale=en-US&to=1394287&from=1392024

Feel free to clarify or change as you think it's more useful.

Thanks Ehsan!

-- Emilio
Reply all
Reply to author
Forward
0 new messages