Guia curto-e-grosso para refactoring

Muito legal esse post, do Davide Savazzi:

http://lab.jugtorino.it:8081/roller/page/dsavazzi/20030804#bad_smells_in_code

Bad Smells in Code

A list of bad smells that can be found in code and their suggested refactorings (notes taken from the book Refactoring).

Duplicated code

If you have the same expression in two methods of the same class Extract Method.

If you have the same expression in two sibling subclasses Extract Method in both classes and then Pull Up Method.

If you have the same expression in two unrelated classes Extract Method in both classes and then consider to Extract Class.

If the expression shared is not exactly the same you can use Form Template Method or Substitute Algorithm.

Long Method

Extract Method.

If you have conditionals and loops you can Decompose Conditional.

Large Class

Use Extract Interface to determine how the class is used and then Extract Class or Extract Subclass.

Long Parameter List

If you can get the data from an object you already know about use Replace Parameter With Method.

If you take some parameters from an object, replace them with the object itself using Preserve Whole Object.

Else you can Introduce Parameter Object.

Divergent Change

If a class is commonly changed in different ways for different reasons,
identify everything that changes for a particular cause and use Extract Class to put them all together.

Shotgun Surgery

If one change can alter many classes, use Move Method and Move Field to put all the changes into a single class.

Feature Envy

If a method seems more interested in a class other than the one it actually is in use Move Method.

Data Clumps

Bunches of data that hang around together ought to be made into their own object: Extract Class.

Primitive Obsession

Use small objects for small tasks. Replace Data Value with Object, Replace Type Code with Class, Replace Array with Object.

Lazy Class

If you have subclasses that aren’t doing enough, try to use Collapse Hierarchy.

Nearly useless components should be subjected to Inline Class.

Speculative Generality

If you have abstract classes that aren’t doing much use Collapse Hierarchy.

Methods with unused parameters should be subject to Remove Parameter.

Rename Methods with odd abstract names.

Message Chains

At various points in the chain you can Hide Delegate.

Middle Man

Remove Middle Man and talk to the object that really knows what’s going on.

Incomplete Library Class

If there are just a couple of methods that you wish the library class had, use Introduce Foreign Method.

If there is a whole load of extra behavior, you need Introduce Local Extension.

Data Class

Encapsulate Collections.

Remove Setting Method on any field that should not be changed.

Look at how the data class is used: try to use Move Method to move behavior into the data class.

Comments

If you need a comment to explain what a block of code does, try Extract Method.

If you need to state some rules about the required state of the system, use Introduce Assertion.

vixi
resumiram o livro todo

cv, muito legal isso! valeu!

[]'s