Skip to content
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

doc: update enumerator naming in developer notes #21930

Merged
merged 1 commit into from
Sep 6, 2021

Conversation

jonatack
Copy link
Member

@jonatack jonatack commented May 12, 2021

Per our current doc, the general rule is we follow the C++ Core Guidelines, which for enumerator naming stipulate:

https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Renum-caps

Don’t use ALL_CAPS for enumerators
Reason: Avoid clashes with macros.

but our examples (and often, codebase) are contradictory to it (and perhaps to common usage), which can be confusing when a contributor needs to choose a style to use. This patch:

  • updates the enumerator examples to snake_case per CPP Core Guidelines
  • clarifies for contributors that in this project enumerators may be snake_case, PascalCase or ALL_CAPS, and to use what seems appropriate.

@fanquake fanquake added the Docs label May 12, 2021
@maflcko
Copy link
Member

maflcko commented May 12, 2021

Concept ACK. I guess it should be clear from the context that those are compile-time constants. Also, IIRC it is not possible to name a enum value ERROR, because this conflicts with some windows macro in our cross build. Error might work.

@hebasto
Copy link
Member

hebasto commented May 12, 2021

Concept ACK.

@ryanofsky
Copy link
Contributor

Mildly disagree with this change. I think it's nice to be able to declare variables that change at runtime as variable, and declare variables that don't change as CONSTANT, and just use the choice to try to make code more understandable. It's possible a constant could conflict with a preprocessor macro, though, and this could be bad. I don't check for preprocessor macros under my bed every night, and I'm not too scared of them, but maybe I should be! So ignore this feedback if this seems like the clear way to go.

Copy link
Contributor

@promag promag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not, considering we use enum class.

@DrahtBot
Copy link
Contributor

DrahtBot commented May 13, 2021

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

No conflicts as of last run.

@laanwj
Copy link
Member

laanwj commented May 13, 2021

ACK if this better matches what is in actual use in the project.

"clashes with macros" wouldn't be so much of an issue of C++ namespaced the things, but it doesn't, and as @ryanofsky already jokes they can just pop up from the kernel headers or libc headers out of nothing.

@jonatack
Copy link
Member Author

jonatack commented May 13, 2021

Checked the books/literature (Stroustrup, Meyers, Lippman, Moo, etc.) and they mostly use lowercase_underscore (snake_case) with a couple of the C++98 era ones using PascalCase (UpperCamelCase).

