14 Refactoring
El término refactoring se refiere a una transformación del código que no afecta la funcionalidad externa del sistema para mejorar la facilidad de comprensión o cambiar su estructura y diseño, con la finalidad de facilitar el mantenimiento en el futuro.
Martin Fowler nos presenta en su libro “Refactoring: Improving the Design of Existing Code”, una excelente guía para aprender más sobre este concepto.
- REFACTORING: IMPROVING THE DESIGN OF EXISTING CODE
- Por: MARTIN FOWLER , ADDISON-WESLEY, 1999
- ISBN: 9780201485677
Datos del libro
- Nº de páginas: 320 págs.
- Encuadernación: Tapa blanda
- Editorial: ADDISON-WESLEY
- Lengua: INGLÉS
- ISBN: 9780201485677
Uno de los conceptos que se presentan en este libro es Code Smell, este concepto se refiere a distintos tipos de errores de diseño que debemos evitar al momento de programar. En la siguiente tabla se muestra un catálogo de algunos de los Code Smells más sobresalientes.
| Smell | Refactoring |
|---|---|
| Comments. When you feel like writing a comment, | |
| first try “to refactor so that the comment becomes superfluous” | Rename Method, Extract Method, Introduce Assertion |
| Duplicated Code. Duplicated code is the most pervasive and | Chain Constructors, Extract Composite, Extract Method, Extract Class |
| pungent smell in software.It tends to be either explicit or subtle. | Form Template Method, Introduce Null Object, Introduce Polymorphic |
| Explicit duplication exists in identical code, while subtle | Creation with Factory, Method, Pull Up Method, Pull Up Field |
| duplication exists in structures or processing steps that are | Replace One/Many Distinctions with Composite, Substitute Algorithm, |
| outwardly different, yet essentially the same | Unify Interfaces with Adapter |
| Large Class. Fowler and Beck note that the presence of too | Extract Class, Extract Subclass, Extract Interface, |
| many instance variables usually indicates that a class is trying | Replace Data Value with Object, Replace Implicit Language |
| to do too much. In general, large classes typically contain | with Interpreter, Replace State-Altering Conditionals with State |
| too many responsibilities |
Tabla: Ejemplos de Code Smells.
Se pueden encontrar todos los Code Smells del libro aquí
Ejercicio: Realizar refactoring a las pruebas de TDD del archivo “python_lab.py”.
Nota: El siguiente comando nos permite conocer cuántas líneas de código tiene un archivo:
1 `wc -l nombre_del_archivo`