Last Time – This time
! Program refactoring ! Design patterns basics
Advanced Software Engineering Topics
Design Patterns - 1
Dr John Kanyaru 1 2
Refactoring
Program Refactoring Refactoring
Program transformation that preserves semantics
Improves software qualities
Understandability, extensibility, reusability
Introduction Why is refactoring important?
Keep up with software evolution
It may be necessary to improve design to support further
functionality
Dr JM Kanyaru 3 4
, Simple Refactoring So What?
for ( int radius=1, radius < 10; radius++ ) {
area = 3.1416 * radius * radius; What was gained?
System.out.printf(“area radius %d= %f“,radius,area);
} Understandability
By looking at the code you can tell better what the
computation is doing
for ( int radius=1,radius< 10; radius++ )
area = computeArea(radius); Anything lost?
System.out.printf(“area radius %d= %f“,radius,area);
}
May be some performance
double computeArea(int radius) { Short loop and compilers are smart (inlining)
return 3.1416 * radius * radius;
}
5 6
Another Simple Refactoring So What? (Again)
double computeArea(int radius) {
return 3.1416 * radius * radius;
What was gained?
} Understandability
By looking at the code you can tell better what the
computation is doing
Performance
static final double PI = 3.1416; Maybe – possible compiler optimization, specially if
double computeArea(int radius) { variable is used in several places
return PI * radius * radius;
} Anything lost?
…
7 8
, Insights (1) Insights (2)
Refactorings are relatively small changes
A refactoring focus on one change at a time To apply a refactoring certain conditions should hold
Several refactorings can be applied to a program incrementally Example: There should be a magic number in a
computation
program program’ program’’
Each refactoring is an algorithm that consists of
several steps
refactoring refactoring
Example:
Declare a static final field with a meaningful name
Initialize the field with the magic number
Substitute the magic number in the original computation with
the new field
9 10
Insights – Recap Why Refactoring?
Refactorings are relatively small changes Improve design of software
A refactoring focus on one change at a time
Improve understandability
Several refactorings can be applied to a program
incrementally
Bug finding
To apply a refactoring certain conditions should hold
Each refactoring is an algorithm that consists of Improve programming productivity
several steps Code faster – because all of the above
11 12
The benefits of buying summaries with Stuvia:
Guaranteed quality through customer reviews
Stuvia customers have reviewed more than 700,000 summaries. This how you know that you are buying the best documents.
Quick and easy check-out
You can quickly pay through credit card for the summaries. There is no membership needed.
Focus on what matters
Your fellow students write the study notes themselves, which is why the documents are always reliable and up-to-date. This ensures you quickly get to the core!
Frequently asked questions
What do I get when I buy this document?
You get a PDF, available immediately after your purchase. The purchased document is accessible anytime, anywhere and indefinitely through your profile.
Satisfaction guarantee: how does it work?
Our satisfaction guarantee ensures that you always find a study document that suits you well. You fill out a form, and our customer service team takes care of the rest.
Who am I buying these notes from?
Stuvia is a marketplace, so you are not buying this document from us, but from seller Nawoda. Stuvia facilitates payment to the seller.
Will I be stuck with a subscription?
No, you only buy these notes for £6.50. You're not tied to anything after your purchase.