-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consistent capitalisation of enum values/constants #7270
Comments
Enum members are semantically like constants, so I don't follow why they should have different naming conventions. Choosing one style or the other is mostly a question of preference and heritage/sentimentality (others might call it religion). Languages in the Java family typically use UPPERCASE for enum members, whereas C# and related languages use PascalCase. Of the more modern languages, Rust and Go advocate PascalCase, Kotlin accepts both. (These mentions are only referring to the stdlib naming conventions not what the respective language supports) Crystal, too has previously accepted both, but PascalCase is more dominant. UPPERCASE is mostly but not exclusively used when creating an enum to represent grouped C constants. While being mostly equivalent, there are a few practical differences:
A common argument against UPPERCASE is that it is screaming and abrasive. While I don't agree to that, I understand that other people might perceive it as such (I can relate to that, as my typografic aesthetics are infuriated by PascalCasing all-cap acronyms). But in the end, I don't think it matters that much. Between autocasted enum values and predicate methods, the actual member names are rarely used outside the enum definition. And in that capacity UPPER_SNAKE_CASE has the practical advantage of similarity to the lower_snake_case names. Hence, I prefer an UPPERCASE naming convention for enum members. |
It seems that switching everything to |
@RX14 Loosing detail in conversion is pretty objective argument. I agree with that too. |
Also remember that the autogenerated flag enum members are None and All. PascalCase is what I expect enums to use. |
I was a fan of @straight-shoota's argument for going all caps - especially in regard to the automatic generation of predicate methods - however having |
But then also know that I would personally remove those generated constants. I regret that decision and we are still in time to remove them. |
@asterite what makes you regret it? |
@asterite you don't want to remove the generated constants because of their case though. |
When you have a flags enum members, for example |
Better example of flags enum members would be rather |
Let's continue the discussion about autogenerated enum members in #7285 |
This has been brought up again in #8984, but this issue is older and has already more comments on the topic. I'd point out that there are several ideas about changing UPDATE: It seems I already declared #7285 settled, so we're just waiting for |
@straight-shoota Since PascalCase was voted unanimously, can we go forward with that? What would be the consequences of, say, going forward with PascalCase? Would any of the other issues benefit if we go with SNAKE_CASE? Is it just that SNAKE_CASE is similar to the question method that's generated for enums? |
I don't know. But that's the thing. We should solve the important questions first. I wouldn't want to push a decision now on a topic that really doesn't matter much whether it goes one way or the other and has worked so far with diversive case styles. |
The approved style for enum values is CamelCase, as per crystal-lang/crystal#7270 However this is not stated in the style guide, although the approved style for constants is.
I think the decision of the case should be related to the short-band one, to have consistency. enum E
FirstElement
SECOND_ELEMENT
end
def m(e : E)
end
# All already valid
m :FirstElement
m :SECOND_ELEMENT
m :second_element For now the status-quo is Do we prefer to see |
enum Protocol
IPv4
IPv6
end with this code enum Protocol
IPV4
IPV6
end generates |
Rolling on from some discussion in #7247 it's been pointed out that as it stands there is no convention for the naming of values on an enum. Through the Crystal code base there are both examples of
SNAKE_UPPERCASE
andPascalCase
implementations.I think it would be ideal if a convention could be established here for the consistency of the codebase.
One option I see as preferable is that constants will be
SNAKE_UPPERCASE
and that enum values will bePascalCase
. One additional benefit from this would be possible (generally) to infer if a value is a constant or an enum from its capitalisation.For example,
Crypto::Blowfish::DEFAULT_ROUNDS
would be a constant, butColorize::ColorANSI::Default
would be an enum.Related, and not sure if it's possible at all, but if a convention was decided on it would be great if the Crystal formatter could automatically handle the change.
The text was updated successfully, but these errors were encountered: