在哪里记录 C 或 C++ 中的函数?

发布于 2024-09-16 07:15:40 字数 1431 浏览 9 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

睫毛溺水了 2024-09-23 07:15:41

我对此反复思考,最终我决定使用头文件中的文档。对于 C/C++ 中的绝大多数 API,您可以访问原始头文件,因此可以访问 [1] 中的所有注释。将评论放在这里可以最大限度地提高开发人员看到它们的机会。

不过,我避免了头文件和源文件之间的重复注释(感觉就像浪费)。使用 Vim 时这确实很烦人,但大多数 IDE 都会获取头文件注释并将它们放入智能感知或参数帮助等内容中。

[1] 此规则的例外情况包括从某些 COM 库生成的头文件。

I've gone back and forth on this and eventually I settled on documentation in header files. For the vast majority of APIs in C/C++ you have access to the original header file and hence all of the comments that lie within [1]. Putting comments here maximizes the chance developers will see them.

I avoid duplication of comments between header and source files though (it just feels like a waste). It's really annoying when using Vim but most IDEs will pick up the header file comments and put them into things like intellisense or parameter help.

[1] Exceptions to this rule include generated header files from certain COM libraries.

暮年慕年 2024-09-23 07:15:41

它通常取决于设置的编码标准。许多人喜欢将文档放在 .h 文件中,而将实现留在 .c 文件中。许多具有代码补全功能的 IDE 也会更容易地了解这一点,而不是 .c 文件中的文档。

但我认为将文档放入 .h 文件的主要目的是编写将与另一个程序共享的库或程序集。想象一下,您正在编写一个 .dll(或 .so),其中包含您将要分发的组件。其他程序员会包含您的 .h,但他们通常不会(也不需要)其背后的实现文件。在这种情况下,.h 文件中的文档非常宝贵。

当您编写在同一程序中使用的类时,也可以这样说。如果您与其他程序员一起工作,那么大多数情况下,这些程序员只是查看头文件以了解如何与您的代码交互,而不是如何实现代码。如何实现它并不是使用该组件的人员或代码所关心的。因此,标头中的文档将再次帮助该人或那些人弄清楚如何使用该代码。

It will often depend on what is set as the coding standard. Many people prefer to put the documentation in the .h file and leave the implementation in the .c file. Many IDE's with code completion will also pick up more easily on this rather than the documentation in the .c file.

But I think the major point in putting the documentation in the .h file deals with writing a library or assembly that will be shared with another program. Imagine that you're writing a .dll (or .so) that contains a component that you will be distributing. Other programmers will include your .h, but they often won't have (nor need) the implementation file behind it. In this case, documentation in the .h file is invaluable.

The same can be said when you're writing a class for use in the same program. If you're working with other programmers, most often those programmers are just looking at the header file for how to interact with your code rather than how the code is implemented. How it is implemented is not the concern of the person or code that will be using the component. So once again, documentation in the header will help that person or those people figure out how to use that code.

浅黛梨妆こ 2024-09-23 07:15:41

考虑到人们可以在仅拥有标头和实现的编译版本的情况下使用这些函数。确保使用函数所需的任何内容都记录在标题中。实现细节可以记录在源代码中。

Consider that it's possible for people to use these functions while only having the headers and a compiled version of the implementation. Make sure that anything necessary for using your functions is documented in the header. Implementation details can be documented in the source.

娇妻 2024-09-23 07:15:41

标头与实现文件中的注释应反映两者使用方式的差异。

如果您要创建显然属于标头的接口文档(例如,使用 Doxygen 提取,与 JavaDocs 的一般顺序相同)。即使您不打算提取注释来生成单独的文档,同样的一般思想也适用 - 解释接口/如何使用代码的注释主要或专门属于标头。

实施中的评论通常应该与实施相关。与常见的做法相反,大多数人应该解释为什么做出特定决定,而不是试图解释事物如何运作。当您做出有意义的决定时尤其如此,但它们可能并不明显(例如,注意到您没有使用快速排序,因为您需要稳定的排序)。

The comments in the header vs. the implementation file should reflect the difference in how the two are used.

If you're going to create interface documentation (e.g., to be extracted with Doxygen, on the same general order as JavaDocs) that clearly belongs in the header. Even if you're not going to extract the comments to produce separate documentation, the same general idea applies -- comments that explain the interface/how to use the code, belong primarily or exclusively in the header.

Comments in the implementation should generally relate to the implementation. Contrary to frequent practice, rather than attempting to explain how things work, most should explain why particular decisions were made. This is especially true when you make decisions that make sense, but it might not be obvious that they do (e.g., noting that you did not use a Quicksort, because you need a stable sort).

新人笑 2024-09-23 07:15:41

当你想一想时,这真的很简单。

API 文档绝对必须放在头文件中。它是定义外部接口的头文件,因此这就是 API 文档所在的位置。

