duanhuanzhi6431 2014-11-17 13:23
浏览 35
已采纳

在导出的名称上排除godocs是否有害?

According to "Effective Go" golang.org/doc/effective_go

Every exported (capitalized) name in a program should have a doc comment.

Let's say I have a view handler on a simple web application

// Handle the front page of the website
func FrontPageView(w http.ResponseWriter, r *http.Request) {
    controllers.RenderBasicPage(w, "frontPage") 
}

My question is this: is that godoc really necessary? Perhaps I'm just in love with Robert Martin's Clean Code right now, but it seems an effectively named variable, in this case FrontPageView removes the need for such a godoc. This is probably a derivative/duplicate of "are javadocs necessary?" or "are python docstrings necessary?", but I do want to make sure that when learning a new language I'm sticking to the language-specific canonical ways-to-do-things.

  • 写回答

2条回答 默认 最新

  • dtd14883 2014-11-17 13:34
    关注

    Note that golint would tell you that the doc for FrontPageView() must start with

    // FrontPageView ...
    

    And yes, it is good practice to include (here a short) comment on all exported methods, functions, as described also in "Go Code Review Comments".

    Comments documenting declarations should be full sentences, even if that seems a little redundant.
    This approach makes them format well when extracted into godoc documentation.

    Comments should begin with the name of the thing being described and end in a period:

    // A Request represents a request to run a command.
    type Request struct { ...
    
    // Encode writes the JSON encoding of req to w.
    func Encode(w io.Writer, req *Request) { ... 
    

    it's my understanding that effectively clean code means keeping functions simple with a name that describes the function's one role;

    Then the doc can include edge cases (even if in your case there are none).
    In any case, adding a short doc won't make it "less clean".

    as they get more complex you split them into multiple, equally simple, functions.

    I use gocyclo for this: any cyclomatic complexity above 10, and I split the function.

    Also, changes would require an update in the godoc as well as the name

    That is the idea: keeping the doc in sync (and golint helps)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上