Skip to content

Remove the NL.27 suggestion for use of .h as the filename extension for headers #2265

Closed
@eyalroz

Description

@eyalroz

(For context, see below)

Regarding: "NL.27: Use a .cpp suffix for code files and .h for interface files (https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rl-file-suffix)

The core guidelines should not suggest using .h as the file extension for header files, by default in new C++ projects. This is because:

  1. Using .h clashes with the .h file extension for C language headers.
  2. The rationale for using .h is that, supposedly, C++ headers are shared between C++ and C; but - this is in fact uncommon, and arguably very rare for new C++ projects.
  3. The use of .h, while popular, is neither a universal nor a near-universal convention.
  4. (I claim that) We have an interest in strengthening the distinction between C and C++, against the continuing custom in media coverage and policy recommendations to talk about "C/C++" as though the languages should be treated similarly.

Possible courses of action:

  1. Remove the recommendation from NL.27, i.e. make it "Use a .cpp suffix for source code files" (or even remove NL.27 entirely, not making any recommendation about file extensions)
  2. Limit the suggestion in NL.27 to projects which do not involve cross-language C-and-C++ work, and recommend .hpp for that case.
  3. Split the recommendation in NL.27 between the case of shared C-and-C++ headers, where .h would be suggested, and C++-only headers or C++-only projects, in which .hpp is suggested. In this case, the title would be "use .hpp or .h for header files", and the body would explain when each is recommended.

... and recall that when we don't suggest anything, the general rule for the NL section still applies: Consistency with existing stylistic/naming choices in a project is important and should be maintained.

See also the discussion of this matter on StackOverflow, where a much larger group of users/developers have opined on this matter over the years, and the vast majority believes .hpp should be used.


Context:

This suggestion used to be a rule, SF.1. Issue #1965, suggesting a change, was opened and discussed in 2022. Most discussants believed that .hpp is a better choice for header file name extensions (and implicitly, that it's what should be suggested). A single discussant supported the recommendation of .h because of the cross-language use of header files, and under the premise of "the vast majority of [C++] header files out there [being] compatible with C."

An "editor's call" was made to move the recommendation to the NL section, as header file naming is a "if you have no better convention already" kind of thing. But no call was made regarding the actual validity or benefit in recommending the use of .h extensions. @cubbimew said that "there was no consensus to choose hpp over other header naming conventions." - which is technically true, but the majority was against .h and in favor of .hpp, with no case being made for .h in non-cross-language scenarios.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions