The process of changing a software system in such a way that it does not alter the external (observable) behavior of the code yet improves its internal structure, to make it easier to understand and cheaper to modify.
1. Ref act oring
Few minutes To better Code …
Prepared By: Diaa Alsaleh
Feb 2015
2. Refactoring
– What is refactoring?
– Why refactoring?
– When refactoring?
– Refactoring Feature in Visual Studio
Ref act or ing
3. What is refactoring?
• Refactoring is the process of changing a
software system in such a way that it
does not alter the external (observable)
behavior of the code yet improves its
internal structure, to make it easier to
understand and cheaper to modify.
Ref act or ing
4. Why refactoring
• Refactoring improves the design of the
software .
• Refactoring makes software easier to
understand
– helps make your code more readable.
– increases program comprehension:
leads to higher levels of
understanding that otherwise may be
missed
Ref act or ing
5. Why refactoring (cont’d)
• Refactoring helps you program faster
– less bugs and helps correct bugs:
errors need to be modified only in one
place
Ref act or ing
6. When refactoring …
• Duplicated Code:
“If you see the same code structure in more than
one place, you can be sure that your program will
be better if you find a way to unify them”.
–Extract Method: When you have the same
expression in two methods of the same class.
Ref act or ing
Bad smells in code !
7. When refactoring …
• Large Class :
“When a class is trying to do too much, it often shows up
as too many instance variables. When a class has too many
instance variables, duplicated code cannot be far behind.”
Ref act or ing
Bad smells in code !
8. When refactoring (Cont’d)…
– Extract Class Or Extract Subclass: bundle a number of the
variables. Choose variables to go together in the
component that makes sense for each, If the component
makes sense as a subclass, you'll find Extract Subclass
often is easier.
Ref act or ing
Bad smells in code !
9. When refactoring (cont’d)
• Long Method:
“the longer the methods is, the more difficult it is
to understand”.
–Extract method: find parts of the methods that
seem to go nicely together and make a new
method.
Ref act or ing
Bad smells in code !
10. When refactoring (cont’d)
• Comments:
“a comment is a good place to say why you did
something. This kind of information helps future
modifiers”.
– If we need a comment to explain what a block of
code does, try Extract Method. If the method is
already extracted but you still need a comment
to explain what it does, use Rename Method.
– You can use Introduce Assertion.
Ref act or ing
Bad smells in code !
11. When refactoring (cont’d) …
• Parallel Inheritance Hierarchies:
“Every time you make a subclass of one class, you also have to
make a subclass of another. You can recognize this smell
because the prefixes of the class names in one hierarchy are
the same as the prefixes in another hierarchy.”
– The general strategy for eliminating the duplication is to
make sure that instances of one hierarchy refer to instances
of the other. If you use Move Method and Move Field, the
hierarchy
on the referring class disappears.
Ref act or ing
Bad smells in code !
12. Refactoring feature in Visual studio
• Rename …
• Extract Method
• Encapsulate Field
• Extract Interface
• Remove parameter
• Reorder Parameter.
Ref act or ing
14. Ref act or ing
Visual Studio Resource Refactoring Tool
• Support Razor (VBHTML and CSHTML).
• Support ASPX (ASCX, MASTER) .
Code Rush for Visual Studio
Additional Refactoring Tools (With Hyperlinks)
15. Ref act or ing
How refactoring can help our team?
• Decrease effort of handout
• Make group work more easy.
• Less bugs , helps Maintenance and improve
performance.