Afficher un message
Vieux 10/03/2008, 20h01   #3
Ranieri Teixeira
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Which is better -- method option or multiple methods?

I think it's a matter of modularity vs performance of the algorithm you
encapsulate in your methods.
You must balance its needs in your code.
Multiple methods let you get more modular, in fact easy to mantain code.
Multiple arguments oprions are for a method that this type of coding is
coherent.
It works for me.

On Mon, Mar 10, 2008 at 2:40 PM, Robert Klemme <shortcutter@googlemail.com>
wrote:

> 2008/3/10, Trans <transfire@gmail.com>:
> > In the case of limited options, like the following example, which is
> > better?
> >
> > class String
> >
> > def align(direction. spacing)
> > case direction
> > when :left
> > ...
> > when :right
> > ...
> > when :center
> > ...
> > end
> > end
> >
> > OR
> >
> > def align_left(spacing)
> > ...
> > end
> >
> > def align_right(spacing)
> > ...
> > end
> >
> > def align_center(spacing)
> > ...
> > end
> >
> > Thanks,

>
> The general wisdom suggests to favor multiple methods over a single
> method that changes behavior based on arguments. They are easier to
> implement (all the switching and conditions are not needed) but most
> of all the code is more modular on the interface level. Note that you
> can still use a single method to implement behavior internally if that
> is more efficient / easier.
>
> Unfortunately I do not have a pointer to a more thorough discussion
> handy...
>
> Kind regards
>
> robert
>
> --
> use.inject do |as, often| as.you_can - without end
>
>



--=20
Ranieri Barros Teixeira
Ci=EAncia da Computa=E7=E3o - Faculdade de Computa=E7=E3o - Universidade Fe=
deral do
Par=E1 (UFPA)
http://tickruby.blogspot.com
http://rubyxchart.rubyforge.org

  Réponse avec citation
 
Page generated in 0,06146 seconds with 9 queries