Refactoring: Improving Software Design and Code Readability, Slides of Object Oriented Programming

The concept of refactoring in software development, explaining its purpose and benefits. It provides a practical example of refactoring a method to improve its clarity and maintainability. The document also lists common refactoring techniques and the support provided by eclipse for refactoring operations. It concludes with an exercise that encourages readers to identify problems in code and apply refactoring principles.

Typology: Slides

2023/2024

Uploaded on 10/23/2024

gauri-verma-3
gauri-verma-3 🇺🇸

1 document

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Refactoring » What is Refactoring?
Refactoring is the process of changing a
software system so that
the external behavior is not altered, but
the internal structure is improved.
Refactoring (http://www.refactoring.com/) is
a “behavior-preserving transformation.”
Small changes per transformation
less likely to go wrong
System works after each change
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Refactoring: Improving Software Design and Code Readability and more Slides Object Oriented Programming in PDF only on Docsity!

Refactoring » What is Refactoring?

Refactoring is the process of changing a

software system so that

the external behavior is not altered, but

the internal structure is improved.

Refactoring (http://www.refactoring.com/) is

a “behavior-preserving transformation.”

Small changes per transformation

 less likely to go wrong

System works after each change

Refactoring » Why Refactoring?

Methods might no longer do (only) what their

name suggests.

Functionality that should be in two different

classes might be in the same class.

Functionality that should be in one class

might be duplicated in two or more classes.

Improve the design of existing code.

Gain a better understanding of the code.

Refactoring » Example

 (^) Of course, to print out a bill for a customer, we also need to include incidentals and taxes … public double getRoomCharge() { double roomCharge = 0.0; ... code to compute room charge... // now add the incidentals to roomCharge ... code to add up incidentals ... // now add the tax for the room to the charge ...several lines of code to compute the tax... return roomCharge; }  (^) What’s inelegant about this method now?  (^) 3 sets of calculations in one function. Method does 3 things.  (^) The name is not illustrative of what the method does.

Refactoring » Example

Better: Changing the name of the method (for

example, calculateCustomerCharge ).

What else do we need to do to solve the problem?

We also need to change the name at all call sites.

We need to update the documentation.

If this method overrides a method in another class, the

other name may need to be changed too. Ditto if this

method implements an interface.

This is known as the Rename Method refactoring.

Refactoring » Common Refactorings

Rename

Methods , Fields , Packages , Projects , Parameters , or

Local Variables

Encapsulate Field (generate getter and setter)

Pull up a Field or Method (into superclass)

Push down a Field or Method (into subclass)

Extract Method , Local Variable , or Constant from

an Expression

Change Method Signature

Refactorings Supported by Eclipse

 (^) Renaming  (^) a package  (^) a compilation unit  (^) a type  (^) a local variable  (^) method parameters  (^) Extracting  (^) a constant  (^) an interface from a type  (^) Inlining  (^) a local variable  (^) a method  (^) a constant  (^) static members between types  (^) an instance method to a component  (^) Converting  (^) a local variable to a field  (^) an anonymous inner class to a nested class  (^) a nested type to a top-level type  (^) Replacing  (^) references to a type with references to one of its supertypes  (^) a single reference to a type with a reference to one of its supertypes  (^) an expression with a method parameter  (^) constructor calls with factory method invocations