I find myself wanting to use a standard name receivers in methods,
like 'self' in Python.
Is there an accepted standard for this? (I am inclined to go with
'me' as it is short yet readable.)
--
Han-Wen Nienhuys
Google Engineering Belo Horizonte
han...@google.com
> Is there an accepted standard for this? (I am inclined to go with
> 'me' as it is short yet readable.)
Unlike Python, the receiver tends to get named according to the actual
type, and many times it's just the lowercased first letter of the type
name, like (decoder *Decoder) or (d *Decoder).
--
Gustavo Niemeyer
http://niemeyer.net
http://niemeyer.net/blog
http://niemeyer.net/twitter
I think part of the point of the method-declaration syntax of Go
is so that you can choose any name you like -- it doesn't need a
separate standard.
One naming convention I use is the first-letter-of-type rule:
func (f *File) whatever ...
func (s string) wossname ...
The second is the appropriate-direction rule:
func (to *File) thingy( from *File ) ...
func (from *Source) tritTrot( to *Sink ) ...
The third is the well-known-convention rule:
func (x Numeric) makePoint( y Numeric) ...
Pick the name that makes the same sense as it would if the
receiver were any old argument -- after all, it pretty much is.
Chris
--
Chris "allusive" Dollin
Hi there,I find myself wanting to use a standard name receivers in methods,
like 'self' in Python.Is there an accepted standard for this? (I am inclined to go with
'me' as it is short yet readable.)
I hesitate to prolong the religious discussion, but Jan's examples prompt a few observations.
point to multiple different selves while the method is executing.
Kinda schizophrenic "self" then, right?
And what about
type Int int
func (i Int) foo() {}
? Is a number '3' self? I don't think so. 'self' or 'this' are naming
conventions applicable, if at all, to references. It sounds strange
elsewhere, IMHO.
Another consideration:
func (id T) name(arg U, arg2 V, ...)
is semantically equal to
func name(id T, arg U, arg2 V, ...)
So if someone argues that 'id' should be named 'self' or 'this' then,
to be consistent, such person should name every first parameter of any
function 'self' or 'this' as well. Then we would read in package
"fmt":
func Printf(self string, args ...interface{}) { ... }
In short, naming a parameter mechanically/monotonically is perhaps the
way a code generator can work. Humans are more creative for their own
good. Good naming pays back when the code is read.
-j
Konstantin Khomoutov Sep 3Re: [go-nuts] Re: Naming convention for method receiver?
> > I find myself wanting to use a standard name receivers in methods,
> > like 'self' in Python.
> >
> > Is there an accepted standard for this? (I am inclined to go with
> > 'me' as it is short yet readable.)
Just look at the code of the Go standard library -- you'll discover
that in most cases a short abbreviation of the receiver's type is used,
like
func (mr *MyReader)DoSomething {
mr.Read(...)
...
}
I find it a good idea because coupling of methods with their respective
types is more loose than in "true OOP" languages.
Nigel Tao 01:07 (9 hours ago)
Yes
On Tue, Sep 3, 2013 at 10:49 AM, <pablo....@gmail.com> wrote:
>
> Will I be frowned upon if I call the receiver "this"?
--
I hesitate to prolong the religious discussion, but Jan's examples prompt a few observations.
Jan asks us to consider:
func (n *Node) Walk() {
for ; n != nil; n = n.Next() {
Whatever(n.Data)
}}I realize Go is only loosely object-oriented, but the principals of Object-oriented programming are possible to follow to some extent in any programming language. One of the OOP practices I value is that methods on an object (instance of a type in this case) should operate on the type (e.g., getArea(), setName(), getNext(), etc). In the example above, Walk() is more properly implemented as a function that takes a node as argument and Walks the path:func Walk(n *Node) {
for ; n != nil; n = n.Next() {
Whatever(n.Data)}
}