Docsity

Refactoring IV - Advance Java Web Technology - Lecture Slides, Slides of Computer Science

Partial preview of the text

Download Refactoring IV - Advance Java Web Technology - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Refactoring IV

Previously discussed bad smells

  • Duplicated code — and other forms of redundancy
  • Long method — use short methods that delegate work
  • Large class — trying to do too much
  • Long parameter list — hard to use and remember
  • Divergent change — changes in one class for different reasons
  • Shotgun surgery — a change requires little changes all over
  • Feature envy — method uses too much from some other class
  • Data clumps — variables that frequently occur together
  • Primitive obsession — being afraid of making “small” objects
  • Switch statements — probably should use polymorphism

instead

Parallel inheritance hierarchies

• When you make a subclass of one class, you

have to make a corresponding subclass of

another class

• General strategy: Use Move Method and Move

Field to make instances of one hierarchy refer

to instances of the other

Lazy class

• Small classes are fine, but sometimes a class just

doesn’t do enough

  • If a class is very similar to its superclass, you can try to

use Collapse Hierarchy to merge the two classes

  • Eliminate the subclass by using Pull Up Field and Pull Up Method; or,
  • Eliminate the superclass by using Push Down Field and Push Down Method
  • If a class just isn’t doing very much, move all its

features into another class with Move Field and Move

Method

Temporary field

• We expect an object to use all its fields

• It’s confusing when an instance variable is

used only in certain cases

  • Use Extract Class to create a home for these variables
  • Eliminate conditional code with Introduce Null Object

• Sometimes programmers will add instance

variables to avoid long parameter lists

between communicating methods

Message chains

• A message chain is a sequence such as

BazObject b =foo.getBar().getBaz()

  • Here we are asking foo for a bar object so that we can ask it for a baz object
  • Often, but not always, these are getter methods
  • Message chains can be abbreviated or eliminated by Hide Delegate
  • As an example of Hide Delegate , we can introduce the following method into foo’s class: - BazObject getBaz() { return bar.getBaz(); }
  • And then we can just call BazObject b = foo.getBaz();

Inappropriate intimacy

• Classes may make too much use of each

other’s fields and methods

  • Use Move Method and Move Field to reduce the association
  • Try to Change Bidirectional Association to Unidirectional - The idea here is to take the class that is less dependent on the other class and remove the remaining dependencies
  • If the classes have common needs, try Extract Class

Replace Inheritance With

Delegation

  • Sometimes a subclass inherits more from its superclass than you want it to have
  • Example: Suppose class Sub extends Super, inherits desired methods int foo() and void bar(), along with other methods it does not want, and adds method int baz()
  • Replace class Sub extends Super {...} with class Sub { // class name should also be changed Super s = new Super(); int foo() { return s.foo(); } // delegate to s void bar() { s.bar(); } // delegate to s int baz() {...} // new method }

Incomplete library class

  • Library classes (such as those supplied by Sun) don’t always

do everything we want them to do

  • It’s usually impossible to modify these library classes
  • Use Introduce Foreign Method:
  • Write the method you want, as if it were in the library class
  • Make an instance of the library class the first parameter
  • Add a comment that describes the method as a “foreign method”
  • Example:
  • private static Date nextDay(Date arg) { // foreign method, should be in Date return new Date(arg.getYear(), arg.getMonth(), arg getDate() + 1); }

Data class

  • Classes that just hold data, and maybe setters and getters for

that data, are undesirable

  • If a field is public, use Encapsulate Method to make it private

and add setters and getters (if it isn’t too late)

  • If a method returns a collection, use Encapsulate Collection
    • Make the method return a read-only view (java.util.Collection s supplies methods such as unmodifiableSet(Set) and unmodifiableMap(Map))
    • Provide add and remove methods as appropriate
  • Try to use Move Method to move the setters and getters to

the class that needs them

Comments

• Fowler says “comments often are used as a

deodorant”

  • If you need a comment to explain what a block of code does, use Extract Method
  • If you need a comment to explain what a method does, use Rename Method
  • If you need to describe the required state of the system, use Introduce Assertion

• This should not discourage the use of

comments (especially javadoc comments)—Docsity.com

Advice from Kent Beck

  • “Stopping is the strongest move in the refactorer’s repertoire.” - The idea is to take small goals, refactor in small steps, so that you can quit in a stable state, having made progress
  • “Somewhere your code smells bad. Resolve to get rid of the problem. Then march toward that goal.”
  • Stop when you are unsure
    • If the code is already better, go ahead and release it
    • If you aren’t sure of your changes, discard them and go back to the previous stable state
  • Backtrack. If the code quits working, and you haven’t tested in a while, don’t debug—throw away the changes since the last working tests - Emotionally difficult, but rewriting is faster and easier than debugging
  • Work with someone. There are many advantages to workingDocsity.com

Soundbites II

• Any fool can write code that a computer can

understand. Good programmers write code

that humans can understand.

• Refactoring (noun): a change made to the

internal structure of software to make it easier

to understand and cheaper to modify without

changing the observable behavior of the

software.

• Refactor (verb): to restructure software by

Soundbites III

• Three strikes and you refactor.

  • “The first time you do something, you just do it. The second time you do something similar, you wince at the duplication, but you do the duplicate thing anyway. The third time you do something similar, you refactor.”

• Don’t publish interfaces prematurely. Modify

your code ownership policies to smooth

refactoring.