dousha2020 2019-01-16 09:26
浏览 48
已采纳

GoLang接口名称的规则及其方法数[关闭]

Ones I had a discussion at work about correlation between interface name and number its methods. In particular, there is an unwritten rule about an interface with postfix notation ending with er in its name. The rule says such an interface should have one method contained.

Let's jump into an example. In the standard Go lang library, there is Pusher interface that does one thing "Push initiates an HTTP/2 server push". Here is its definition:

type Pusher interface {
  Push(target string, opts *PushOptions) error
}

https://golang.org/pkg/net/http/#Pusher

Good example. However, some colleague defended his implementation that contained more than two methods with er in name's postfix. The main argument was that there are stdlib's interfaces that violates such a rule. He referred to interface ReadCloser.

Looking at its definition:

type ReadCloser interface {
        Reader
        Closer
}

https://golang.org/pkg/io/#ReadCloser

I can say its wrong assumption. Interface embed in itself two other interfaces. How I interpret that? The rule is not violated.

How will you interpret such a case?

  • 写回答

1条回答 默认 最新

  • douxiong2999 2019-01-16 12:13
    关注

    This question is possibly going to get closed, either because it's deemed opinion based, or not code related, or whatever...

    However, golang being deemed quite opinionated, and because I deem standards very important, I'll chip in with my take on the unwritten rule, and how I'd reconcile, essentially why the ReadCloser is fine, but some other er interfaces might not be.


    I'd interpret the ReadCloser not to violate the "rule" (I'd call it a convention more like). I have a number of arguments why I'd say it's not violating the convention:

    1. It's not a standalone interface

    The ReadCloser interface isn't a self-contained interface. It's a composed interface. It's name reflects this. It concatenates Read and Close (the 2 functions in the interface you're after), and adds the er suffix. How both functions are implemented, and where they come from is irrelevant to the interface. If you read something, chances are you'll need to close the resource, too. It only makes sense to combine the two interfaces, so you can use the type that guarantees both Reader and Closer functionality to be available.

    2. Names mustn't stutter

    Much like the guidelines WRT package names stutter is to be avoided. Especially if it doesn't add any value. Technically, one could argue that the interface should be called ReaderCloser, but does that name communicate anything that isn't conveyed by the name ReadCloser? Surely not. The latter doesn't repeat the suffix, and reads easier.

    3. The er interfaces and CamelCasing

    The examples of single-function er interfaces like Stringer, or Publisher are indeed cut & dry. A Stringer contains the String function. End of story. Same as the Publisher interface.

    You'll note that the ReadCloser interface is CamelCased, suggesting it's a composit type. Just split the name on UpperCase characters, and add the suffix to each part. If the parts are bona-fide er interfaces, and the composite interface makes sense (cf point 1: if you read, chances are you'll need to close), then it's a valid composite interface.

    Examples of an invalid er interface would be:

    type FileReader interface {
        ReadCloserer
        ScanDir(string) ([]string, error)
        IsFile(string) bool
        Open(string, string) error
        // and so on
    }
    

    This interface contains too many BS functions to be packed into a FileReader interface.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境