Professional Documents
Culture Documents
Supporting OO Design Heuristics
Supporting OO Design Heuristics
Supporting OO Design Heuristics
Abstract "Quality"
1
Hypothesis Visualisation is discussed in §4 and some results are presented in §5. Fi-
Heuristics Heuristics nally, we present our conclusions and outline our ongoing
work.
2. Design heuristics
Metrics
In this section, we focus on the rôle of (sets of) design
Semantic Model
heuristics in modern OO software engineering.
.NET Design is, in general, a difficult task because it involves
finding compromises between conflicting pressures—cost
Figure 2. Supporting heuristics and reliability, for example—and many of these pressures
ultimately arise from human concerns, with all that implies
in complexity, diversity and changeability. Designers must
is on their evaluation1 and interpretation. Our work is in- find ways to provide specific capabilities required by stake-
tended to provide the basis for an exploratory framework in holders, while attaining sufficient quality in emergent prop-
which heuristics may be postulated, explored and managed. erties such as usability, efficiency, and flexibility.
Two significant contributions are reported in this paper. Software designers aim to satisfy the expectations of
Firstly, we show how heuristics can be refined sufficiently stakeholders by meeting functional and non-functional re-
to enable precise formulation and quantitative evaluation. quirements. But in order to make this possible, they must
We also present a common semantic model for OO soft- first address the needs of the software developers them-
ware: the model is independent of source languages yet al- selves. Keeping the complexity of the design in check is
lows mappings to particular language constructs where re- foremost among these.
quired. We illustrate its rôle in supporting the formulation Object-orientation (OO) allows software to be structured
and evaluation of heuristics. in a way that helps to manage complexity and change.
Our approach is based on the architecture outlined in However, as software reuse practitioners (and others) have
Figure 2 and consists of a number of steps. discovered, realising the benefits of OO is not straight-
• A set of heuristics is identified according to their rel- forward. Competence with the mechanisms of OO—
evance to design hypotheses, activities and require- classes and objects, attributes and methods, inheritance and
ments. The selected heuristics will in general not be polymorphism—is far from sufficient to ensure successful
orthogonal—and may even have conflicts. designs.
In this regard, OO is no different from other program-
• Individual heuristics are parameterised so that they can ming paradigms. Over some decades, software engineers
be expressed in terms of precisely-defined metrics. have developed a number of approaches and principles to
elevate design considerations above programming language
• The metrics employed are themselves precisely de-
mechanisms. Concepts such as abstraction, separation of
fined in terms of elements of a semantic model.
concerns [13], information hiding [40], cohesion and cou-
• The model is populated by extracting information from pling [43], and Design By ContractTM [35] provide guid-
individual source files, using the .NET CLR infrastruc- ance to designers.
ture to maximise language independence. On top of these general principles, the OO design com-
munity has developed a rich doctrine of principles, apho-
• Values for the required metrics are then computed and risms and practices to inform designers. Examples in-
expressed in a suitable parameterised form. clude: the open/closed principle [34, 32], the Liskov substi-
tution principle [28, 31], the acyclic dependencies princi-
• Finally, the resulting information is presented to the
ple [34, 30], Favour composition over inheritance [26], the
user in forms appropriate to the corresponding heuris-
Law of Demeter [27], You ain’t gonna need it (YAGNI) 2
tics.
and Tell, don’t ask [3].
The remainder of the paper is structured as follows. In This lore is supplemented by design patterns and idioms,
the next section we outline the rationale for heuristics and which provide prototypical solutions to common problems.
discuss their relevance to software engineering. In §3 we The original set of 23 Gang of Four design patterns [20]
describe our semantic model-based approach. The process has been broadened by more design patterns [12, for exam-
of mapping heuristics to quantities which can be evaluated ple], architectural patterns [6], analysis patterns [17], an-
1 We use evaluation to mean obtaining suffi cient information to deter-
tipatterns [5], and others.
mine the extent to which a heuristic is satisfi ed 2 http://c2.com/cgi/wiki?YouArentGonnaNeedIt
Some authors have collated parts of this complex web of and a particular designer is likely to be interested only
concepts into sets of heuristics3 . Johnson and Foote (1988) in one side of the debate. Many of the tenets aris-
provide an early example, which describes design max- ing from software reuse culture, for example, are in
ims intended to promote reuse. Riel (1996) documents 61 opposition to more recent refactoring and agile meth-
‘golden rules’ for OO design, while Fowler and Beck de- ods approaches. The open/closed principle, for exam-
scribe 22 code smells [18]. ple, encourages anticipation of future needs by mak-
The choice of the term ‘smells’ is instructive. It evokes a ing the design open for extension (reusable), but with-
subjective, subtle process of perceiving something about a out requiring modification of existing code; refactoring
design. Beck and Fowler note that code smells do not lend culture discourages anticipation of future needs and
themselves to automatic quantification [18]. The designer prefers modifying existing code when necessary. This
must form an impression of the net product of many factors cultural difference might show up in unexpected ways,
at work in the design. This requires judgement and insight such as a stronger preference for small methods in the
beyond the capabilities of simple automata. reuse culture, so that methods constitute small overrid-
From the architect Christopher Alexander [2, 1], the de- able units.
sign patterns community has borrowed an illuminating way
of viewing the designer’s task: it is to find a balance point • Nebulous definitions. RH2.8, for example, says “A
between all of the forces acting on the design. Design pat- class should capture one and only one key abstraction”,
terns are examples of such balance points, but they are not but rigorously specifying the meaning of “key abstrac-
simple prescriptive solutions: the forces are identified and tion” is problematic. Similarly, RH3.6 “Model the real
the factors that might influence the balance are described. world whenever possible”, is only as firm as our grip
A notable characteristic of design patterns is that they often on reality.
break rules. For example, the Composite pattern advocates
the use of methods that are overridden to do nothing, con- • Subjectivity and calibration. Code smells require
trary to a common maxim, expressed by Riel’s heuristic 5.7 the designer to judge when some intangible threshold
(RH5.17) as “It should be illegal for a derived class to over- has been crossed. The “large class smell”, “lazy class
ride a base class method with a NOP method, i.e. a method smell” and “long method smell” are obvious examples
which does nothing.” However, the Gang of Four chose to where different standards might apply. The relative im-
break this rule deliberately, in their words preferring trans- portance of conflicting heuristics is also dependent on
parency over safety. Many similar examples of conflicting the value system of the designer. If breaking up a large
forces can be found. class produces a lazy class, is the result better?
Some conflicts are so pervasive that they apply to nearly
all design situations. Separation of concerns, for example, • Interpretation in different contexts. Many heuris-
encourages decoupling portions of a design, while RH2.9, tics are expressed abstractly, in order to apply to any
“Keep related data and behaviour in one place” often sug- OO design. It may be necessary, however, to adapt a
gests the opposite. heuristic to local conditions. For example, when de-
ciding if an inheritance hierarchy is too deep, should
Even within an organised set of heuristics, conflicts oc-
the root class be counted in programming languages
cur. RH5.4 says “Theoretically, inheritance hierarchies
that enforce a single root? Or, in an organisation that
should be deep, i.e. the deeper the better”, while RH5.5
has adopted a refactoring approach to software devel-
adds the qualification that “In practice, inheritance hierar-
opment, how much emphasis should be placed on a
chies should be no deeper than an average person can keep
heuristic motivated by software reuse, such as RH5.7
in his or her short-term memory. A popular value for this
“All base classes should be abstract classes”?
depth is six”.
Heuristics are a valuable tool for identifying design
• Diverse levels of abstraction. Some heuristics can be
forces (whether conflicting or not) and evaluating design
interpreted at different levels. RH2.1, “All data should
quality, but their application is not straightforward for many
be hidden within its class”, might be viewed as a syn-
reasons, such as:
tactic restriction—make attributes private—or as a se-
mantic one, which might also discourage the use of
• Lack of consensus on which heuristics should be
getters. A “long method smell” could be detected at a
adopted. Some conflicting heuristics usefully illumi-
lexical level by counting lines of code, at a syntactic
nate matters of concern to the designer. Other con-
level by counting statements and expressions, at a lan-
flicts, however, reflect differing design philosophies,
guage semantic level by counting method invocations,
3 We use the term “heuristics” inclusively, to describe maxims, con- collaborators, etc, or at a problem-domain semantic
cepts, principles, warnings, etc. level by gauging the conceptual size of the method.
• Information overload. Heuristics are intended to help mon Intermediate Language (CIL) and run on the same Vir-
software engineers manage the complexity of soft- tual Execution System regardless of their individual syn-
ware, but injudicious application of heuristics could taxes [37].
compound the problem. The existence of a language-independent semantic
model confers significant advantages. It allows us to de-
• Acquiring relevant data and relating it to heuris- fine heuristics and metrics against the common model, giv-
tics. Many heuristics require substantial data gather- ing us language-independent metrics and a basis for cross-
ing. RH4.4 “Minimize fanout in a class”, and RH4.6 language comparisons, without loss of rigour. In addition, it
“Most of the methods defined on a class should be makes adding new languages to the model straightforward.
using most of the data members most of the time”
It is necessary to address the problem of mapping
are examples. Additionally, the correspondence be-
the common semantic model to programming language-
tween available information and heuristics is not al-
specific semantics, in order to communicate our findings
ways clear.
back to a software engineer working in a specific pro-
These issues, and the inherent fuzziness of heuristics, gramming language. We achieve this by explicitly mod-
make automated support of heuristics difficult. In conse- elling programming language-specific semantics, and map-
quence, designers usually must gauge the quality of their ping the relationships between the specific and common
products without assistance from tools. The designer builds model, as shown in Figure 3. In this figure, each map-
a mental model of the software, and evaluates it (smells it?), ping is a bi-directional association between semantic con-
according to a subjective, and perhaps even subconscious, cepts in .NET and Java. Java and C# are sufficiently similar
process that is likely to be informed by heuristics, but may that most mappings are 1:1. For example, a .NET class is
explicitly apply few. mapped exactly onto a Java class and vice versa. Mean-
Our research investigates ways to help designers with the while, a Property in .NET is a combination of a setter and a
task of understanding, evaluating and improving their prod- getter method and is therefore mapped to two Java methods.
ucts. While we view the art of design—and the judgement Our mapping approach addresses differences in the se-
of how to apply heuristics—as beyond the reach of current mantic models, such as the absence of a pointer type from
technology, we argue that tools can provide valuable infor- Java. Similarly, .NET has no corresponding concepts to the
mation to assist the designer with these judgements. scope defined by a Java source file.
With most OO languages, heuristics and metrics calcu-
lated against one model can usually be translated into the
3. Semantic models
semantics of another model, as illustrated in Figure 4. For
example, the Depth of Inheritance Tree (DIT) of a class in
A key element of our approach is the use of a semantic
J# increases by 1 when compiled to .NET CIL. This is be-
model that exposes software structure. In this paper we ex-
cause every class in J# inherits from the java.lang.Object
tend our previous work on applications of semantic models
in a library assembly, which in turn inherits the root
in software metrics, software visualisation and collaborative
System.Object class of the .NET framework.
software engineering [24, 25, 11, 39].
The latest version of .NET supports generic types and
Object-oriented semantic models represent the semantic
our common semantic model captures these. We have ex-
concepts of OO software including entities such as classes,
tended our earlier Java model to include the generic type
interfaces, methods and fields together with relationships
features introduced in Java 1.5 and mapped them to .NET
between them such as inheritance, implementation, contain-
generics. Our architecture allows us to add new language-
ment, overloading and invocation. Semantic models are the
specific models and mappings as needed.
result of static analysis, which is concerned with examining
source code and possibly other software artifacts, such as
UML diagrams, without executing the software. 4. Evaluating heuristics
In this paper we describe how semantic models can sup-
port the calculation of design heuristics. From earlier work, As outlined in §1, our approach is to identify heuristics of
we have a Java [25] and a .NET semantic model [38]. Our interest, and find ways of mapping them to metrics derived
Java semantic model captures the type system of the Java from our semantic model. Figure 5 shows an expanded view
language, while our .NET model captures the type system of the architecture summarised in Figure 2 and illustrates
defined by the .NET CLI standard. In effect, the .NET how this fits the pipeline-based models we have described
model gives us a way of representing the common semantic elsewhere [23].
underpinnings for a wide range of programming languages. Heuristic statements, such as those given by Riel, are
The .NET Common Language Infrastructure allows dif- typically couched in high-level, natural language, abstract
ferent languages to be compiled to the same .NET Com- terms: they express concepts which are readily understood
Class
Method Method
Java source C# source .pe
Java Field Method
Setter Getter
parse
ClassMapping FieldMapping
PropertyMapping
Mappings MethodMapping
Parse
Property
trees
.NET Class Field Method
analyse
Java-specific C#-specific
semantic model semantic model
language-specific
mappings
Common Semantic
Model
Figure 4. Mapping metrics
evaluation
by developers. Unlike metrics, heuristics generally do not
have precise values. They are often expressed as commands
or slogans (e.g. RH2.9 “Keep data and behaviour in one
place”) or in a ‘more/less is better’ form (e.g. RH3.3 “Be- Metrics
filters
ware of classes with many accessor methods in their public
interfaces”). evaluation
Assessing whether a particular heuristic applies or is be-
ing followed is inevitably subjective. Some ‘fuzziness’ is
required in order to present data to the user in a form which
is consistent with the original heuristic statements. Parametrised heuristics
In order to introduce objective, quantifiable elements we
first derive parameterised heuristics. comprehension
This involves the identification of the relevant param- mapping & exploration
eter(s) for each heuristic. For example, the occurrence
of literal values (e.g. in contexts such as “inheritance
should be no deeper than 6”) suggests that a constant (e.g.
max depth) might be appropriate. However, a variable Heuristic Visualisation
whose value can be adjusted to serve as a threshold is likely statement & presentation
4 http://www/parallelgraphics.com 5 http://www.ggobi.org
is complete and longitudinal studies will initially involve
studies of student software engineering projects.
We are encouraged by the results to date and are now
able to deploy heuristics alongside our range of metrics
and visualisation techniques. Our ongoing work includes
the development of interactive tools for managing and cus-
tomising heuristics. Ultimately, we anticipate that our ap-
proach will enable software engineers to propose, evaluate
and test their own heuristics alongside those in the wider
body of knowledge.
References