[pyar] Como puedo no repetir código?

Bernardo Ezequiel Contreras vonbecmann en gmail.com
Mar Mayo 24 10:40:21 ART 2011

Refactoring>>Bad Smells in Code>>Data Clumps

Data Clumps

Data items tend to be like children; they enjoy hanging around in groups
together. Often you'll see the same three or four data items together in
lots of places: fields in a couple of classes, parameters in many method
signatures. Bunches of data that hang around together really ought to be
made into their own object. The first step is to look for where the clumps
appear as fields. Use Extract Class on the fields to turn the clumps into an
object. Then turn your attention to method signatures using Introduce
Parameter Object or Preserve Whole Object to slim them down. The immediate
benefit is that you can shrink a lot of parameter lists and simplify method
calling. Don't worry about data clumps that use only some of the fields of
the new object. As long as you are replacing two or more fields with the new
object, you'll come out ahead.

A good test is to consider deleting one of the data values: if you did this,
would the others make any sense? If they don't, it's a sure sign that you
have an object that's dying to be born.

Reducing field lists and parameter lists will certainly remove a few bad
smells, but once you have the objects, you get the opportunity to make a
nice perfume. You can now look for cases of feature envy, which will suggest
behavior that can be moved into your new classes. Before long these classes
will be productive members of society.

2011/5/24 Martin Volpe <martin.volpe en gmail.com>

> Hola lista,
> Estoy desarrollando un programa y tengo datos que casi siempre vienen de a
> tres, es decir, los guardo de a tres, los gráfico de a tres, los envio de a
> tres, siempre juntos.
> hasta ahora algo:
> #[...]
>         self.graph_ph = Graph()
>         self.graph_o2 = Graph()
>         self.graph_temp = Graph()
>         self.contenedor.add(self.graph_ph.canvas)
>         self.contenedor.add(self.graph_o2.canvas)
>         self.contenedor.add(self.graph_temp.canvas)
> #[...]
> Algo me dice que estoy haciendo algo mal, y es posible que en un futuro
> sean mas datos a tratar.
> Alguien tiene alguna idea de como automatizar esto?
> en otra parte del código lo resolví así:
> #[...]
>     def actualizar_gui(self, gui):
>         for i in self:
>             execute = "gui.%s_label.set_text('%s')" % (i, self[i])
>             eval(execute)
> #[...]
> pero no me termina de cerrar
> Alguna idea para este pobre pythonista perdido?
> saludos.
> --
> Martín Volpe @Argentina
> <http://volteck.net>
> _______________________________________________
> pyar mailing list pyar en python.org.ar
> http://listas.python.org.ar/listinfo/pyar
> PyAr - Python Argentina - Sitio web: http://www.python.org.ar/
------------ próxima parte ------------
Se ha borrado un adjunto en formato HTML...
URL: <http://listas.python.org.ar/pipermail/pyar/attachments/20110524/942f25b4/attachment.html>

More information about the pyar mailing list