We all have a burning desire to write clean code. Every morning we wake up, look in the mirror, and promise ourselves that today we will follow the principles and best practices learned from Uncle Bob and his disciples. But we live in a cruel environment, surrounded by millions of smelly lines of code, reflections of a stinky design… and these constantly challenge our pure-hearted desire for writing clean code.
In such an environment, the stubbornness to practice daily the writing of clean code is vital.
But is it enough? Can we avoid getting lost in a sea of smelly code and design?
In this talk I will try to persuade you that, in dealing with large-scale systems, craftsmanship must be supported by proper techniques and tools that can help us to quickly understand, assess and improve the sea of smelly design that surrounds us.
I will present a pragmatic approach on how design anti-patterns (e.g. God Class, Feature Envy, Refused Bequest, Shotgun Surgery) can be automatically detected using a set of metrics-based detection rules, by analyzing the history of the system, and by using intriguing software visualizations.
The presentation will also include a live demo of tools that can automate the entire approach to a high-extent. These tools are so robust that they can deal with systems of several million lines of code; but they are also friendly enough to provide you with customized hints that help you deal with each and every case of an “unclean” code.
11. Michele Lanza
Radu Marinescu
Object-Oriented
Metrics
in Practice
Using Software Metrics to
Characterize, Evaluate, and Improve
the Design of Object-Oriented Systems
Foreword by Stéphane Ducasse
Lanza·Marinescu
Object-Oriented
MetricsinPractice
tant Professor at the University of Lugano, Switzer-
rests lie in software (re)engineering and software evo-
n software visualization and metrics. He is the creator
ailable language-independent software visualization
e Ernst Denert Software Engineering Award in 2003.
istant Professor at the Politehnica University of
earch focus is on object-oriented design, reenginee-
He is the author of a suite of novel object-oriented
or of iPlasma, an integrated and freely available tool-
. Several of his published research ideas have already
own “Borland Together Control Center” CASE tool.
ented Metrics in
very engineering discipline. However, due to its lack
mplexity, software engineering is not considered a
y. Moreover, defining, understanding and applying
s like an overly complex activity, recommended only
general, if a software system is delivering the
few people – if any – care about measuring the qua-
Consequently, software metrics are still regarded
st software developers.
stify the design metrics used to assess the size,
bject-oriented software systems. Based on a novel
ally accepted semantics for metrics and by statistical
ustrial projects, they deduce a suite of metrics-based
esign of object-oriented software systems. They
fy design disharmonies in code, and how to devise
cally sound results and practically tested procedu-
es this book an ideal companion for professional
pers and quality engineers. The pattern-oriented des-
ers easy access to detecting shortcomings and
oblems.
ect-oriented
*many reengineering strategies
for poorly structured code
*brief introduction to software
visualization
‘’This well-written book is an impor-
tant piece of work that takes the
seemingly forgotten art of object-
oriented metrics to the next level in
terms of relevance and usefulness.’
Richard C. Gronback,
Chief Scientist, Borland Software
Corporation
1 3
1
12. Michele Lanza
Radu Marinescu
Object-Oriented
Metrics
in Practice
Using Software Metrics to
Characterize, Evaluate, and Improve
the Design of Object-Oriented Systems
Foreword by Stéphane Ducasse
Lanza·Marinescu
Object-Oriented
MetricsinPractice
tant Professor at the University of Lugano, Switzer-
rests lie in software (re)engineering and software evo-
n software visualization and metrics. He is the creator
ailable language-independent software visualization
e Ernst Denert Software Engineering Award in 2003.
istant Professor at the Politehnica University of
earch focus is on object-oriented design, reenginee-
He is the author of a suite of novel object-oriented
or of iPlasma, an integrated and freely available tool-
. Several of his published research ideas have already
own “Borland Together Control Center” CASE tool.
ented Metrics in
very engineering discipline. However, due to its lack
mplexity, software engineering is not considered a
y. Moreover, defining, understanding and applying
s like an overly complex activity, recommended only
general, if a software system is delivering the
few people – if any – care about measuring the qua-
Consequently, software metrics are still regarded
st software developers.
stify the design metrics used to assess the size,
bject-oriented software systems. Based on a novel
ally accepted semantics for metrics and by statistical
ustrial projects, they deduce a suite of metrics-based
esign of object-oriented software systems. They
fy design disharmonies in code, and how to devise
cally sound results and practically tested procedu-
es this book an ideal companion for professional
pers and quality engineers. The pattern-oriented des-
ers easy access to detecting shortcomings and
oblems.
ect-oriented
*many reengineering strategies
for poorly structured code
*brief introduction to software
visualization
‘’This well-written book is an impor-
tant piece of work that takes the
seemingly forgotten art of object-
oriented metrics to the next level in
terms of relevance and usefulness.’
Richard C. Gronback,
Chief Scientist, Borland Software
Corporation
1 3
1
1000+reprinted2010
25. Dr. Radu MarinescuDr. Adrian Trifu Dr. Mircea Trifu George Ganea Ioana Verebi
...uniquely innovative tools, and project-specific consultancy
to support complex quality assessment tasks
in large-scale software systems
65. Lorenz, Kidd, 1994
Chidamber, Kemerer, 1994
LOC - number of lines of code
CYCLO - cyclomatic complexity of a function
NOF - number of functions
FANOUT - outgoing coupling
66. NOA - number of attributes
DIT - depth of inheritance tree
TCC - tight class cohesion
Lorenz, Kidd, 1994
Chidamber, Kemerer, 1994
LOC - number of lines of code
CYCLO - cyclomatic complexity of a function
NOF - number of functions
FANOUT - outgoing coupling
107. Foutse et. al. - An exploratory study of the impact of anti-patterns on class change- and fault-proneness, 2012
108. Foutse et. al. - An exploratory study of the impact of anti-patterns on class change- and fault-proneness, 2012
Classes participating in design problems are
significantly more likely to be subject to changes and
to be involved in fault-fixing changes (bugs)
“
”
121. God Classes
are complex,
are not cohesive,
access external data.
Compose metrics into queries using
logical operators
122. Detection Strategies are metric-based queries to
detect design flaws.
METRIC 1 > Threshold 1
Rule 1
METRIC 2 < Threshold 2
Rule 2
AND Quality problem
Marinescu
123. A God Class centralizes too much intelligence in
the system.
ATFD > FEW
Class uses directly more than a
few attributes of other classes
WMC ≥ VERY HIGH
Functional complexity of the
class is very high
TCC < ONE THIRD
Class cohesion is low
AND GodClass
Marinescu
124. Envious Methods are more interested in data from
a handful of classes.
ATFD > FEW
Method uses directly more than
a few attributes of other classes
LAA < ONE THIRD
Method uses far more attributes
of other classes than its own
FDP ≤ FEW
The used "foreign" attributes
belong to very few other classes
AND Feature Envy
Marinescu, Lanza
125. Data Classes are dumb data holders.
WOC < ONE THIRD
Interface of class reveals data
rather than offering services
AND Data Class
Class reveals many attributes and is
not complex
Lanza, Marinescu 2006
126. Data Classes are dumb data holders.
AND
OR
Class reveals many
attributes and is not
complex
NOAP + NOAM > FEW
More than a few public
data
WMC < HIGH
Complexity of class is not
high
NOAP + NOAM > MANY
Class has many public
data
WMC < VERY HIGH
Complexity of class is not
very high
AND
Lanza, Marinescu 2006
145. public class TarHeader{
/**
* The entry's name.
*/
public StringBuffer name;
/**
* The entry's permission mode.
*/
public int mode;
/**
* The entry's user id.
*/
public int userId;
/**
* The entry's group id.
*/
public int groupId;
}
146. public class TarHeader{
/**
* The entry's name.
*/
StringBuffer name;
/**
* The entry's permission mode.
*/
int mode;
/**
* The entry's user id.
*/
int userId;
/**
* The entry's group id.
*/
int groupId;
}
public
public
public
public
147. public class TarHeader{
/**
* The entry's name.
*/
StringBuffer name;
/**
* The entry's permission mode.
*/
int mode;
/**
* The entry's user id.
*/
int userId;
/**
* The entry's group id.
*/
int groupId;
}
public
public
public
public
DATA
CLASS
148. public class TarHeader{
/**
* The entry's name.
*/
StringBuffer name;
/**
* The entry's permission mode.
*/
int mode;
/**
* The entry's user id.
*/
int userId;
/**
* The entry's group id.
*/
int groupId;
}
149. public class TarHeader{
/**
* The entry's name.
*/
StringBuffer name;
/**
* The entry's permission mode.
*/
int mode;
/**
* The entry's user id.
*/
int userId;
/**
* The entry's group id.
*/
int groupId;
}
private
private
private
private
150. public class TarHeader{
/**
* The entry's name.
*/
StringBuffer name;
/**
* The entry's permission mode.
*/
int mode;
/**
* The entry's user id.
*/
int userId;
/**
* The entry's group id.
*/
int groupId;
}
private
private
private
private
Encapsulate public data
(in TarHeader)1
167. vs.
Extract method
(out of the duplicated code)1
Move the newly created method
(in TarHeader)2
Encapsulate public data
(in TarHeader)3
Move data
(TarHeader > TarEntry)1
Encapsulate public data
(in TarEntry)2
168. vs.
Which solution is best?
Extract method
(out of the duplicated code)1
Move the newly created method
(in TarHeader)2
Encapsulate public data
(in TarHeader)3
Move data
(TarHeader > TarEntry)1
Encapsulate public data
(in TarEntry)2