Smells To Refactorings

From: http://wiki.java.net/bin/view/People/SmellsToRefactorings

Smells Within Classes

SmellDescriptionRefactorings
CommentsShould only be used to clarify “why” not “what”.
Can quickly become verbose and reduce code clarity.
Extract Method
Rename Method
Introduce Assertion
Long MethodThe longer the method the harder it is to see what it’s doing.Extract Method
Replace Temp with Query
Introduce Parameter Object
Preserve Whole Object
Replace Method with Method Object
Long Parameter ListDon’t pass in everything the method needs; pass in enough so that the method can get to everything it needs.Replace Parameter with Method
Preserve Whole Object
Introduce Parameter Object
Duplicated Code Extract Method
Pull Up Field
Form Template Method
Substitute Algorithm
Large ClassA class that is trying to do too much can usually be identified by looking at how many instance variables it has. When a class has too many instance variables, duplicated code cannot be far behind.Extract Class
Extract Subclass
Type Embedded in NameAvoid redundancy in naming. Prefer schedule.add(course) to schedule.addCourse(course)Rename Method
Uncommunicative NameChoose names that communicate intent (pick the best name for the time, change it later if necessary).Rename Method
Inconsistent NamesUse names consistently.Rename Method
Dead CodeA variable, parameter, method, code fragment, class, etc is not used anywhere (perhaps other than in tests).Delete the code.
Speculative GeneralityDon’t over-generalize your code in an attempt to predict future needs.If you have abstract classes that aren’t doing much useCollapse Hierarchy

Remove unnecessary delegation withInline Class

Methods with unused parameters –Remove Parameter

Methods named with odd abstract names should be brought down to earth with Rename Method

Smells Between Classes

SmellDescriptionRefactorings
Primitive ObsessionUse small objects to represent data such as money (which combines quantity and currency) or a date range objectReplace Data Value with Object
Replace Type Code with Class
Replace Type Code with Subclasses
Replace Type Code with State/Strategy
If you have a group of fields that should go together, use Extract Class
If the primitives occur in a param lists useIntroduce Parameter Object
When working with an array consider Replace Array With Object
Data ClassClasses with fields and getters and setters and nothing else (aka, Data Transfer Objects – DTO)Move in behavior withMove Method
Data ClumpsClumps of data items that are always found together.Turn the clumps into an object with Extract Class
Then continue the refactoring withIntroduce Parameter Object
or Preserve Whole Object
Refused BequestSubclasses don’t want or need everything they inherit.
The Liskov Substitution Principle (LSP) says that you should be able to treat any subclass of a class as an example of that class.
Most of the time that’s fine, just don’t use what you don’t need. Occassionally you’ll need to create a new sibling class and use Push Down Method and Push Down Field
The smell is worst if a subclass is reusing behavior but does not want to support the interface of the superclass. In this case use Replace Inheritance with Delegation
Inappropriate IntimacyTwo classes are overly entertwined.Move Method Move Field
Change Bidirectional Association to Unidirectional Association
Extract Class
Hide Delegate
Replace Inheritance with Delegation
Lazy ClassClasses that aren’t doing enough should be refactored away.Collapse Hierarchy
Inline Class
Feature EnvyOften a method that seems more interested in a class other than the one it’s actually in. In general, try to put a method in the class that contains most of the data the method needs.Move Method May need to use Extract Methodfirst, then Move Method
Message ChainsThis is the case in which a client has to use one object to get another, and then use that one to get to another, etc. Any change to the intermediate relationships causes the client to have to change.Hide Delegate
Or try using Extract Method and then Move Method to move it down the chain.
Middle ManWhen a class is delegating almost everything to another class, it may be time to refactor out the middle man.Remove Middle Man
If only a few methods aren’t doing much, useInline Method
You could also consider turning the middle man into a subclass withReplace Delegation with Inheritance
Divergent ChangeOccurs when one class is commonly changed in different ways for different reasons. Any change to handle a variation should change a single class.Identify everything that changes for a particular cause and use Extract Class to put them all together.
Shotgun SurgeryThe opposite of Divergent Change. A change results in the need to make a lot of little changes in several classes.Use Move Method andMove Field to put all the changes into a single class.
Often you can use Inline Class to bring a whole bunch of behavior together.
Parallel Inheritance HierarchiesA special case of Shotgun Surgery. Every time you make a subclass of one class, you also have to make a subclass of another.Use Move Method andMove Field to combine the hierarchies into one.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章