Refactoring!
With a side of RDi
Charles Guarino
Central Park Data Systems, Inc.
@charlieguarino
0
About The Speaker
Charles Guarino believes in the “power” of IBM Power Systems. His career
reflects his dedication and interest in bringing the platform and its solutions to
others and as a result has been recognized as an IBM Champion.
He is a member of COMMON’s Speaker Excellence Hall of Fame and a proud
recipient of the Al Barsa Memorial Scholarship Award. Additionally, he participates
on COMMON’s Strategic Education Team. Other professional endeavors have
included the roles of President and monthly Q&A host of the Long Island System
User’s Group LISUG ([Link])
Today, along with the team at Central Park Data Systems, he is serving
individuals and companies on a worldwide basis though his consulting work and
award-winning speaking engagements.
Charles is a true people person and can often be found at conferences sharing
his expertise on RDi and other IBM i topics.
Charles can be reached at cguarino@[Link].
LinkedIn - [Link]
Twitter - @charlieguarino
Copyright Central Park Data Systems Inc 1
What We’ll Cover …
• Terminology
• Why refactoring?
• What is the end goal?
• Decision time
• Getting started
• Refactoring using RDi
• Other ways RDi can be useful
• Change management
• Wrap up
Refactoring
Code refactoring is the process of restructuring existing computer code—
changing the factoring—without changing its external behavior. Refactoring is
intended to improve nonfunctional attributes of the software.
[Link]
Copyright Central Park Data Systems Inc 2
Technical Debt
Technical debt (also known as design debt or code
debt) is a concept in software development that reflects the
implied cost of additional rework caused by choosing an easy
(limited) solution now instead of using a better approach that
would take longer.
[Link]
Code Smell
A code smell is any characteristic in the source code of
a program that possibly indicates a deeper problem.
Determining what is and is not a code smell is subjective, and varies
by language, developer, and development methodology.
[Link]
Copyright Central Park Data Systems Inc 3
Code Rot
As code is continually modified and maintained over
time with possibly imperfect changes, more and more
bugs get introduced.
Worse, it gets more and more “correct but ugly.”
This reduces the integrity of the code, “rotting” it until it
eventually falls apart.
[Link]
estions/255866/what-is-meant-by-code-rot
What We’ll Cover …
• Terminology
• Why refactoring?
• What is the end goal?
• Decision time
• Getting started
• Refactoring using RDi
• Other ways RDi can be useful
• Change management
• Wrap up
Copyright Central Park Data Systems Inc 4
Bug fixing is NOT the main reason for program maintenance
Enhancements in functionality is the bulk of the cost
Copyright Central Park Data Systems Inc 5
New functions and processes take too long to deploy
10
Software is an expensive asset to maintain
11
Copyright Central Park Data Systems Inc 6
Business requirements are always changing
12
Take advantage of new advancements in software and hardware
13
Copyright Central Park Data Systems Inc 7
What We’ll Cover …
• Terminology
• Why refactoring?
• What is the end goal?
• Decision time
• Getting started
• Refactoring using RDi
• Other ways RDi can be useful
• Change management
• Wrap up
14
Roadmap to a “perfect” program
15
Copyright Central Park Data Systems Inc 8
Modularity
16
Less global variables
17
Copyright Central Park Data Systems Inc 9
No magic numbers and hard coded values
18
No GOTO statements
GOTO
19
Copyright Central Park Data Systems Inc 10
Well documented
20
What We’ll Cover …
• Terminology
• Why refactoring?
• What is the end goal?
• Decision time
• Getting started
• Refactoring using RDi
• Other ways RDi can be useful
• Change management
• Wrap up
21
Copyright Central Park Data Systems Inc 11
When to refactor
“The best time to consider refactoring is before adding any
updates or new features to existing code.
Going back and cleaning up the current code before adding
in new programming will not only improve the quality of the
product itself, it will make it easier for future developers to
build on the original code.”
[Link]
when-and-when-not-to-do-it/
22
When **NOT** to refactor
If you are trying to get a product to
market within a set time frame.
Refactoring can be like going down the
proverbial rabbit hole: Once you start,
it can become quite time-consuming
[Link]
refactoring-best-practices-when-and-when-not-to-do-it/
23
Copyright Central Park Data Systems Inc 12
Rewrite vs refactor – the controversy continues….
[Link]
24
Rewrite vs refactor – the controversy continues….
[Link]
know-2hi4
25
Copyright Central Park Data Systems Inc 13
Does the developer have the right skills to rewrite or refactor?
26
Does faster hardware make up for poorly written code?
27
Copyright Central Park Data Systems Inc 14
Where is the sweet spot?
28
What We’ll Cover …
• Terminology
• Why refactoring?
• What is the end goal?
• Decision time
• Getting started
• Refactoring using RDi
• Other ways RDi can be useful
• Change management
• Wrap up
29
Copyright Central Park Data Systems Inc 15
Why “refactor” DDS? Benefits of DDL over DDS
• DDS is IBM i specific, SQL is universal
• SQL and DDL are widely taught, DDS is not
• DDS is stabilized, last enhancement was in V5R3
• DDL supports CLOB, DBCLOB, BLOB, XML, ROWID
• Identity columns
• Longer column names support, up to 128 characters
See new ALIAS keyword for RPG!
• And other performance and I/O benefits
32
Fun Fact! Alias keyword allows you to read long field names
33
Copyright Central Park Data Systems Inc 16
Alias keyword is placed on the F spec or dcl-f statement
34
Database normalization
1NF
• Each table cell should contain a single value
• Each record needs to be unique
2NF
• Must be in 1NF
• Single column primary key
3NF
Must be in 2NF
Have no transitive functional dependencies
35
Copyright Central Park Data Systems Inc 17
A quick way to list all members in a source physical file
[Link]
file-with-sql
36
A quick way to list all object information in a library
[Link]
37
Copyright Central Park Data Systems Inc 18
How do I start – some ideas
RPG FREE
38
Sample source code changes
Eliminate record level access where appropriate
using set processing with SQL
39
Copyright Central Park Data Systems Inc 19
Some classic code smells
Duplicated routines
Contrived complexity – forced usage of complicated
design patterns where simpler would have sufficed
Too many parameters
Excessively long or short variable names
Excessively long line of code with many operators
40
Using intermediate variables makes the code easier to read and debug
Instead of this:
If hours <= 40;
wages = hours * hourlyrate;
else;
wages = (hourlyrate * 40) + (hourlyrate * 1.5) * (hours – 40);
endif;
Consider this:
If hours <= 40;
Wages = hours * hourlyrate;
else;
overtimerate = hourlyrate * 1.5;
overtimehours = hours – 40;
wages = (hourlyrate * 40) + (overtimerate * overtimehours);
endif;
45
Copyright Central Park Data Systems Inc 20
Some code changes for clarifying procedure calls
[Link]
46
What We’ll Cover …
• Terminology
• Why refactoring?
• What is the end goal?
• Decision time
• Getting started
• Refactoring using RDi
• Other ways RDi can be useful
• Change management
• Wrap up
47
Copyright Central Park Data Systems Inc 21
Examples of Field Renaming
48
Examples of Extract Constant – Character value
49
Copyright Central Park Data Systems Inc 22
Examples of Extract Constant – Numeric value
50
Extract Procedure
This can be the start of something BIG…
Section of
Mainline code
Refactor
Extract Procedure
Copy local procedure
into separate module
Create service
program!!!
51
Copyright Central Park Data Systems Inc 23
Example 1:
Housekeeping section should not be part of mainline code
53
Example 2:
A process that can be reused in the same program
54
Copyright Central Park Data Systems Inc 24
What We’ll Cover …
• Terminology
• Why refactoring?
• What is the end goal?
• Decision time
• Getting started
• Refactoring using RDi
• Other ways RDi can be useful
• Change management
• Wrap up
57
Use iSphere tagging to store refactoring reminders
58
Copyright Central Park Data Systems Inc 25
ACS Visual Explain
59
ACS Index Advisor
60
Copyright Central Park Data Systems Inc 26
RDi Code Coverage can help identify dead code
61
Code coverage report
Can also compare and merge reports!
62
Copyright Central Park Data Systems Inc 27
Code coverage report
63
Free videos on how to use Code Coverage
[Link]
64
Copyright Central Park Data Systems Inc 28
Code Coverage command
65
Simple idea for using Code Coverage
Call Program ABC
End with error?
Y
Call Program
N Rollback
If necessary ABC again using
CODE COVERAGE
Continue Processing
66
Copyright Central Park Data Systems Inc 29
What We’ll Cover …
• Terminology
• Why refactoring?
• What is the end goal?
• Decision time
• Getting started
• Refactoring using RDi
• Other ways RDi can be useful
• Change management
• Wrap up
69
e-GIT Perspective
70
Copyright Central Park Data Systems Inc 30
Stage the file change
71
Commit and Push
*** Because master repository was created as “—bare”
a *NEW* master branch will be created automatically
72
Copyright Central Park Data Systems Inc 31
What We’ll Cover …
• Terminology
• Why refactoring?
• What is the end goal?
• Decision time
• Getting started
• Refactoring using RDi
• Other ways RDi can be useful
• Change management
• Wrap up
73
Code Refactoring!
With a side of RDi
Charles Guarino
THANK YOU!!!
@charlieguarino
74
Copyright Central Park Data Systems Inc 32