doutangu4671 2016-02-24 00:56
浏览 30
已采纳

注释中的函数名称

Why do Go programmers put the name of the function as the first word of the function comment? This is a technical question please not a religious one.

As an example

// addOptions adds options s to opt
func addOptions(s string, opt interface{}) (string, error) {
....

Dose this not violate the SPOT (Single Point Of Truth) principle?

I am asking because I am sure there is a very good reason.

That is how it is done in 'The GO Programming Language' (Alan A. A. Donovan and Brian W. Kernighan). Also it was done like that in 'The C Programming Language' (Dennis M. Ritchie and Brian W. Kernighan) back in 1988. If it has lasted this long it seems there must be a good reason?

The SPOT principle is attributed to Kernighan by Eric S. Raymond in 'The Art of UNIX Programming'.

  • 写回答

1条回答 默认 最新

  • dongque20030402 2016-02-24 01:10
    关注

    TL;DR it makes grepping easier.

    Quoting from https://golang.org/doc/effective_go.html#commentary referred to by @twotwotwo:

    Doc comments work best as complete sentences, which allow a wide variety of automated presentations. The first sentence should be a one-sentence summary that starts with the name being declared.

    ...

    If the name always begins the comment, the output of godoc can usefully be run through grep. Imagine you couldn't remember the name "Compile" but were looking for the parsing function for regular expressions, so you ran the command,

    $ godoc regexp | grep parse
    

    If all the doc comments in the package began, "This function...", grep wouldn't help you remember the name. But because the package starts each doc comment with the name, you'd see something like this, which recalls the word you're looking for.

    $ godoc regexp | grep parse
        Compile parses a regular expression and returns, if successful, a Regexp
        parsed. It simplifies safe initialization of global variables holding
        cannot be parsed. It simplifies safe initialization of global variables $
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序