Refactoring is a change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior.
2. Agenda
● What is Refactoring ?
● Why Should You Refactor?
● When Should You Refactor?
● Why Refactoring Works?
● Process of Refactoring
● Bad smells in code
● Examples of Refactoring
2
4. What is Refactoring?
a change made to the internal structure of software to
make it easier to understand and cheaper to modify
without changing its observable behavior.
4
6. Why Should You Refactor?
★ Refactoring Improves the Design of Software.
6
7. Why Should You Refactor?
★ Refactoring Improves the Design of Software.
★ Refactoring Makes Software Easier to Understand.
7
8. early refactorings is like wiping the dirt off a window so you can see beyond.
-Ralph Johnson
8
9. Why Should You Refactor?
★ Refactoring Improves the Design of Software.
★ Refactoring Makes Software Easier to Understand.
★ Refactoring Helps You Program Faster.
9
10. When Should You Refactor?
★ Refactor When You Add Function
★ Refactor When You Need to Fix a Bug
★ Refactor As You Do a Code Review
10
12. Programs, which are hard to work with
★ Programs that are hard to read are hard to modify.
★ Programs that have duplicated logic are hard to modify.
★ Programs that require additional behavior that requires you to change
running code are hard to modify.
★ Programs with complex conditional logic are hard to modif
12
17. Process of Refactoring
● Detect a problem
Is there a problem? What is the problem?
● Characterise the problem
Why is it necessary to change something? What are the benefits? Are
there any risks?
● Design a solution
What should be the "goal state" of the code? Which code
transformation(s) will move the code towards the desired state?
● Modify the code
Steps that will carry out the code transformation(s) that leave the code
functioning the same way as it did before.
17
18. Bad smells in code
● Duplicated code
● Long method
● Large Class
● Long Parameter List
● Lazy Class
18
19. Examples of Refactoring
● Rename
Methods, variables, classes or other java items. Changing the name of
a Java item to make it less confusing and misleading.
● Move Class
Moving a class into a package where it would fit better. All import
statements should refer to the given class.
● Extract Method
Break up long methods to improve readability and maintainability.
Replace a section of code that accomplishes a single task with a new
method with a descriptive name. This will also allow methods to be
reused for a specific function.
19
20. Examples of Refactoring
● Inline Method
A method’s body is just as clear as its name. Put the method’s body
into the body of its callers and remove the method.
20
21. Examples of Refactoring
● Remove Assignments to Parameters
The code assigns to a parameter. Use a temporary variable instead.
● Extract Class
You have one class doing work that should be done by two. Create a
new class and move the relevant fields and methods from the old class
into the new class.
21
22. Examples of Refactoring
● Introduce Local Extension
A server class you are using needs several additional methods, but you
can't modify the class. Create a new class that contains these extra
methods. Make this extension class a subclass or a wrapper of the
original.
So the idea is, If want to add features to a library what you would do?
If you can modify the source that’s the best thing, If you cannot modify the
source then you can always add in a wrapper to it.
22
23. Examples of Refactoring
● Replace array with Object
You have an array in which certain elements mean different things.
Replace the array with an object that has a field for each element.
String[] row = new String[3];
row[0] = “Liverpool”;
row[1] = “15”;
Instead
Team team = new Team();
team.setName(“Liverpool”);
team.setWins(“15”);
23
24. Examples of Refactoring
● Replace Magic Number with Symbolic Constant
You have a literal number with a particular meaning. Create a constant,
name it after the meaning, and replace the number with it.
double potentialEnergy(double mass, double height) {
return mass * 9.81 * height;
}
double potentialEnergy(double mass, double height) {
return mass * GRAVITATIONAL_CONSTANT * height;
}
static final double GRAVITATIONAL_CONSTANT = 9.81;
24
25. Examples of Refactoring
● Decompose Conditional
You have a complicated conditional (if-then-else) statement.
Extract methods from the condition, then part, and else parts.
if (date.before (SUMMER_START) || date.after(SUMMER_END))
charge = quantity * _winterRate + _winterServiceCharge;
else charge = quantity * _summerRate;
Refactored to
if (notSummer(date))
charge = winterCharge(quantity);
else charge = summerCharge (quantity);
25
26. Examples of Refactoring
● Remove setting method
A field should be set at creation time and never altered. Remove any
setting method for that field.
26
27. Examples of Refactoring
● Replace error code with exception
A method returns a special code to indicate an error.
int withdraw(int amount) {
if (amount > _balance)
return -1;
else {
_balance -= amount;
return 0;
}
}
27
28. Examples of Refactoring
● Replace error code with exception
A method returns a special code to indicate an error.
void withdraw(int amount) throws BalanceException {
if (amount > _balance) throw new BalanceException();
_balance -= amount;
}
28
29. When we should not Refactor?
● There are times when existing code is such a mess that although you
could refactor it, it would be easier to start from the beginning.
● The other time you should avoid refactoring is, when you are close to a
deadline. At that point the productivity gain from refactoring would
appear after the deadline and thus be too late.
29
Is refactoring just cleaning up code?No, It is cleaning up code in more efficient and controlled manner.Only changes made to make code "easy to understand" are refactoring. A good contrast is performance optimization. Like refactoring PO doesn't change the behaviour of the software (other than speed). It only ulter the internal structure, However purpose is different.
As you develop software, you probably find yourself swapping hats frequently. You start by trying to add a new function, and you realize this would be much easier if the code were structured differently. So you swap hats and refactor for a while. Once the code is better structured, you swap hats and add the new function. Once you get the new function working, you realize you coded it in a way that's awkward to understand, so you swap hats again and refactor
Without refactoring, the design of the program will decay. As people change code—changes to realize short-term goals or changes made without a full comprehension of the design of the code—the code loses its structure.It becomes harder to see the design by reading the code.
Computer responds by doing exactly what you tell it. In time you close the gap
between what you want it to do and what you tell it to do. Programming in this mode is all about saying exactly what you want.
But there is another user of your source code. Someone will try to read your code in a few months'time to make some changes. We easily forget that extra user of the code, yet that user is actually the most important.
When refactoring you have code that works but is not ideally structured. A little time spent refactoring can make the code
better communicate its purpose. Programming in this mode is all about saying exactly what you mean.
This sounds counterintuitive. When I talk about refactoring, people can easily see that it improves quality. Improving design, improving readability, reducing bugs, all these improve quality. But doesn't all this reduce the speed of development?
I strongly believe that a good design is essential for rapid software development. Indeed, the whole point of having a good design is to allow rapid development. Without a good design, you can progress quickly for a while, but soon the poor design starts to slow you down. You spend time finding and fixing bugs instead of adding new function. Changes take longer as you try to understand the system and find the duplicate code. New features need more coding as you patch over a patch that patches a patch on the original code base.
Programs have two kinds of value: what they can do for you today and
what they can do for you tomorrow. Most times when we are programming, we are focused on what we want the program to do today. Whether we are fixing a bug or adding a feature, we are making today's program more valuable by making it more capable.
You can't program long without realizing that what the system does today is only a part of the story. If you can get today's work done today, but you do it in such a way that you can't possibly get tomorrow's work done tomorrow, then you lose. Notice, though, that you know what you need to do today, but you're not quite sure about tomorrow. Maybe you'll do this, maybe that, maybe something you haven't imagined yet. I know enough to do today's work. I don't know enough to do tomorrow's. But if I only work for today, I won't be able to work tomorrow at all
Subversive? I don't think so.A schedule-driven manager wants me to do things the fastest way I can; how I do it is my business. The fastest way is to
refactor; therefore I refactor
Subversive? I don't think so.A schedule-driven manager wants me to do things the fastest way I can; how I do it is my business. The fastest way is to
refactor; therefore I refactor
Code smells
1. Duplicated Code - if you see the same code structure in more than one place, then all you to have to do is Extract method.
2. Large class = when a class is trying to do too much
3. Long parameter lst = long parameter list are haard to understand.
4.Lazy class - Each class you create costs money to maintain and understand. A class that isn't doing enough to pay for itself should be eliminated.
The other area of confusion is within the body of the code itself. It is much clearer if you use only the parameter to represent what has been passed in, because that is a consistent usage.You have to decide how to split the reponsibility of the class.
Create an extension class either as a subclass or a wrapper of the original. Add new features to the extension.
Create an extension class either as a subclass or a wrapper of the original. Add new features to the extension.
When you need to reference to the same logical number in more than one place and if the number ever changes, making the change everywhere will be a nightmare.
Decompose conditionals as much as possible
Providing a setting method indicates that a field may be changed. If you don't want that field to
change once the object is created, then don't provide a setting method (and make the field final).