Docsity

Refactoring Patterns in Software Engineering: Lecture 11, Slides of Software Engineering

Partial preview of the text

Download Refactoring Patterns in Software Engineering: Lecture 11 and more Slides Software Engineering in PDF only on Docsity!

Patterns in Software Engineering Lecturer: Raman Ramsin^ Lecture 11^ Refactoring Patterns

Part 2

Sharif University of Technology

Part

Patterns in Software Engineering โ€“ Lecture 11 Organizing Data: Self Encapsulate Field Organizing Data: Self Encapsulate Field ย„ Self Encapsulate Field

You are accessing a field directly, but the coupling to the field is becomingawkward.

Create getting and setting methods for the field and use only those to accessthe field.

Sharif University of Technology

Patterns in Software Engineering โ€“ Lecture 11 Organizing Data: Replace Array with Object Organizing Data: Replace Array with Object ย„ Replace Array with Object ย„ 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.

Sharif University of Technology

Patterns in Software Engineering โ€“ Lecture 11 Organizing Data: Duplicate Observed Data Organizing Data: Duplicate Observed Data ย„ Duplicate Observed Data

You have domain data available only in a GUI control, and domain methods need

You have domain data available only in a GUI control, and domain methods need access.

Copy the data to a domain object. Set up an observer to synchronize the two piecesof data.

Sharif University of Technology

Patterns in Software Engineering โ€“ Lecture 11 Organizing Data: Encapsulate Collection Organizing Data: Encapsulate Collection ย„ Encapsulate Collection

A method returns a collection.

Make it return a read-only view and provide add/remove methods

Make it return a read-only view and provide add/remove methods.

Sharif University of Technology

Patterns in Software Engineering โ€“ Lecture 11 Organizing Data: Replace Subclass with Fields Organizing Data: Replace Subclass with Fields ย„ Replace Subclass with Fields

You have subclasses that vary only in methods that return constant data

You have subclasses that vary only in methods that return constant data.

Change the methods to superclass fields and eliminate the subclasses.

Sharif University of Technology

Patterns in Software Engineering โ€“ Lecture 11 Simplifying Conditional Expressions: Consolidate Conditional Expression p y g p p ย„ Consolidate Conditional Expression ย„ Consolidate Conditional Expression

You have a sequence of conditional tests with the same result.

Combine them into a single conditional expression and extract it.

Sharif University of Technology

Patterns in Software Engineering โ€“ Lecture 11 Simplifying Conditional Expressions:

Replace Nested Conditional with Guards

p y g p

p

ย„ Replace Nested Conditional with Guard Clauses

A method has conditional behavior that does not make clear the normal path of p execution.

Use guard clauses for all the special cases.

Sharif University of Technology

Patterns in Software Engineering โ€“ Lecture 11 Simplifying Conditional Expressions: Introduce Null Object p y g p j I d N ll Obj ย„ I ntroduce Null Object

You have repeated checks for a null value.

Replace the null value with a null object. p j

Sharif University of Technology

Patterns in Software Engineering โ€“ Lecture 11 Making Method Calls Simpler: Separate Query from Modifier g p p Q y ย„ Separate Query from Modifier

You have a method that returns a value but also changes the state of an object.

Create two methods one for the query and one for the modification

Create two methods, one for the query and one for the modification.

Sharif University of Technology

Patterns in Software Engineering โ€“ Lecture 11 Making Method Calls Simpler:

Replace Parameter with Explicit Methods

g p

p

p

Replace Parameter with Explicit Methods

You have a method that runs different code depending on the values of an p g enumerated parameter.

Create a separate method for each value of the parameter.

Sharif University of Technology

Patterns in Software Engineering โ€“ Lecture 11 Making Method Calls Simpler: Preserve Whole Object g p j ย„ Preserve Whole Object ย„ Preserve Whole Object

You are getting several values from an object and passing these values asparameters in a method call.

S d th h l bj t i t d

Send the whole object instead.

Sharif University of Technology

Patterns in Software Engineering โ€“ Lecture 11 Making Method Calls Simpler: Introduce Parameter Object g p j ย„ Introduce Parameter Object ย„ Introduce Parameter Object

You have a group of parameters that naturally go together.

Replace them with an object.

Sharif University of Technology

Patterns in Software Engineering โ€“ Lecture 11 R fR eference ย„ Fowler, M., Refactoring: Improving the Design of Existing Code, Addison-Wesley, 1999.

Sharif University of Technology