SE Code Restructuring
SE Code Restructuring
SE Code Restructuring
Reverse Engineering
Code Restructuring Mainframe
Guest Lecture
Session: Basic Techniques
October 2007 – 3rd Version
presented by
Dipl. Ing. Werner Hoffmann
A member of IEEE and ACM
"Learning is
experience.
Everything else is
information.“
Albert Einstein.
• Introduction
• Review: Structured Programming
• Basic Code Restructuring Methods
• The Spaghetti Bowl Approach
• Indecomposable program figures
• Conclusion
• Introduction
• Review: Structured Programming
• Basic Code Restructuring Methods
• The Spaghetti Bowl Approach
• Indecomposable program figures
• Conclusion
"You do not really
understand something
unless you can explain it to
your grandmother."
Albert Einstein.
• Motivation:
• Software Maintenance
• Structure of a program refers to:
• structure of the code,
• structure of the system.
We only look at the
structure of code.
Note: Before you start a Code Restructuring task, take a short Analysis about program
understanding:
If you find a very bad design, a bad solution (which may address one of the other points
mentioned above),
Date: 21.06.2007
Code Codeyour
Restructuring may not help to solve Restructuring
problem! Page: 8
Introduction (5)
Example:
(Convert Julian date into
Gregorian date)
Bad Design/Solution
Don’t try to
restructure this code.
The problem here is
missing data
structures to solve
the problem!
It’s a problem of
data restructuring,
not a problem of
code restructuring!
Date: 21.06.2007 Code Restructuring Page: 9
Introduction (6)
Example:
(Convert Julian date into Gregorian date)
A better solution:
Previous Research:
• Basic works: Dijkstra – debate over the use of GOTO statement.
• Code and Procedural Restructuring: Böhm and Jacobi – transform
arbitrary flow diagrams into structured flow diagrams.
• Ashcroft and Manna extended this by introducing an algorithm for
transforming arbitrary “goto programs” into equivalent “while
programs”.
• Peterson showed how to transform any program into “well-formed”
program, that only use if, repeat, and multi-level exit statements.
• Yourdon introduced a boolean flag approach to eliminate multi-exit
loops.
• Linger introduced a technique for parsing arbitrary flowgraphs into
their prime components: sequence, if-then-else and while-do.
• There are published several work available for a fully automatic
FORTRAN “structure engine”. Additional several work where
published to restructure COBOL programs.
Don't:
• Try not to refactor based on what you think future requirements will
be.
Refactoring is based on the state of things today.
An Example:
“Spaghetti-Code”,
Unstructured Flowchart.
An Example:
Our goal:
Structured Flowchart,
Structured Pseudo-Code.
Levels of Indentation
1-4
• Introduction
• Review: Structured
Programming
• Basic Code Restructuring Methods
• The Spaghetti Bowl Approach
• Indecomposable program figures
"Great thinkers have
• Conclusion always encountered
opposition from mediocre
minds."
Albert Einstein.
5. Proper Programs
“Intellectuals solve
problems. Geniuses
prevent them.”
Albert Einstein.
p 1
F B
1. DO WHILE Block:
A
T
F
1 p
A A
T DOWHILE (p)
F
A 1 p A
ENDWHILE
F Pseudo-Code:
… pn n
CASE_ENTRY
F CASE (p1) F1
Fotherwise
CASE (p2) F2
…
CASE (pn) Fn
OTHERWISE Fotherwise
END_CASE
Date: 21.06.2007 Code Restructuring Page: 20
Review: Structured Programming (4)
LOOP
- LOOP’s F1
EXITIF (p1)
…
- CASE block’s Fn
ENDLOOP
etc.
Date: 21.06.2007 Code Restructuring Page: 21
Review: Structured Programming (3)
p1
T
F
The goal is to construct
C a well defined hierarchy
for the solution.
• Introduction
• Review: Structured Programming
• Basic Code Restructuring
Methods
• The Spaghetti Bowl Approach
• Indecomposable program figures
"I have no special talents. I
• Conclusion am only passionately
curious."
Albert Einstein.
Interchange
another way to
(2)
teach; it's the only
way.”
Albert Einstein.
1) Interchange - Example:
T F
p
T F
p
A B
A B
1
1
Interchange
1/2
2
2
T T
q C q C
F F
2) Transposition:
A
1 A = 1
1 = 1 A
2) Transposition (continued) :
A
T T
A p = p
F F
A
A
T T
p = A p
F F
A
2) Transposition - Example:
T F
p
T F
p B
A
D
A B
1
1
Interchange Transposition
1/2 D
2
2
T D
q C T
q C
F
F
3) Combination:
A B = A,B
F1 F2 F3 = F3,F2,F
1
3) Combination - Example:
T F T F
Combinati
p p
T F
p B
on
A A B,D
D
A B
Transposition
1 1
1
Interchange
Combination
1/2 D
2 2
2
T D C,D
q C T T
q C q
F
F F
Step 1: Step 2:
Structured Code:
Unstructured Code
IF_THEN_ELSE, DO_WHILE
Date: 21.06.2007 Code Restructuring Page: 30
Resolution (1)
4) Resolution:
a c a 1 A 2
1 A 2 m =
b b 4 A 3 m
4) Resolution - Example:
T F
1 2 p
T F B
p A
2
A 1
B
D D
1,2
1 D 2 2 1
T T T
q C q C q C
F F F
3
5) Substitution:
A
T
F
1 p = DoW (p) A
Means: DO_WHILE
F
T
1 A q = Rep A U(q)
Means:
Entry REPEAT_UNTIL
A
“proper program” = PGM x
x
Exit
5) Substitution - Example:
T F
p
A B,D
1 SPGM S1
Substitution
S1
2
C,D
T
q
Structured Code
Date: 21.06.2007 Code Restructuring Page: 34
Inverse Notation (1)
6) Inverse Notation:
F T
p = ¬p
T F
A A
F T
T F
1 p = 1 ¬p
Means: DO_WHILE
T F
F T
1 A q = 1 A ¬q
Means: REPEAT_UNTIL
Date: 21.06.2007 Code Restructuring Page: 35
Agenda
• Introduction
• Review: Structured Programming
• Basic Code Restructuring Methods
• The Spaghetti Bowl Approach
• Indecomposable program figures
• Conclusion
"Today's problems
cannot be solved
with the level of
thinking that
created them.”
Albert Einstein.
Date: 21.06.2007 Code Restructuring Page: 36
The Spaghetti Bowl Approach (1)
Method:
Draw a flowchart of the unstructured program logic. B 2 C
A 1 q
T
F
output line
T T
Special action is needed (see next section) . 1 p B q
F
Check and Cleanup flowchart. 4) F
B 2 C
T
Input line
p D
F T output line
A 1 q
F
B 2 C
T
Input line
p D
F T output line
A 1 q
F
SB
B 2 C
T
Input line
p D
F T output line
A 1 q
F
SB
B 2 C
D
T
T 1 q
Input line F
p
output line
F 2 C
3
D
T
A 1 q
F
B 2 C
D
T
T 1 q
SB1 F
Input line
p
output line
F 2 C
SB2 3
D
T
A 1 q
F
IF_THEN_ELSE
Date: 21.06.2007 Code Restructuring Page: 42
The Spaghetti Bowl Approach (8)
T
B 2 D q
T F
Input line
p
output line
F 2 C
SB2 3
D
T
A 1 q
F
T
B 2 D q
T F
Input line
p
output line
F
SB2 3
C,D
T
A 1 q
F
Input line
p
output line
F
SB2 3
C,D
T
A 1 q
F
Input line
p
output line
F
SB2 3
C,D
T
A 1 q
F
Input line
p
output line
F
SB2 3
C,D
T
A 1 q
F
Input line
p
output line
F
SB2 3
C,D
T
A 1 q
F
SB1
Input line
p
output line
F
SB2 3
C,D
T
A 1 q
F
SB1
Input line
p
output line
F
SB2 3
C,D
T
A 1 q
F
C,D
T
A 1 q
F
T
B,D 2 q
T F
SB1
Input line
p
output line
F
SB2 3
C,D
T
A 1 q
F
T
B,D 2 q
T F
block DO_WHILE
Input line
p
output line
F
SB2 3
C,D
T
A 1 q
F
T
B,D 2 q
T F
block DO_WHILE
Input line
p
output line
F
SB2 3
C,D
T
A 1 q
F
T
B,D 2 q
T F
block DO_WHILE
Input line
p
output line
F
SB2 3
Rule 2 : There is a node with
2 input lines, we assume a
C,D
DO_WHILE construct.
T
A 1 q
F
T
B,D 2 q
T F
block DO_WHILE
Input line
p
output line
F
SB2 3
C,D
T
A 1 q
F
block DO_WHILE
Date: 21.06.2007 Code Restructuring Page: 56
The Spaghetti Bowl Approach (20)
T
B,D 2 q
T F
block DO_WHILE
Input line
p
Same Code, we can use the
transportation method to
F simplify the control structure.
3
output line
C,D
T
A 1 q
F
block DO_WHILE
Date: 21.06.2007 Code Restructuring Page: 57
The Spaghetti Bowl Approach (21)
F
A
IF_THEN_ELSE DO_WHILE
Pseudo-Code: IF p
THEN {B,D}
ELSE A
ENDIF
DOWHILE q
{C,D}
ENDWHILE
• Introduction
• Review: Structured Programming
• Basic Code Restructuring Methods
• The Spaghetti Bowl Approach
• Indecomposable program
figures
• Conclusion "The secret to creativity
is to know how to hide
your sources.”
Albert Einstein.
Terms:
T T
1 p B q
F
F
Pseudo-Code: TRUE
REPEAT
POP
IF p
THEN {A,TRUE}
ELSE {B, IF q
THEN TRUE
ELSE FALSE
ENDIF}
ENDIF
UNTIL TOP
Date: 21.06.2007 Code Restructuring Page: 63
Agenda
• Introduction
• Review: Structured Programming
• Basic Code Restructuring Methods
• The Spaghetti Bowl Approach
• Indecomposable program figures
• Conclusion
"Everybody is a genius. But
if you judge a fish by its
ability to climb a tree, it will
spend its whole life thinking
its stupid."
Albert Einstein.
Date: 21.06.2007 Code Restructuring Page: 64
Conclusion (1)
Benefits:
• We can identify:
PROGRAMMING AS A PROCESS:
“Of course! Why
didn't I ever think of
• A Revolution in Programming? that?”
D.E. Knuth.
Closing remarks:
• After this session you should know how to
restructure a unstructured flowchart into a structured
form by using a manually procedure which includes 6
basic transformation methods and 3 additional rules
to deal with more complex programs.
• The resulting flowchart /pseudo-code can be used to
rebuild the source code into a structured format.
…! ? GOTO…
Web page
http://www.refactoring.com/
http://istpub.berkeley.edu:4201/bcc/Fall2004/refactoring.html
Discussion site on code smells: http://c2.com/cgi/wiki?CodeSmell
Books:
• Software Productivity, Harlan D. Mills, 1988
• General Principles of System Design, Gerald M. Weinberg & Daniela
Weinberg
• Rethinking System Analysis & Design, Gerald M. Weinberg
• Program Development by stepwise transformation, F.L. Bauer, Springer
Berlin
• Refactoring: Improving the Design of Existing Code, by Martin Fowler (et
al.),1999, Addison-Wesley
• Additional publications:
• Using Hammock Graphs to Structure Programs, Fubo Zhang and Erik H.
D’Hollander, Member, IEEE
• An Algorithm for Structuring Flow graphs, Brenda S. Baker
• Restructuring Control Flow of IBM/370 Assembler Programs, Michael
Ricking
• A Non-Deterministic Approach to Restructuring Flow Graphs, Toni A.
Bünter, TR CUCS-019-93 Columbia University
• Code Cleanup Idioms, Ninad
Date: 21.06.2007 Jog
Code Restructuring Page: 70
• Aggressive Function Inlining with Global Code Reordering, IBM, H-0247,
Discussion (1)
Q/A:
Not just prepared…
-You’ll get a final paper until next
session.
Requests:
1) Exercises – ok. I prepare some examples
…
Code Restructuring
I tha
n
for y k you
o
atte ur
ntio
n!