-
Notifications
You must be signed in to change notification settings - Fork 36.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
doc: Clarify developer notes about constant naming #18568
Conversation
I'm pretty sure developer notes were intended to say constants should be upper case and variables should be lower case, but right now they are ambiguous about whether to write: ```c++ // foo.h extern const int SYMBOL; // foo.cpp const int SYMBOL = 1; ``` or: ```c++ // foo.h extern const int g_symbol; // foo.cpp const int g_symbol = 1; ``` First convention above is better than the second convention because it tells you without having to look anything up that the value of `SYMBOL` will never change at runtime. Also I've never seen any c++ project anywhere using the second convention
Why is |
Can you give a more specific example? In the example from the PR description, the compiler does not know what the value of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Concept ACK
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the example from the PR description, the compiler does not know what the value of SYMBOL is when you include foo.h and use SYMBOL in compiled code, so it is misleading or at least a stretch to call SYMBOL a "compile time constant"
While to compiler doesn't know its value, it does know that it is a constant, right? Maybe I've been using "compile time constant" interchangeably with "link time constant".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated 05f9770 -> 58ff41e (pr/capconst.1
-> pr/capconst.2
, compare) to not imply local constants need to be capitalized.
I'm assuming that in some cases it makes sense to capitalize local constants, and in other cases it doesn't, and it isn't important to have a rule about it.
doc/developer-notes.md
Outdated
@@ -83,7 +83,7 @@ code. | |||
separate words (snake_case). | |||
- Class member variables have a `m_` prefix. | |||
- Global variables have a `g_` prefix. | |||
- Compile-time constant names are all uppercase, and use `_` to separate words. | |||
- Global constant names are all uppercase, and use `_` to separate words. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Global constant names are all uppercase, and use `_` to separate words. | |
- Global or compile-time constant names are all uppercase, and use `_` to separate words. |
Since there could be non-global-scope compile-time constants, which should be UPPER_CASE?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder the same. Class-scope constants should be the same, right? Even function-level constants would be better off in caps, _
separated imo. Do we have any notable exceptions here?
Edit: I think the exception is function arguments and things that aren't really constants but marked const
to enforce that they shouldn't be changed. So the "compile-time" makes a lot of sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I second the extra specification of compile-time
constants and the exclusion of run-time
constants
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I second the extra specification of
compile-time
constants and the exclusion ofrun-time
constants
This PR is trying to drop the phrase "compile-time" because it doesn't have an obvious and clear meaning (see discussion in opening comments above), and because being "compile-time" seems less relevant to how a variable should be used or named compared to other attributes like mutability, scope, and lifetime. Also, I don't think in this document it is worth trying to be very technical or make detailed rules about how everything needs to be named. For local variables, it seems fine to just let developers have freedom to emphasize constness with caps where it seems better. Global variables are different because global variables are declared and accessed in different places, often far away, and if you are using one you almost always need to be aware if it has a constant value or if the value can change. Whether the value is initially evaluated at preprocessing time, compile time, link time, or perhaps early init can be interesting to know, but probably less relevant than knowing whether the value changes. A capitalized name is a good way to indicate that the value will not change.
Re: class constants, if it would help I could replace "global constants" with "global constants and other constants with static duration including namespaced and class-scope constants" here. But I don't think developer guidelines need to be so procedural or legalistic. I wouldn't expect someone just reading the updated text to be confused by it, and I wouldn't expect someone familiar with C++ to think of ::MY_CONSTANT
, my_namespace::MY_CONSTANT
, and MyClass::MY_CONSTANT
much differently by default. Also, it seems fine to me if someone wants to use normal variable names for non-global constants. I think the benefit of using ALL_CAPS
in a symbol name is to communicate the fact that the symbol's value will not change. If a developer chooses to not indicate or emphasize constness in some symbol name, it doesn't seem like a problem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe just remove "global" or is there a reason to imply function-scope constants should be lower snake case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe just remove "global" or is there a reason to imply function-scope constants should be lower snake case?
Removing "global" is really my preference, and "global" wasn't part of the original push which just dropped the "compile-time" phrase: pr/capconst.1
. I added "global" later in pr/capconst.2
to try to address a concern about the rule sounding too broad (actually from you).
I think I'll switch back to the original push for now since it's pretty clear "global" doesn't help in the way I thought it might. Clarifying things is hard I guess. My goal is really just to drop the phrase "compile-time."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Concept ACK, pending the re-inclusion of compile-time
constants in the changed line.
doc/developer-notes.md
Outdated
@@ -83,7 +83,7 @@ code. | |||
separate words (snake_case). | |||
- Class member variables have a `m_` prefix. | |||
- Global variables have a `g_` prefix. | |||
- Compile-time constant names are all uppercase, and use `_` to separate words. | |||
- Global constant names are all uppercase, and use `_` to separate words. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I second the extra specification of compile-time
constants and the exclusion of run-time
constants
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ConflictsNo conflicts as of last run. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reverted 58ff41e -> 05f9770 (pr/capconst.2
-> pr/capconst.1
, compare)
doc/developer-notes.md
Outdated
@@ -83,7 +83,7 @@ code. | |||
separate words (snake_case). | |||
- Class member variables have a `m_` prefix. | |||
- Global variables have a `g_` prefix. | |||
- Compile-time constant names are all uppercase, and use `_` to separate words. | |||
- Global constant names are all uppercase, and use `_` to separate words. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe just remove "global" or is there a reason to imply function-scope constants should be lower snake case?
Removing "global" is really my preference, and "global" wasn't part of the original push which just dropped the "compile-time" phrase: pr/capconst.1
. I added "global" later in pr/capconst.2
to try to address a concern about the rule sounding too broad (actually from you).
I think I'll switch back to the original push for now since it's pretty clear "global" doesn't help in the way I thought it might. Clarifying things is hard I guess. My goal is really just to drop the phrase "compile-time."
cr ACK 05f9770 Current wording seems ok and attempts to make it more clear will only raise more concerns. Also, there doesn't seem too much disagreement in practice about this. The rules hare are more descriptive than prescriptive, so the exact wording shouldn't matter too much when most are doing the right thing anyway. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK 05f9770 🥃
ACK |
ACK 05f9770 |
05f9770 doc: Clarify developer notes about constant naming (Russell Yanofsky) Pull request description: I'm pretty sure developer notes were intended to say constants should be upper case and variables should be lower case, but right now they are ambiguous about whether to write: ```c++ extern const int SYMBOL; ``` or: ```c++ extern const int g_symbol; ``` First convention above is better than the second convention because it tells you without having to look anything up that the value of `SYMBOL` won't change at runtime. Also I haven't seen other c++ projects using the second convention. ACKs for top commit: MarcoFalke: cr ACK 05f9770 practicalswift: ACK 05f9770 jarolrod: ACK 05f9770 🥃 Tree-SHA512: 766d0e25d9db818d45df4ad6386987014f2053584cbced4b755ceef8bda6b7e2cfeb34eb8516423bd03b140faaf577614d5e3be2799f7eed0eb439187ab85323
Don't know why this wasn't autoclosed, but this has been merged. |
05f9770 doc: Clarify developer notes about constant naming (Russell Yanofsky) Pull request description: I'm pretty sure developer notes were intended to say constants should be upper case and variables should be lower case, but right now they are ambiguous about whether to write: ```c++ extern const int SYMBOL; ``` or: ```c++ extern const int g_symbol; ``` First convention above is better than the second convention because it tells you without having to look anything up that the value of `SYMBOL` won't change at runtime. Also I haven't seen other c++ projects using the second convention. ACKs for top commit: MarcoFalke: cr ACK 05f9770 practicalswift: ACK 05f9770 jarolrod: ACK 05f9770 🥃 Tree-SHA512: 766d0e25d9db818d45df4ad6386987014f2053584cbced4b755ceef8bda6b7e2cfeb34eb8516423bd03b140faaf577614d5e3be2799f7eed0eb439187ab85323
05f9770 doc: Clarify developer notes about constant naming (Russell Yanofsky) Pull request description: I'm pretty sure developer notes were intended to say constants should be upper case and variables should be lower case, but right now they are ambiguous about whether to write: ```c++ extern const int SYMBOL; ``` or: ```c++ extern const int g_symbol; ``` First convention above is better than the second convention because it tells you without having to look anything up that the value of `SYMBOL` won't change at runtime. Also I haven't seen other c++ projects using the second convention. ACKs for top commit: MarcoFalke: cr ACK 05f9770 practicalswift: ACK 05f9770 jarolrod: ACK 05f9770 🥃 Tree-SHA512: 766d0e25d9db818d45df4ad6386987014f2053584cbced4b755ceef8bda6b7e2cfeb34eb8516423bd03b140faaf577614d5e3be2799f7eed0eb439187ab85323
05f9770 doc: Clarify developer notes about constant naming (Russell Yanofsky) Pull request description: I'm pretty sure developer notes were intended to say constants should be upper case and variables should be lower case, but right now they are ambiguous about whether to write: ```c++ extern const int SYMBOL; ``` or: ```c++ extern const int g_symbol; ``` First convention above is better than the second convention because it tells you without having to look anything up that the value of `SYMBOL` won't change at runtime. Also I haven't seen other c++ projects using the second convention. ACKs for top commit: MarcoFalke: cr ACK 05f9770 practicalswift: ACK 05f9770 jarolrod: ACK 05f9770 🥃 Tree-SHA512: 766d0e25d9db818d45df4ad6386987014f2053584cbced4b755ceef8bda6b7e2cfeb34eb8516423bd03b140faaf577614d5e3be2799f7eed0eb439187ab85323
05f9770 doc: Clarify developer notes about constant naming (Russell Yanofsky) Pull request description: I'm pretty sure developer notes were intended to say constants should be upper case and variables should be lower case, but right now they are ambiguous about whether to write: ```c++ extern const int SYMBOL; ``` or: ```c++ extern const int g_symbol; ``` First convention above is better than the second convention because it tells you without having to look anything up that the value of `SYMBOL` won't change at runtime. Also I haven't seen other c++ projects using the second convention. ACKs for top commit: MarcoFalke: cr ACK 05f9770 practicalswift: ACK 05f9770 jarolrod: ACK 05f9770 🥃 Tree-SHA512: 766d0e25d9db818d45df4ad6386987014f2053584cbced4b755ceef8bda6b7e2cfeb34eb8516423bd03b140faaf577614d5e3be2799f7eed0eb439187ab85323
05f9770 doc: Clarify developer notes about constant naming (Russell Yanofsky) Pull request description: I'm pretty sure developer notes were intended to say constants should be upper case and variables should be lower case, but right now they are ambiguous about whether to write: ```c++ extern const int SYMBOL; ``` or: ```c++ extern const int g_symbol; ``` First convention above is better than the second convention because it tells you without having to look anything up that the value of `SYMBOL` won't change at runtime. Also I haven't seen other c++ projects using the second convention. ACKs for top commit: MarcoFalke: cr ACK 05f9770 practicalswift: ACK 05f9770 jarolrod: ACK 05f9770 🥃 Tree-SHA512: 766d0e25d9db818d45df4ad6386987014f2053584cbced4b755ceef8bda6b7e2cfeb34eb8516423bd03b140faaf577614d5e3be2799f7eed0eb439187ab85323
I'm pretty sure developer notes were intended to say constants should be upper case and variables should be lower case, but right now they are ambiguous about whether to write:
or:
First convention above is better than the second convention because it tells you without having to look anything up that the value of
SYMBOL
won't change at runtime. Also I haven't seen other c++ projects using the second convention.