一般来说,实施细节应该对 API 用户隐藏。这包括实现的文档(除非它可能影响使用,例如时间复杂度等)。因此,实施文档应该放在实施文件中。

切勿在多个地方重复文档。它将无法维护,并且几乎一旦有人必须更改它就会失去同步。

It's simple really when you think about it.

The API docs absolutely must go in the header file. It's the header file that defines the external interface, so that's where the API docs go.

As a rule, implementation details should be hidden from API users. This includes documentation of implementation (except where it might affect the use e.g. time complexity etc). Thus implementation documentation should go in the implementation file.

Never ever duplicate documentation in multiple places. It will be unmaintainable and will be out of sync almost as soon as somebody has to change it.

折戟 2024-09-23 07:15:41

我编写了一个简单的脚本,它将没有函数声明的模板头文件和带有注释函数的源代码文件作为输入。该脚本从源代码文件中提取函数定义之前的注释,并将其和关联的函数声明写入输出头文件中。这确保了 1)只有一个地方需要编写函数注释; 2) 头文件中的文档和源代码文件始终保持同步。对函数实现的注释被放入函数体中并且不被提取。

I wrote a simple script that takes as input a template header-file with no function declarations and a source-code file with commented functions. The script extracts the commentary before a function definition from the source code file and writes it and the associated function declaration into an output header-file. This ensures that 1) there's only one place where function commentary needs to be written; and 2) the documentation in the header-file and the source code file always remain in sync. Commentary on the implementation of a function is put into the body of the function and is not extracted.

站稳脚跟 2024-09-23 07:15:41

一定要将文档保存在一个地方,以避免维护噩梦。就你个人而言,可能会非常挑剔地保持两份副本同步,但下一个人不会。

使用诸如 doxygen 之类的东西来创建文档的“漂亮”版本。

Definitely keep the docs in one place, to avoid the maintenance nightmare. You, personally, might be fastidious enough to keep two copies in sync, but the next person wont.

Use something like doxygen to create a "pretty" version of the docs.

只是偏爱你 2024-09-23 07:15:40
  • 将使用功能的人需要了解的信息放在标题中。

  • 将函数维护者需要了解的信息放在源代码中。

  • Put the information that people using the functions need to know in the header.

  • Put the information that maintainers of the functions need to know in the source code.

好久不见√ 2024-09-23 07:15:40

我喜欢遵循 ​​Google C++ 样式指南。

其中说道:

函数声明

  • 每个函数声明都应该
    前面有评论
    它描述了函数的内容
    以及如何使用它。这些
    评论应该是描述性的
    (“打开文件”)而不是
    命令式(“打开文件”);这
    注释描述了该功能,它
    不告诉函数要做什么
    做。总的来说,这些评论不
    描述该函数如何执行
    它的任务。相反,应该是
    留给函数中的注释
    定义。

函数定义

  • 每个函数定义应该有
    描述什么的评论
    函数可以做任何棘手的事情
    关于它如何完成其​​工作。为了
    例如,在定义注释中
    你可以描述任何编码技巧
    您使用的,概述一下
    您经历的步骤,或解释原因
    你选择实现该功能
    以你所做的方式而不是使用
    一个可行的替代方案。例如,
    你可能会提到为什么它必须获得
    前半部分的锁定
    功能,但为什么不需要它
    下半场。

    请注意,您不应该只是重复
    函数给出的注释
    声明,在 .h 文件中或
    无论何处。重述一下就可以了
    简单介绍一下该函数的作用,但是
    评论的重点应该是
    关于它是如何做到的。

I like to follow the Google C++ Style Guide.

Which says:

Function Declarations

  • Every function declaration should
    have comments immediately preceding
    it that describe what the function
    does and how to use it. These
    comments should be descriptive
    ("Opens the file") rather than
    imperative ("Open the file"); the
    comment describes the function, it
    does not tell the function what to
    do. In general, these comments do not
    describe how the function performs
    its task. Instead, that should be
    left to comments in the function
    definition.

Function Definitions

  • Each function definition should have
    a comment describing what the
    function does and anything tricky
    about how it does its job. For
    example, in the definition comment
    you might describe any coding tricks
    you use, give an overview of the
    steps you go through, or explain why
    you chose to implement the function
    in the way you did rather than using
    a viable alternative. For instance,
    you might mention why it must acquire
    a lock for the first half of the
    function but why it is not needed for
    the second half.

    Note you should not just repeat the
    comments given with the function
    declaration, in the .h file or
    wherever. It's okay to recapitulate
    briefly what the function does, but
    the focus of the comments should be
    on how it does it.

梦魇绽荼蘼 2024-09-23 07:15:40

您应该使用像 doxygen 这样的工具,因此文档是由源代码中特制的注释生成的。

You should use a tool like doxygen, so the documentation is generated by specially crafted comments in your source code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文