The Google style guide (https://google.github.io/styleguide/cppguide.html#Enumerator_Names) is a bit different but "use kEnumName not ENUM_NAME" for new code since 2009 for the same reasons.

Currently in this project it seems to be mostly ALL_CAPS, or in newer code lowercase (in src/bench/nanobench.h) or PascalCase (git grep -A5 "enum class ConnectionDirection").

I don't have a strong opinion (on my own I'd probably use lowercase_underscore (snake_case) with scoped enums as AFAICT it's widely used nowadays) but I think the important thing is to say which naming to use and to clarify if our choice is different from the CPP Core Guidelines so people aren't confused on which style to use.

@ryanofsky
Copy link
Contributor

I don't have a strong opinion (on my own I'd probably use lowercase_underscore (snake_case) with scoped enums as AFAICT it's widely used nowadays) but I think the important thing is to say which naming to use and to clarify if our choice is different from the CPP Core Guidelines so people aren't confused on which style to use.

Again just an opinion to throw in the mix, but I like CONSTANT<<3/2 to emphasize unchangedness of expressions when enums are used as collections of constants in bitwise or arithmetic code, and I like Confused NotConfused NotConfusedAndHungry ConfusedButSleepy for readability of english words in logical / event-handling code that more closely resembles natural language. I would not want to codify this and think it's fine to have guidelines that say there is no rule, and please do what seems appropriate.

@jonatack
Copy link
Member Author

guidelines that say there is no rule, and please do what seems appropriate.

Yes, that could be a helpful clarification too.

@maflcko
Copy link
Member

maflcko commented May 14, 2021

If there is no rule, we shouldn't be mentioning it

@jonatack jonatack force-pushed the developer-notes-enum-naming branch from bc2f1b0 to 2cf94a8 Compare May 14, 2021 09:30
@jonatack
Copy link
Member Author

Per the current doc, the general rule is we follow the CPP Core Guidelines, but our examples are contradictory to it (and perhaps to common usage). I found this confusing.

Re-pushed an update that combines @ryanofsky's suggestion, open to feedback.

@jonatack jonatack force-pushed the developer-notes-enum-naming branch from 2cf94a8 to 2d813c4 Compare May 14, 2021 09:33
Copy link
Contributor

@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 2d813c4. This looks good to me and it seems nice to move closer to cpp core guidelines and link to them even if not adopting them wholly.

Feel free to ignore my review suggestions below. I think they help but they're just my opinions and none are important.

doc/developer-notes.md Outdated Show resolved Hide resolved
doc/developer-notes.md Outdated Show resolved Hide resolved
doc/developer-notes.md Outdated Show resolved Hide resolved
doc/developer-notes.md Outdated Show resolved Hide resolved
@jonatack jonatack force-pushed the developer-notes-enum-naming branch from 2d813c4 to e721833 Compare July 13, 2021 19:19
- Update the enumerator examples to snake_case per CPP Core Guidelines
  https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Renum-caps

- Clarify that in this project enumerators may be snake_case, PascalCase, or
  ALL_CAPS, and to use what is seems appropriate.
@jonatack jonatack force-pushed the developer-notes-enum-naming branch from e721833 to 77f37f5 Compare July 13, 2021 19:23
@jonatack
Copy link
Member Author

Updated per @ryanofsky feedback (thanks!) and rebased for #18568.

Copy link
Contributor

@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 77f37f5. I think this is good because it modernizes the example and clarifies current conventions.

@practicalswift
Copy link
Contributor

cr ACK 77f37f5

Agree with @ryanofsky regarding:

[…] it seems nice to move closer to cpp core guidelines and link to them even if not adopting them wholly.

Copy link
Contributor

@promag promag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 77f37f5.

@maflcko maflcko merged commit 92aad53 into bitcoin:master Sep 6, 2021
@jonatack jonatack deleted the developer-notes-enum-naming branch September 6, 2021 12:37
sidhujag pushed a commit to syscoin/syscoin that referenced this pull request Sep 7, 2021
77f37f5 doc: update enum naming in developer notes (Jon Atack)

Pull request description:

  Per our current doc, the general rule is we follow the C++ Core Guidelines, which for enumerator naming stipulate:

  https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Renum-caps
  ```
  Don’t use ALL_CAPS for enumerators
  Reason: Avoid clashes with macros.
  ```

  but our examples (and often, codebase) are contradictory to it (and perhaps to common usage), which can be confusing when a contributor needs to choose a style to use. This patch:

  - updates the enumerator examples to snake_case per CPP Core Guidelines
  - clarifies for contributors that in this project enumerators may be snake_case, PascalCase or ALL_CAPS, and to use what seems appropriate.

ACKs for top commit:
  practicalswift:
    cr ACK 77f37f5
  ryanofsky:
    ACK 77f37f5. I think this is good because it modernizes the example and clarifies current conventions.
  promag:
    ACK 77f37f5.

Tree-SHA512: 7facc607fe5e1abab0f635864340143f13c2e4bb074eb17eac7d829dcd0cf244c5c617fc49d35e8774e8af1fa1205eeebe0cca81f538a2a61f6a7ba200878bc6
PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Apr 3, 2022
77f37f5 doc: update enum naming in developer notes (Jon Atack)

Pull request description:

  Per our current doc, the general rule is we follow the C++ Core Guidelines, which for enumerator naming stipulate:

  https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Renum-caps
  ```
  Don’t use ALL_CAPS for enumerators
  Reason: Avoid clashes with macros.
  ```

  but our examples (and often, codebase) are contradictory to it (and perhaps to common usage), which can be confusing when a contributor needs to choose a style to use. This patch:

  - updates the enumerator examples to snake_case per CPP Core Guidelines
  - clarifies for contributors that in this project enumerators may be snake_case, PascalCase or ALL_CAPS, and to use what seems appropriate.

ACKs for top commit:
  practicalswift:
    cr ACK 77f37f5
  ryanofsky:
    ACK 77f37f5. I think this is good because it modernizes the example and clarifies current conventions.
  promag:
    ACK 77f37f5.

Tree-SHA512: 7facc607fe5e1abab0f635864340143f13c2e4bb074eb17eac7d829dcd0cf244c5c617fc49d35e8774e8af1fa1205eeebe0cca81f538a2a61f6a7ba200878bc6
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Sep 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants