From: Fabian Streitel Date: 2009-09-01T17:02:04+09:00 Subject: Re: Private methods’ viability (was: What is the ruby conventions to name private method?) --0015174bed1ab4167404727f9094 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable same as you said :-) I'm excited to hear Ryan Davis's approach since he claims to never use private methods at all. 2009/9/1 Tony Arcieri > I also use private methods to separate out internal implementation detail= s > that I expect to change (e.g. if we decide to switch to a different > algorithm). I also decompose into small functions a lot, perhaps a side > effect of Erlang on the brain. > > However, the instant I feel the urge to use __send__ to call a private > method I make that method public. > > On Mon, Aug 31, 2009 at 4:29 PM, Josh Cheek wrote: > > > On Mon, Aug 31, 2009 at 4:49 PM, Shot (Piotr Szotkowski) > >wrote: > > > > > Ryan Davis: > > > > > > > 4) Don't have private methods if you don't need them. > > > > 5) You don't need them. No, really. > > > > > > I=92ll bite. Note: I consider myself a beginner Rubyist and I=92d lov= e to > > > hear your (and others=92!) reasons for the above and/or against the > below. > > :) > > > > > > I use private methods when I factor them out of my public methods (on= ce > > > the public methods have decent spec coverage). This makes me think > > > harder on the (hopefully =96 minimal) public API of my objects and > > > prevents me from calling such methods =91in just that single place > > > where it would be useful=92; a method=92s either useful to the outsid= e > > > world (and should be promoted to public API with individual, > descriptive > > > spec coverage) or not, period. Using private methods helps me keep > > > checks on this. > > > > > > I also use protected attr_readers so that my objects can tell whether > > > they =3D=3D others of the same kind without exposing their instance > > > variables to the outside world; an attr_reader on an Array/Hash/Set > > > does not guarantee that the given Array/Hash/Set is not modifiable fr= om > > > the outside world=B9, hence I=92d rather not expose such instance var= iables > > > more than necessary. > > > > > > =B9 It=92s one of the few places where Ruby diverges from the princip= le > > > of least surprise; I can see certain lovely symmetry and simplicity > > > in attr_reader working the same for all variables, but I found the na= me > > > a bit misleading when I started to learn Ruby =96 my thoughts were th= at > > > it should either not be called a reader or should return a dup of the > > > variable in question=85 (I did get used to this since then, though, a= nd > > > realised that other languages=92 generic getters usually do the same.= ) > > > > > > =97 Shot > > > -- > > > Born to be mild. [elpanda] > > > > > > -----BEGIN PGP SIGNATURE----- > > > Version: GnuPG v1.4.9 (GNU/Linux) > > > > > > iEYEARECAAYFAkqcRXAACgkQi/mCfdEo8UpIWwCeNYQ+NZNW895936Gbnwj3btMA > > > ylYAnRbvAkMrzP0Ep6SW1y6kl07id7aV > > > =3D0fIP > > > -----END PGP SIGNATURE----- > > > > > > > > I do the same thing, I use private methods to encapsulate internal logi= c. > > Sure something _could_ be used elsewhere, or for reasons that I haven't > > considered, but it pollutes the public namespace, and obfuscates the AP= I. > > The outside world doesn't need (or probably want) to be subjected to th= e > > internal logic of my code. Even I, using my own code later, prefer this > > approach, because it simplifies use. > > > > Take the Array class as an example, it has 96 private methods, and 85 > > public > > methods. The public ones are the ones I almost certainly am looking for= , > > how > > burdensome would it be if they made them all public and I had to look a= t > > 181 > > methods, more than half of which are irrelevant, every time I was tryin= g > to > > find something. > > > > This is the main reason I use private methods. I also use Rails, in whi= ch > > private vs public is relevant because it affects behaviour. For example= , > a > > public method in a controller will try to render a template after it ha= s > > been called. > > > > I have, however, experienced situations where things are irritating as = a > > result of private methods, for example, in the API for define_method > > http://www.ruby-doc.org/core/classes/Module.html#M001654 it says "This > > block > > is evaluated using instance_eval, a point that is tricky to demonstrate > > because define_method< > > http://www.ruby-doc.org/core/classes/Module.html#M001654>is > > private . > (This > > is > > why we resort to the send hack in this example.)" So it can make things > > difficult, as well. > > > > > > -- > Tony Arcieri > Medioh/Nagravision > --0015174bed1ab4167404727f9094--