布尔谓词函数的命名应遵循Go语言的惯用方式。

huangapple go评论82阅读模式
英文:

Go idiomatic way to name boolean predicate functions

问题

在Go语言中,命名布尔谓词函数的惯用方式是将函数名以"Is"或"Has"开头,后面跟着描述性的动词短语。因此,可以使用以下方式来命名布尔谓词函数:

func IsActiveInLastMonth() bool {}

或者

func HasBeenActiveInLastMonth() bool {}

这样的命名方式可以清晰地表达函数的用途和返回值的含义。

英文:

Lets say you are working with a func that returns a bool as to whether a user has been active in the last month.

In Ruby:

def active_in_last_month?;end

In C#

public bool WasActiveInLastMonth(){}

What is the idiomatic way of naming boolean predicate functions in Go?

答案1

得分: 12

tl;dr

func wasActiveInLastMonth() bool

完整回答

我查看了一些知名开源项目的GitHub存储库,选择了一个半随机的文件,并找到了以下内容:

Etcd lease/lessor.go

func (le *lessor) isPrimary() bool

Kubernetes service/service_controller.go

func (s *ServiceController) needsUpdate(oldService *v1.Service, newService *v1.Service) bool

func portsEqualForLB(x, y *v1.Service) bool

func portSlicesEqualForLB(x, y []*v1.ServicePort) bool

Consul agent/acl.go

func (m *aclManager) isDisabled() bool

Docker Moby(Docker的开源上游)cli/cobra.go

func hasSubCommands(cmd *cobra.Command) bool

func hasManagementSubCommands(cmd *cobra.Command) bool

我认为这四个项目代表了一些最受好评和最著名的Go代码。看起来"is/has"模式非常普遍,尽管不是唯一的模式。如果你选择这种模式,你肯定能够将其作为一种事实上的习惯用法来进行辩护。

英文:

tl;dr

func wasActiveInLastMonth() bool

Full Answer

I looked in the GitHub repos of some well known open source projects, picked a semi-random file, and found the following:

Etcd lease/lessor.go

func (le *lessor) isPrimary() bool

Kubernetes service/service_controller.go

func (s *ServiceController) needsUpdate(oldService *v1.Service, newService *v1.Service) bool

func portsEqualForLB(x, y *v1.Service) bool

func portSlicesEqualForLB(x, y []*v1.ServicePort) bool

Consul agent/acl.go

func (m *aclManager) isDisabled() bool

Docker Moby (open source upstream of Docker) cli/cobra.go

func hasSubCommands(cmd *cobra.Command) bool

func hasManagementSubCommands(cmd *cobra.Command) bool

I would say that these four projects represent some of the most well reviewed and famous go code in existence. It seems that the is/has pattern is very prevalent although not the only pattern. If you choose this pattern you will certainly be able to defend your choice as a de facto idiom.

答案2

得分: -2

《Go编程语言》(Donovan和Kernighan著)在第6章中建议:

> 仅仅访问或修改类型内部值的函数被称为getter和setter。然而,在命名getter方法时,我们通常省略Get前缀。这种对简洁性的偏好不仅适用于字段访问器,还适用于所有方法,以及其他冗余前缀,比如Fetch、Find和Lookup。

根据这个建议,如果函数是私有的,我会将其命名为"LastMonth"或"lastMonth",以跳过前缀。

英文:

"The Go Programming Language" by Donovan and Kernighan recommends in Chapter 6 that

> Functions that merely access or modify internal values of a type..are called getters and setters. However, when naming a getter method we usually omit the Get prefix. This Preference for brevity extends to all methods not just field accessors, and to other reductant prefixes as well, such as Fetch, Find and Lookup

From this advice I would name the function "LastMonth" Or "lastMonth" if it is private to the package to skip the prefix

huangapple
  • 本文由 发表于 2014年1月5日 22:56:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/20935005.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定