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

Implement the Google variable naming style more faithfully. #4410

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

fruffy
Copy link
Collaborator

@fruffy fruffy commented Feb 11, 2024

Fixes #4404.

The closest approximation of the Google style I could find is defined here: https://github.com/googleapis/google-cloud-cpp/blob/main/.clang-tidy#L104

We deviate quite a bit. For example, we use camelCase in most places instead of CamelCase and snake_case for variables. I do not have a strong opinion on the style except that we should decide for one and stick to it. These days clang-tidy is advanced enough to actually enforce it, which is nice.

Pinging some people on this PR to get agreement. This will be helpful in reviews going forward.

.clang-tidy Outdated
- { key: readability-identifier-naming.ClassMemberCase, value: camelCase }
- { key: readability-identifier-naming.ClassMemberSuffix, value: _ }
- { key: readability-identifier-naming.PrivateMemberSuffix, value: _ }
- { key: readability-identifier-naming.ProtectedMemberSuffix, value: _ }
Copy link
Contributor

Choose a reason for hiding this comment

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

We tend to use snake_case for private/internal members (including for members that are "logically" private but exposed publicly for logging/debugging). Not sure if clang-tidy provides a way to note that.

Copy link
Collaborator Author

@fruffy fruffy Feb 12, 2024

Choose a reason for hiding this comment

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

Yes, specific case for private members is possible with https://clang.llvm.org/extra/clang-tidy/checks/readability/identifier-naming.html#cmdoption-arg-PrivateMemberCase

But marking a public member logically private is not.

Copy link
Collaborator Author

@fruffy fruffy Feb 12, 2024

Choose a reason for hiding this comment

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

At least in the compiler front end and libraries, the common style seems to be camelCase for everything. We can easily change that with an automated clang-tidy run. But that will assuredly break downstream back ends and repositories.

@fruffy fruffy force-pushed the fruffy/clang-tidy-google-style branch from 4970f9f to 40a1c4d Compare February 12, 2024 15:59
@fruffy fruffy added the infrastructure Topics related to code style and build and test infrastructure. label Feb 20, 2024
@asl
Copy link
Contributor

asl commented Apr 5, 2024

Do we want to add abseil-* checks here?

@fruffy fruffy force-pushed the fruffy/clang-tidy-google-style branch from 40a1c4d to 78a370e Compare April 5, 2024 19:57
@fruffy
Copy link
Collaborator Author

fruffy commented Apr 5, 2024

Do we want to add abseil-* checks here?

Sure.

@fruffy
Copy link
Collaborator Author

fruffy commented May 1, 2024

@ChrisDodd

With type you mean a typedef?

typedef or nested struct

Unfortunately, there is no way to distinguish between a private and public typedef for now. But we could add the _t suffix.

However, the current configuration here uses the prefix style. So _camelCase for private member variables instead of lower_case. This might be more consistent than snake case for only private members?

This is what it would look like:

Classes/Structs/NameSpaces/TypeDefs: PascalCase
Functions/Methods: camelCase
Public/local variables: camelCase
Private variables: _camelCase

For reference, other styles:
Google C++ style is:

Classes/Structs/NameSpaces/TypeDefs: PascalCase
Functions/Methods: PascalCase
Public/local variables: snake_case
Private variables: _snake_case?

C# style is:

Classes/Structs/NameSpaces/TypeDefs: PascalCase
Functions/Methods: PascalCase
Public/local variables: PascalCase
Private variables: _camelCase?

Java Style

Classes/Structs/NameSpaces/TypeDefs: PascalCase
Functions/Methods: camelCase
Public/local variables: camelCase
Private variables: camelCase

@fruffy fruffy force-pushed the fruffy/clang-tidy-google-style branch from 78a370e to a3cd02b Compare May 1, 2024 23:54
@vlstill
Copy link
Contributor

vlstill commented May 2, 2024

I don't have many strong opinions about naming style. Personally I find _t suffix annoying and prefer uppercase for types which matches with the proposed style. But mainly I am for naming consistency.

Do you prefer to enforce this (by a CI check)?

Did you try how large changes are needed to make the codebase conform?

@fruffy
Copy link
Collaborator Author

fruffy commented May 2, 2024

Do you prefer to enforce this (by a CI check)?

We are still missing Clang-tidy support for this (#4254).

Did you try how large changes are needed to make the codebase conform?

There are quite a few changes needed across the codebase, but I'd say we can just gradually fix them. Just having the clang-tidy standards will encourage developers to use the right naming scheme.

@vlstill
Copy link
Contributor

vlstill commented May 2, 2024

Do you prefer to enforce this (by a CI check)?

We are still missing Clang-tidy support for this (#4254).

Would the clang-tidy still run very slowly if you disabled all the checks except for the formatting once? Maybe then it would be manageable.

Of course, if we wanted enforce it we would also have to re-format whole codebase at once.

@fruffy
Copy link
Collaborator Author

fruffy commented May 2, 2024

Would the clang-tidy still run very slowly if you disabled all the checks except for the formatting once?

Yes, I have not found a way yet. But even having it as part of CMakelists is already useful for cleanup, e.g., #4326

Of course, if we wanted enforce it we would also have to re-format whole codebase at once.

I do not think this is necessary, we can do this gradually. Unlike clang-format these changes are more sensitive.

@fruffy fruffy force-pushed the fruffy/clang-tidy-google-style branch from a3cd02b to b8d1f1d Compare May 14, 2024 14:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
infrastructure Topics related to code style and build and test infrastructure.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Define an informal variable naming style in the .clang-tidy file.
4 participants