delicious

Better Know A Ruby Thing: On The Use of Private Methods

Een murb'ed feed, 8 maanden geleden geplaatst onder ruby, programming, discussion & when to use.

Noel Rappin had something to say about private methods (in ruby):

I would consider making these methods private if:

  • The class has a specific other meaning for public methods, like a Rails controller or generator. In both cases, making a method public makes it something more than just a method, so in those cases I’m more likely to use privacy.
  • There is direct reason to believe that calling the method at the wrong time would cause problems. This doesn’t happen to me often, but I could imagine a class that interacted with a billing service, or which was doing some other thing with external state where you’d really want to lock down the context in which a method is called.
  • I have unusual reason to believe that I don’t want to maintain this method in a public-facing API. Again, this doesn’t happen to me often, but I can imagine a case were there was a system metaphor that I really wanted to keep clean, or a case where I knew there would be multiple users in other code bases that I don’t control. Again, this would for me go beyond just “this makes the class harder to maintain” – if I think the method is useful, it’s worth the cost to maintain.

Lees meer op de oorspronkelijke bron.

Op de hoogte blijven?

Maandelijks maak ik een selectie artikelen en zorg ik voor wat extra context bij de meer technische stukken. Schrijf je hieronder in:

Mailfrequentie = 1x per maand. Je privacy wordt serieus genomen: de mailinglijst bestaat alleen op onze servers.