Skip to content

doc: Clarify developer notes about constant naming - #18568

Closed
ryanofsky wants to merge 1 commit into
bitcoin:masterfrom
ryanofsky:pr/capconst
Closed

ryanofsky wants to merge 1 commit into
bitcoin:masterfrom
ryanofsky:pr/capconst

Conversation

@ryanofsky

@ryanofsky ryanofsky commented Apr 8, 2020 •

Copy link
Copy Markdown
Contributor

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:

extern const int SYMBOL;

or:

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.

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
@maflcko

maflcko commented Apr 8, 2020 •

Copy link
Copy Markdown
Member

Why is const int ... = 1; not a compile time constant?

@ryanofsky

ryanofsky commented Apr 8, 2020 •

Copy link
Copy Markdown
Contributor Author

Why is const int ... = 1; not a compile time constant?

Can you give a more specific example? 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". In this example, SYMBOL is just const in the sense that it can't be modified after it is initialized and its value does not change at runtime.

@jonatack jonatack left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Concept ACK

Comment thread doc/developer-notes.md Resolved
@DrahtBot DrahtBot added the Docs label Apr 8, 2020

@maflcko maflcko left a comment

Copy link
Copy Markdown
Member

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".

Comment thread doc/developer-notes.md Resolved

@ryanofsky ryanofsky left a comment

Copy link
Copy Markdown
Contributor Author

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.

Comment thread doc/developer-notes.md Resolved
Comment thread doc/developer-notes.md Outdated
- 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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
- 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?

@laanwj laanwj Apr 22, 2020 •

Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor Author

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

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.

Copy link
Copy Markdown
Member

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?

Copy link
Copy Markdown
Contributor Author

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."

@jarolrod jarolrod left a comment

Copy link
Copy Markdown
Contributor

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.

Comment thread doc/developer-notes.md Outdated
- 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.

Copy link
Copy Markdown
Contributor

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

@DrahtBot

DrahtBot commented May 13, 2021 •

Copy link
Copy Markdown
Contributor

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

Conflicts

No conflicts as of last run.

@ryanofsky ryanofsky left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Comment thread doc/developer-notes.md Outdated
- 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.

Copy link
Copy Markdown
Contributor Author

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."

@maflcko

maflcko commented Jun 29, 2021

Copy link
Copy Markdown
Member

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.

@jarolrod jarolrod left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ACK 05f9770 🥃

@sipa

sipa commented Jun 29, 2021

Copy link
Copy Markdown
Member

ACK

@practicalswift

Copy link
Copy Markdown
Contributor

ACK 05f9770

fanquake added a commit that referenced this pull request Jun 30, 2021
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
@fanquake fanquake closed this Jun 30, 2021
@fanquake

Copy link
Copy Markdown
Member

Don't know why this wasn't autoclosed, but this has been merged.

PastaPastaPasta pushed a commit to PastaPastaPasta/dash that referenced this pull request Dec 28, 2021
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
gwillen pushed a commit to ElementsProject/elements that referenced this pull request Jun 1, 2022
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Aug 18, 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