Topic G - Program Refactoring and Comprehension
Topic G - Program Refactoring and Comprehension
Omar Badreddin 2
What is Refactoring? (cont.)
Omar Badreddin 3
Why Refactor?
It improves design so that maintenance is easier
• Makes source code and models easy
understand
— Models are easier to understand by
designers
— Code is more usable by programmers
• Reduced chaos in long-lived systems
• Faster and easier maintenance in the long
term
Omar Badreddin 4
Opportunities for Refactoring
During initial design and programming
Omar Badreddin 5
Specific Refactorings to be Discussed
in the Following Slides
Many refactorings make use of other simpler refactorings
Omar Badreddin 6
Refactoring: Extract Method
Probably the most used refactoring
• You may have done this many times yourself
Omar Badreddin 7
Inverse Refactoring: Inline method
Omar Badreddin 8
Refactoring: Extract Class
Performed when a you perform object-oriented
analysis and the resulting UML class
diagram suggests two classes where you have
only one.
• E.g. Extract a ‘common superclass’
• Can result from too many Extract Method
refactorings
Omar Badreddin 9
Inverse Refactoring: Inline Class
The opposite of Extract Class
Omar Badreddin 10
Refactoring: Inline Temp
Omar Badreddin 11
Refactoring: Rename Method
Reasons for doing this
• The old name was not clear
• The task of a method has changed
Omar Badreddin 12
Refactoring: Replace Magic Number with
Symbolic Constant
Encapsulates a standard piece of programming
advice
Omar Badreddin 13
Refactoring: Replace Error Code with
Exception
Code can be written assuming it works
• It can therefore be much easier to read
Omar Badreddin 14
Refactoring: Encapsulate Field
Make a field (instance variable) private
• Add a getter, and a setter if necessary
Omar Badreddin 15
Refactoring: Remove Control Flag
Omar Badreddin 16
Refactoring: Split Complex Condition
if ((a || b) && (c || d)) {
…
}
Becomes
if (a || b) {
if (c || d) {
…
}
}
Omar Badreddin 17
Refactoring: Name The Condition
if ((a || b) && (c || d)) {
…
}
Becomes
meaningfulVariable = a || b;
anotherVariable = c || d;
if (meaningfulVariable && anotherVariable) {
…
}
Omar Badreddin 18
General Considerations about Refactoring 1
Do not apply refactorings without thought
for the consequences
• Multi-threaded applications may
experience unexpected effects
• Changing interfaces may affect a
published API
• Some code should not be touched
Omar Badreddin 19
General Considerations about Refactoring 2
Perform good upfront design
• but you can design for ease of
refactoring
Omar Badreddin 20
Program Analysis and Comprehension
Program Analysis
Static Analysis
• Examines the source code
Dynamic Analysis
• Examines the system as it is executing
Omar Badreddin 22
What are we looking for when performing
program analysis?
Depends on our goals and the system
• In almost any language, we can find out
information about variable usage
Omar Badreddin 25
Dynamic Analysis
Omar Badreddin 26
Instrumentation
Omar Badreddin 27
Instrumentation process
Annotation
script Compiler
Instrumented
executable
Omar Badreddin 28
Non-instrumented approach
Omar Badreddin 29
Summary: Static vs. Dynamic Analysis
Omar Badreddin 30
Program Transformation
Omar Badreddin 31
Program transformation application
examples
Converting to a new language dialect
Requirement upgrading
• e.g. using 4 digits for years instead of 2
(Y2K)
Structural improvements
• e.g. changing GOTOs to control structures
Omar Badreddin 32
Simple program transformation
x := (2+5)*3
becomes
x := 2*3 + 5*3
Omar Badreddin 33
Two types of transformations
Translation
• Source and target language are
different
• Semantics remain the same
Rephrasing
• Source and target language are the
same
• Goal is to improve some aspect of the
program such as its understandability
or performance
• Semantics might change
Omar Badreddin 34
Transformation tools
Program-Transformation.org lists 90 of
them
• http://www.program-transformation.org/
Omar Badreddin 35
Program Comprehension
Program comprehension:
• The discipline concerned with studying
the way software engineers understand
programs
37
Program Comprehension Strategies 2
38
Program Comprehension Strategies 3
39
Partial Comprehension
41
Two main levels of reverse engineering
Binary reverse engineering
• Take a binary executable
— Recover source code you can then modify
• Useful for companies that have lost their source
code
• Used extensively by hackers
• Can be used legally, e.g. to enable your system to
interface to existing system
• Illegal in some contexts
Source code reverse engineering
• Take source code
— Recover high level design information
• By far the most widely performed type of reverse
engineering
• Binary reverse engineers also generally do this too
42
Reverse Engineering Objectives 1
43
Reverse Engineering Objectives 2
44
Reverse Engineering Objectives 3
45
Reverse Engineering Objectives 4
Facilitate reuse
• Detect candidate system components
that can be reused
46