Is there any nomenclature for private methods?

  • Thread starter Iñaki Baz Castillo
  • Start date
I

Iñaki Baz Castillo

Hi, I don't like the way of declaring publi and private methods since it's =
not=20
really clear. AFAIK there are you ways:

1)
class C
def public_1
end

def public_2
end

private:
def private_1
end

public:
def public_3
end

or:

2)
class C
def public_1
end

def public_2
end

def private_1
end

def public_3

private :private_1
end



I prefer 2) but until the end of class it's not easy to know if a method is=
=20
public or private/protected. So i'm thinking about use a special nomenclatu=
re=20
for private methods, for example:

def _private_1


Is there any "standar" for this? I've seen lost of ethods called "__xxxx__"=
=20
or "_xxxxx" but not sure the reason of that name.

Thanks a lot.

=2D-=20
I=C3=B1aki Baz Castillo
 
T

Trans

Hi, I don't like the way of declaring publi and private methods since it's= not
really clear. AFAIK there are you ways:

1)
class C
def public_1
end

def public_2
end

private:
def private_1
end

public:
def public_3
end

or:

2)
class C
def public_1
end

def public_2
end

def private_1
end

def public_3

private :private_1
end

One way is do it immediately after the method. E.g.

def private_1
end
private :private_1

def public_3
end

I've seen other coders use the declarations before every single method
regardless

public
def public_2
end

private
def private_1
end

public
def public_3
end


I agree that these declarations are one of the "sore-eyes" of Ruby,
but what else can be done? Matz has always seemed against allowing:

private def foo()
end

In many ways it's really not important though. A more Ruby-esque way
in my opinion is to just make them all public and document them
appropriately. There is the exception of method_missing's behavior
however.
I prefer 2) but until the end of class it's not easy to know if a method i= s
public or private/protected. So i'm thinking about use a special nomenclat= ure
for private methods, for example:

def _private_1

Is there any "standar" for this? I've seen lost of ethods called "__xxxx__= "
or "_xxxxx" but not sure the reason of that name.

An initial underscore or double underscore basically indicates
"special/avoid overwrite" --meaning it's meant to reduce the
likelihood that someone else might define a method/attribute of the
same name. The most common occurrence is __send__.

T.
 
R

Ryan Davis

I prefer 2) but until the end of class it's not easy to know if a =20
method is
public or private/protected. So i'm thinking about use a special =20
nomenclature
for private methods, for example:

def _private_1


Is there any "standar" for this? I've seen lost of ethods called =20
"__xxxx__"
or "_xxxxx" but not sure the reason of that name.

I don't ever use private or protected, but I do use that naming =20
convention to convey the notion. Don't use double-underscore, that's =20
basically considered reserved.


def _think_twice_before_calling
...
end
 
A

Arlen Cuss

SGksCgpPbiBGcmksIEFwciAyNSwgMjAwOCBhdCA4OjE4IFBNLCBJw7Fha2kgQmF6IENhc3RpbGxv
IDxpYmNAYWxpYXgubmV0PiB3cm90ZToKCj4gQnV0IGlmIHlvdSBkbyBhIGZyYW1ld29yayBvdGhl
ciBwZW9wbGUgdG8gcHJvZ3JhbW1lIGluIGl0IG1heWJlIGlzCj4gbmVjY2VzYXJ5IHRvIGFic3Ry
YWN0IHRoZSBBUEkgYW5kIHVzZSBwcml2YXRlIG1ldGhvZHMgYXMgc2VjdXJpdHksCj4gaXNuJ3Q/
Cj4KCkJ1dCB0aGV5J3ZlIGdvdCB5b3VyIGNvZGUgYW55d2F5LiBUaGV5IGNhbiBzZWUgaXQgYW5k
IG1vZGlmeSBpdCBpZiB0aGV5CmNob29zZSAoZm9yIGV4YW1wbGUuLiByZXBsYWNpbmcgYWxsIGBw
cml2YXRlJ3Mgd2l0aCBgcHVibGljJ3MuLiB0aGVuIHdoYXQ/KS4KQWxzbywgbm90ZSB5b3UgY2Fu
IGFsd2F5cyB1c2UgYHNlbmQnIHRvIGdldCBhcm91bmQgcHJvdGVjdGlvbjoKCj4+IGNsYXNzIFNv
bWV0aGluZwo+PiAgIHByaXZhdGUKPj4gICBkZWYgaGkKPj4gICAgIHB1dHMgIk9vcHMhIgo+PiAg
IGVuZAo+PiBlbmQKPT4gbmlsCj4+IHMgPSBTb21ldGhpbmcubmV3Cj0+ICM8U29tZXRoaW5nOjB4
YjdhMDkyNzQ+Cj4+IHMuaGkKTm9NZXRob2RFcnJvcjogcHJpdmF0ZSBtZXRob2QgYGhpJyBjYWxs
ZWQgZm9yICM8U29tZXRoaW5nOjB4YjdhMDkyNzQ+CiAgICBmcm9tIChpcmIpOjEwCj4+IHMuc2Vu
ZCA6aGkKT29wcyEKPT4gbmlsCj4+CgpBYnN0c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oZnJvbSAn
MDUpCnN0YXJ0aW5nIGhlcmU6CgpodHRwOi8vYmxhZGUubmFnYW9rYXV0LmFjLmpwL2NnaS1iaW4v
dmZyYW1lLnJiL3J1YnkvcnVieS10YWxrLzE1MzY3Mj8xNTM2NjYtMTU0NjgyK3NwbGl0LW1vZGUt
dmVydGljYWwKCkFybGVuCg==
 
F

Florian Gilcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


Abstracting the API is a good point, but perhaps they should be
disconnected
in a manner other than using private/protected methods...

Cheers,
Arlen


Actually, I see private and protected as hint of intension. Even in
Java, you can circumvent them.

The method is private => it's for internal use - so don't cry if it
gets changed in some minor version, you should not rely on it anyway.
So if you really, really have to use __send__, know the consequences.

Regards,
Florian Gilcher
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (Darwin)

iEYEARECAAYFAkgR4rsACgkQJA/zY0IIRZYwpgCbB0QxAxv1sT7d4KP/gbWKqyvL
IFIAn2zmRT2OWz9cY/7PWJokRsUu4lbh
=1jKc
-----END PGP SIGNATURE-----
 
X

Xavier Noria

Unfortunately (in my opinion), I believe that's been reverted, so that
#send will be what it always has been and a new method, #public_send,
will be send that only works on public methods.

The Ruby Programming Language says so on page 274.
 
A

Avdi Grimm

I don't ever use private or protected, but I do use that naming convention
to convey the notion. Don't use double-underscore, that's basically
considered reserved.


def _think_twice_before_calling
...
end

Good lord, one of the [smaller] reasons I enjoy coding Ruby over
Python is avoiding that kind of ugliness. May I ask *why* you would
prefer that over real private/protected?

--
Avdi

Home: http://avdi.org
Developer Blog: http://avdi.org/devblog/
Twitter: http://twitter.com/avdi
Journal: http://avdi.livejournal.com
 
R

Ryan Davis

Good lord, one of the [smaller] reasons I enjoy coding Ruby over
Python is avoiding that kind of ugliness. May I ask *why* you would
prefer that over real private/protected?

1) makes testing easier / cleaner.

2) there IS no real private/protected. Just try to keep me out if I
want in.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top