13
REFACTORING

REFACTORING. What is refactoring ? In refactoring, you start with the basic code and make it better. Change the internal structure of the existing code

Embed Size (px)

Citation preview

Page 1: REFACTORING. What is refactoring ? In refactoring, you start with the basic code and make it better. Change the internal structure of the existing code

REFACTORING

Page 2: REFACTORING. What is refactoring ? In refactoring, you start with the basic code and make it better. Change the internal structure of the existing code

What is refactoring ?In refactoring, you start with the basic code and make it better. Change the internal structure of the existing code by changing the factoring without changing the external behaviour.The idea is that you should improve the code in some significant way. for example:-Reducing near duplicate code-Improved cohesion,lessened coupling-Improved understanding,flexibility,abstraction etc.

Page 3: REFACTORING. What is refactoring ? In refactoring, you start with the basic code and make it better. Change the internal structure of the existing code

Why Refactor?Advantage of refactoring:1.Improved code2.Readability3.Reduced complexity hence easy to maintain4.Create a more expressive object model (improve extensibility-design principle where implementation takes future growth into consideration).Extreme Programming and Agile methodologies promote refactor as an integral part of software development cycle.

Page 4: REFACTORING. What is refactoring ? In refactoring, you start with the basic code and make it better. Change the internal structure of the existing code

Rules of three (XP)This is the most important rule in refactoring.When you have a piece of code which is replicated in your code, If its used once then its fine but if its twice its bad but tolerable but if the code is replicated for the third time the code needs to be refactored; that is it should be extracted into a new procedure.Duplication is a bad programming practice as it makes the code harder to maintain.However refactoring is also time consuming and hence the code should be refactored only after it has been used more than twice.

Page 5: REFACTORING. What is refactoring ? In refactoring, you start with the basic code and make it better. Change the internal structure of the existing code

Rules of ThreeThe problem with replication is that if the rules encoded in the replicated code changes whoever maintains the code will have to make the changes in the other parts of the code too where it has been replicated.This makes the process error prone and hence leads to problems. especially if the code is present in different locations.

Page 6: REFACTORING. What is refactoring ? In refactoring, you start with the basic code and make it better. Change the internal structure of the existing code

Code smellA code smell is a symptom in the source code like a bad smell and refactoring is used to clean them up.The idea is, if the code looks ugly,awkward or smelly fix it.

Page 7: REFACTORING. What is refactoring ? In refactoring, you start with the basic code and make it better. Change the internal structure of the existing code

Code Smell and fixesDuplicated Code

-- extract out the common bits into their own method (extract method) if code is

in same class

--if two classes duplicate code, consider extract class to create a new class to

hold the shared functionality.

Page 8: REFACTORING. What is refactoring ? In refactoring, you start with the basic code and make it better. Change the internal structure of the existing code

ExampleLazy Class

Class doesn't seem to be doing anything. Get rid of it!

collapse hierarchy if a subclass is nearly vacuous.

inline class (stick the class' methods and fields in the class that was using it and get rid of original class).

Page 9: REFACTORING. What is refactoring ? In refactoring, you start with the basic code and make it better. Change the internal structure of the existing code

exampleSpeculative generality

Class designed to do something in the future but never ends up doing it. Thinking too far ahead. Or you though you needed this generality but you didn't.

-- like above, collapse hierarchy or inline class

Page 10: REFACTORING. What is refactoring ? In refactoring, you start with the basic code and make it better. Change the internal structure of the existing code

exampleMessage chains

1. Say you want to send a message to object D in class A but you have to go through B to get C and C to get D.

2. -- In other words, you were writing something like a.getB().getC().getD().messageToD()

3. -- These are strong violations of object encapsulation / active objects philosophy

4. -- find a sensible spot in the middle where a logical interface can be inserted.

5. -- e.g. use hide delegate to hide C and D in B, and add a method to B that does what A wanted to do with D:a.getB().letDKnowThisMessage().

Page 11: REFACTORING. What is refactoring ? In refactoring, you start with the basic code and make it better. Change the internal structure of the existing code

ToolEclipse has a refactor menu which refactors your code automatically.

Page 12: REFACTORING. What is refactoring ? In refactoring, you start with the basic code and make it better. Change the internal structure of the existing code

Referenceshttp://pl.cs.jhu.edu/oose/lectures/refactoring.shtmlhttp://www.cs.usfca.edu/~parrt/course/601/lectures/refactoring/refactoring.htmlhttp://en.wikipedia.org/wiki/Code_smellhttp://en.wikipedia.org/wiki/Rule_of_three_(computer_programming)

Page 13: REFACTORING. What is refactoring ? In refactoring, you start with the basic code and make it better. Change the internal structure of the existing code

THANK YOU