few minutes to better code - refactoring

16
Refactorin g Few minutes To better Code … Prepared By: Diaa Alsa Feb 2015

Upload: diaa-al-salehi

Post on 08-Aug-2015

39 views

Category:

Software


1 download

TRANSCRIPT

Refactoring

Few minutes To better Code …

Prepared By: Diaa AlsalehiFeb 2015

Refactoring

– What is refactoring?

– Why refactoring?

– When refactoring?

– Refactoring Feature in Visual Studio

Refactoring

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.

Refactoring

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 Refactoring

Why refactoring (cont’d)

• Refactoring helps you program faster

– less bugs and helps correct bugs:

errors need to be modified only in one

place

Refactoring

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.Refactoring

Bad smells in code !

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.”

Refactoring

Bad smells in code !

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.

Refactoring

Bad smells in code !

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.Refactoring

Bad smells in code !

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.Refactoring

Bad smells in code !

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.

Refactoring

Bad smells in code !

Refactoring feature in Visual studio

• Rename … • Extract Method• Encapsulate Field• Extract Interface• Remove parameter• Reorder Parameter.

Refactoring

Let’s Start

Refactoring

Refactoring

Visual Studio Resource Refactoring Tool• Support Razor (VBHTML and CSHTML).• Support ASPX (ASCX, MASTER) .

Code Rush for Visual Studio

Additional Refactoring Tools (With Hyperlinks)

Refactoring

How refactoring can help our team?

• Decrease effort of handout

• Make group work more easy.

• Less bugs , helps Maintenance and improve performance.

Thank You