From d80041ba7b334b9c1a82106e3c4e8f906403bd11 Mon Sep 17 00:00:00 2001 From: Yves Junqueira Date: Sun, 22 May 2011 23:27:21 +0200 Subject: [PATCH] style changes. - method names can't start with "Get", enforced by the style tool. - document our preference for return-by-reference, given the new design. --- DESIGN.md | 5 +++++ src/util/style/style.go | 16 ++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/DESIGN.md b/DESIGN.md index 9def011..06bd4f9 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -74,6 +74,11 @@ improvement in the following regards. * Identifier naming generally makes use of CamelCasing. Leading upper/lowercase letter dictates private/public to package, as defined by the Go programming language. +* Getter method names should *not* start with "Get". See + http://golang.org/doc/effective_go.html#Getters +* Functions and methods should preferably return objects by reference + (pointer) and callers can change them freely without fear of + synchronization issues, thanks to our clear channels-based design. To ease the above, there are three make targets: diff --git a/src/util/style/style.go b/src/util/style/style.go index cd1898a..f84d3b7 100644 --- a/src/util/style/style.go +++ b/src/util/style/style.go @@ -21,6 +21,7 @@ func main() { v := NewNodeChecker(fset) v.InterfaceName = regexp.MustCompile("I[A-Z][A-Za-z]+") + v.InvalidFuncName = regexp.MustCompile("^Get") // can't do negative match in Go's regexp? for _, pkg := range pkgMap { ast.Walk(v, pkg) @@ -28,8 +29,9 @@ func main() { } type NodeChecker struct { - fset *token.FileSet - InterfaceName *regexp.Regexp + fset *token.FileSet + InterfaceName *regexp.Regexp + InvalidFuncName *regexp.Regexp } func NewNodeChecker(fset *token.FileSet) *NodeChecker { @@ -42,6 +44,10 @@ func (v *NodeChecker) Visit(node ast.Node) (w ast.Visitor) { switch n := node.(type) { case *ast.TypeSpec: v.checkTypeName(n) + case *ast.FuncDecl: + if n.Recv != nil { // is a method. + v.checkFunc(n) + } } return v } @@ -62,6 +68,12 @@ func (v *NodeChecker) report(pos token.Pos, format string, args ...interface{}) allArgs...) } +func (v *NodeChecker) checkFunc(f *ast.FuncDecl) { + if v.InvalidFuncName.MatchString(f.Name.String()) { + v.report(f.Name.NamePos, "Bad name for method %q\n", f.Name) + } +} + func (v *NodeChecker) checkTypeName(typeSpec *ast.TypeSpec) { name := typeSpec.Name.Name switch t := typeSpec.Type.(type) {