You are on page 1of 5

2018 IEEE International Conference on Software Maintenance and Evolution

Towards Feature Envy Design Flaw Detection at


Block Level
Árpád Kiss and Petru Florin Mihancea
LOOSE Research Group
Politehnica University of Timişoara, Romania
Email: arpad.kiss29@gmail.com, petru.mihancea@cs.upt.ro

Abstract—Software is continuously evolving as bugs need to should be extracted and moved to the envied class, not the
be fixed and new features need to be added. Design flaws hinder entire operation. Consequently, to simplify the maintainer
the simple evolution of software and thus, we have to detect and activity in identifying these sub-method fragments that need
correct them. Feature Envy is an object-oriented design issue that
can be detected at the level of methods using different state-of- to be refactored, a more fine-grained detection mechanism
the-art approaches. Unfortunately, these are insufficient because is needed. Furthermore, this mechanism is also necessary if
only a portion of a method may actually be affected by this flaw. a higher degree of automation of the entire detection and
Thus, only that part needs to be treated using the corresponding correction process is desired.
correction strategy, not the entire method. To address this issue, The same limitation is also recognized by Lanza and
we propose the detection of Feature Envy code smell at the level
of blocks of code. Initial evaluation suggests that our approach Marinescu [2]. Due to dependencies between the problematic
is promising in spotting the envious areas within a method. method and the class declaring it, the authors reiterate that “it
is very rare that we can move the whole method to the other
Keywords-feature envy, design flaw detection, refactoring
class”. Moreover, the authors explain that a method affected by
the Brain Method flaw may contain portions that are actually
I. I NTRODUCTION
affected by the Feature Envy problem. Thus, a fine-grained
Nowadays a never-ending amount of code is written by detection mechanism of the latter bad smell at a sub-method
developers and gets deployed in various software products. level, could also help a maintainer to spot more easily envious
While it may function satisfactorily at that moment, require- parts “hidden” in complicated and large methods.
ment changes, new features and other kinds of enhancement As a result, we propose the detection of Feature Envy
requests will definitely imply source code modifications in the design flaw at the source code block level. First, we apply
future. Unfortunately, when a software product is continuously a lightweight method body decomposition into blocks by
changed and/or extended through time, it inevitably grows in employing a third-party technique described by Yang et al. [6].
complexity. Developers will start spending more and more Next, we use the detection strategy introduced by Lanza and
time on understanding their own or others’ source code in Marinescu for the identification of envious methods [2], and
order to modify it, adapt it and search it for bugs. An option to applying it at the block level in order to detect envious pieces
reduce the ever growing complexity of code is to continuously of code. Finally, we try to group together envious blocks with
detect and eliminate emerging design flaws. their neighbours to obtain more accurate envious fragments.
Fowler et al. [1] define 22 recurring bad smells (i.e., design The paper is organized as follows. Section II presents some
flaws) by the elimination of which one may increase the related work. Section III describes the inner workings of our
maintainability of an object-oriented program. We focus on solution with some few additional implementation details in
the design problem known as Feature Envy, occurring when Section IV. Section V illustrates our findings after applying
a method is more interested in the data belonging to another our approach on real life code, while Section VI concludes
class rather than of its own. In short, the treatment of this our paper and elaborates future work ideas.
“disease” consists of moving the method to the envied class.
However, we have to first identify the problematic entities. II. R ELATED W ORK
Different approaches are presented in the state-of-the-art Several different approaches for Feature Envy design flaw
literature for the identification of the Feature Envy bad smell detection are described in the state-of-the-art literature. In [2]
e.g., [2], [3], [4], [5]. For instance, Lanza and Marinescu [2] a metric-based approach is applied. Three software metrics
define specific object-oriented software metrics and use them are used to capture structural properties of methods, relevant
in combination, in the form of a detection strategy, for the in the context of Feature Envy e.g., the number of attributes
identification of envious methods. from external classes accessed by an operation. Next, for
However, all these approaches are insufficient because they each method, these measurements are classified as normal or
identify the flaw only at the method level. This is problematic, abnormal by comparing them to some threshold limits. Finally,
because “sometimes only part of the method suffers from a method is considered envious when all its metric values are
envy” [1]. In these cases, only the envious part of the method abnormal.

2576-3148/18/$31.00 ©2018 IEEE 544


DOI 10.1109/ICSME.2018.00064
In the same metric-based spirit, in [3], the authors introduce strategy from [2]. Finally, we try to group envious blocks with
a distance metric between methods and classes based on their neighbors in order to form larger envious fragments. In
member accesses. In essence, methods that are “closer” to the following, we present each of these phases in detail based
other classes rather than their declaring class are identified as on the accompanying example from Listing 1.
“desiring” to be moved elsewhere.
Listing 1. Accompanying example
A different approach for Feature Envy detection is intro- 1 public void p e r s i s t ( Object o ) {
duced in [4]. The author uses the commit history to detect 2 if (o instanceof Circle ) {
3 t h i s . s t r e a m . w r i t e I n t ( ( ( C i r c l e ) o ) . getX ( ) ) ;
operations more frequently changed together with operations 4 t h i s . s t r e a m . w r i t e I n t ( ( ( C i r c l e ) o ) . getY ( ) ) ;
from other classes rather than to methods from the same class. 5 t h i s . s t r e a m . w r i t e I n t ( ( ( C i r c l e ) o ) . getR ( ) ) ;
6 }
Yet a different detection methodology is presented in [5]. The 7 i f ( o instanceof Rectangle ) {
potential envious methods are identified as those that are more 8 t h i s . s t r e a m . w r i t e I n t ( ( ( R e c t a n g l e ) o ) . getX1 ( ) ) ;
9
similar to other classes, rather than the classes where they are 10 t h i s . s t r e a m . w r i t e I n t ( ( ( R e c t a n g l e ) o ) . getY1 ( ) ) ;
declared in, from the point of view of the involved textual 11 t h i s . s t r e a m . w r i t e I n t ( ( ( R e c t a n g l e ) o ) . getX2 ( ) ) ;
12 t h i s . s t r e a m . w r i t e I n t ( ( ( R e c t a n g l e ) o ) . getY2 ( ) ) ;
concepts e.g., identifiers found in their source code. 13 }
All these approaches detect the Feature Envy design issue 14 }
at method level. By contrast, we propose the detection of
the problem at the level of blocks of code inside the method A. Decomposition into blocks
bodies. For the actual detection, we use the same metric-based In essence, the block of statements representing the entire
approach described in [2], but we apply it at the more fine- body of a method is recursively split into sub-blocks following
grained block level. the nesting structure of the compound statements such as if,
From the granularity point of view, our work is related to for, etc. If a block does not contain compound statements it
several other approaches trying to split long methods into will become a basic block of statements corresponding to a
smaller pieces of code that could be extracted in separate leaf in the resulted hierarchical structure. However, there is an
operations. For this purpose, in [7], a methodology based additional splitting heuristic that is used in [6]: a block is split
on program slicing is presented. On the one hand, it tends into sibling sub-blocks at the positions of each blank line.
to be more accurate as it is based on a detailed program For the example presented in Listing 1 the corresponding
representation (i.e., program dependency graph) but, on the hierarchy of blocks in presented in Figure 1. As it can be seen,
other hand, more intensive from a computational point of view. we have 3 leaves here corresponding to the statements in lines
At the other side of the spectrum, the method splitting problem 3-5, 8 and 10-12, the last two being the result of the empty
is also addressed in [6], but in a more lightweight manner, line 9.
sacrificing precision. In essence, an operation is broken into
B. Detecting envious leaf blocks
blocks following the nesting of compound statements (e.g., if,
for). Next, refactoring opportunities are identified by consid- In this step, we apply on each leaf block the Feature
ering the length of a block and the number of input and output Envy detection strategy introduced in [2] and summarized in
values required if the block would be extracted into a separate Formula 1.
function.
In our approach, we chose to be lightweight and we em- IsEnvious(B) = AT F D(B) > F EWAT F D
ployed the same method decomposition approach as in [6]. ∧ LAA(B) < M AX P ERCEN TLAA (1)
However, by contrast to both aforementioned approaches, there ∧ F DP (B) ≤ F EWF DP
is an additional differentiating aspect. That is, the previous
methodologies could potentially identify and extract a piece of
code from a long method that would actually solve a correlated Method Block
Lines 1-14
Feature Envy design problem. As showed in [7], one such case
has actually been encounter during their evaluation. However,
in general, it cannot be know if the Feature Envy issues IfStatement IfStatement
from a long method would properly be extracted into separate Lines 2-6 Lines 7-13

methods using these techniques because they are not Feature


Envy aware. By contrast, we try to highlight envious fragments
within methods (long or not) that might be extracted into Then Block Then Block
separate operations to directly address Feature Envy issues. Lines 2-6 Lines 7-13

III. D ETECTING E NVIOUS B LOCKS OF C ODE


BasicStatement BasicStatement BasicStatement
Our methodology is composed of three phases. First, we Block Block Block
split the body of the analyzed method into a hierarchical Lines 3-5 Lines 8-8 Lines 10-12

structure of blocks according to the technique used in [6].


Next, for each leaf block, we apply the Feature Envy detection Fig. 1. The hierarchy of blocks for the example in Listing 1

545
The metrics at the block level are similar to those defined classes of the resulted fragment is included in the set of foreign
at the method level in [2]. ATFD (Access To Foreign Data) data providers of the initial envious block. The procedure stops
represents the number of attributes from other classes accessed when all previous siblings are included or at the first non-
from a block directly or indirectly via accessor methods. acceptable grouping attempt. Similarly, then we try to create an
LAA (Locality of Attributes Accesses) represents the relative even larger fragment by considering for grouping the siblings
number of local attributes (e.g., of the class declaring the occurring after the initial envious block1 .
method containing the block) accessed by that block. FDP b) Bottom-up grouping: When all the children of a parent
(Foreign Data Providers) represents the number of classes in node are grouped together by the previous heuristic, we try
which are defined the foreign attributes accessed by the block to also include the parent in the grouping to form an even
in accordance to the ATFD metric. larger envious fragment. The extension is performed under
We mention that in the current state of our work, we the same conditions, as mentioned in case of the previous
use the same form of the Feature Envy detection strat- heuristic: the Feature Envy detection strategy must still be
egy as implemented in the I P LASMA analysis tool [2], trigger when applied on the larger fragment and its set of
[8], including the threshold values (i.e., F EWAT F D = 2, foreign data providers must be included in the set of providers
M AX P ERCEN TLAA = 1/3 and F EWF DP = 2). Nev- of the children group. We mention that the case when a parent
ertheless, some adaptations may be required in the future for has just one child (that is also envious) is similarly treated by
tuning the strategy accuracy at the block level e.g., different this heuristic.
thresholds, additional metrics. For the example in Listing 1 / Figure 1, the leaf cor-
By applying the aforementioned detection strategy on the responding to lines 10-12 was detected as envious in the
leaf blocks from Figure 1, we can identify as envious the two previous analysis step. Its foreign data provider set of classes
leaves associated to lines 3-5 and 10-12. For instance, the first is {Rectangle}, being the single external class from which
block accesses 3 foreign attributes via accessors (i.e., getX, the fragment gathers data. As a result of the breadth grouping
getY and getR), all from the Circle class. This means its ATFD this leaf is combined with its sibling leaf corresponding to line
is 3 and its FDP is 1. Additionally, the first block accesses 1 8. It is easy to observe that the larger fragment (i.e., lines 8-
local field i.e., stream and thus, its LAA is 0.25. 12) is still detected as envious by the Feature Envy detection
What is important to observe here is that by applying the strategy and that its set of foreign data providers remains
detection strategy at the method level, we could also identify the same i.e., {Rectangle}. Thus, the grouping is accepted
the entire method as envious. However, in that case we would and the entire larger fragment is considered envious. Next,
find 2 distinct data providers, namely the Circle and the since all the children of the then-block from the second if are
Rectangle classes. Thus, at the method level, we cannot be sure grouped together, the second heuristic takes action. The parent
where to move the entire operation to solve the Feature Envy node does not add any supplementary statements and thus,
problem. By contrast, using our approach, we can observe that the Feature Envy detection strategy is still triggered on the
the block for lines 3-5 envies the Circle class while the block larger fragment and the set of data providers is not changed.
for lines 10-12 envies the Rectangle class. Thus, we can easily Consequently, the entire then-block is considered envious.
conclude where should these fragments be moved i.e., the first In the same manner, the bottom-up grouping heuristic marks
in the Circle class the second in the Rectangle class. as envious the fragments corresponding to each of the two if
Unfortunately, at a closer look, one may observe that the leaf statements i.e., the fragments in lines 2-6 and 7-13, respec-
block containing only line 8 is not detected as envious because tively. Note that the if conditions do not hinder the detection of
its ATFD is just 1. However, conceptually, it is similar to the those larger blocks of code as envious. However, it is important
envious leaf containing lines 10-12. The way we address this to observe that the two ifs will not be combined into an even
problem is the subject of the following subsection. large piece of code. That is because their foreign data provider
sets are different: for the first fragment it is {Circle}, while for
C. Grouping envious block with neighbours the second one it is {Rectangle}. Thus, the second condition
As a result of the second phase of our analysis, the envious for applying the breadth grouping does not hold.
leaf blocks are detected. However, these separate leaves may
IV. I MPLEMENTATION
actually form larger envious fragments. For instance, the leaf
containing lines 10-12 (see Listing 1) should be grouped with The approach we propose in this paper was implemented
the block corresponding to line 8 to form a larger envious piece in a prototypical E CLIPSE plugin called M ETHOD D EFRAG -
of code. Different grouping heuristics could be imagined here MENTER 2 (see Figure 2). The detected envious fragments are
but, at this moment, we define the following two ones. Both presented to the user in a dedicated view with additional infor-
are applied during a post-order traversal of the block structure. mation, such as the values of all involved metrics. Moreover,
a) Breadth grouping: When an envious block is found, the user can ask the plugin to highlight the envious fragments
we try to group it with its previous siblings, one by one. in the source code editor, using different colors.
In each step, a grouping is accepted if i) the resulted group 1 The grouping order may influence the final result but, for the moment, we
of blocks is still detected as envious by the Feature Envy do not consider this aspect.
detection strategy and ii) if the set of foreign data provider 2 https://bit.ly/2NHKaIl

546
Fig. 2. M ETHOD D EFRAGMENTER at Work

TABLE I TABLE II
C HARACTERISTICS OF THE ANALYZED SYSTEM C OMPARISON OF THE DETECTION RESULTS

Name Classes Methods Lines of Code I P LASMAdetection Same Block-level detection


jFreeCharta 980 11 018 123 110 more relevant relevance more relevant
a http://www.jfree.org/jfreechart/ 10 7 9

The hierarchical structure of blocks is constructed by vis-


between them and the Feature Envy issue as described in
iting the ASTs of the investigated methods as provided by
[2] e.g., a Brain Method might partially be a Feature Envy.
the E CLIPSE JDT infrastructure. Similarly, the values of the
Next, we manually investigated these cases and, to the best of
involved software metrics are computed by visiting the ASTs
our understanding, we selected only those containing at least
of the statements forming the measured block and using the
some relevant envious portion. We mention that the eliminated
cross-reference mechanism provided by E CLIPSE to uniquely
methods were in general instances of the Brain Method and /
identify the accessed entities.
or Intensive Coupling flaws and nothing was detected by our
V. E VALUATION approach in these cases.
The main goal of our very first evaluation was to establish As a result, we got 26 relevant methods to be considered
if the proposed approach is feasible in spotting Feature Envy in the remaining of our investigation. Next, we manually
areas within the body of methods in a better way than detecting compared the results produced by I P LASMA and by our
their entire bodies as envious. As a second objective, we approach and, to the best of our understanding of the source
wanted to observe real code situations in order to identify code, we established whose detection is more relevant. To do
ways to enhance our detection approach and the accuracy of that we tried to determine if an entire suspected method is
the envious fragment boundaries. really envious, if only a portion of it is envious, etc. and
For this purpose, we used I P LASMA [8] in order to detect we compared our assessment with the reports provided by
object-oriented design flaws in the system from Table I. From I P LASMA and by our tool. The results are summarized in
the resulted report, we randomly selected 40 methods exhibit- Table II and discussed in details in the remaining of this
ing a Feature Envy, a Brain Method, an Intensive Coupling section.
or any combination between these flaws. We also included In 7 cases (i.e., Same relevance category), we found that
the last two kinds of problems because of the close relations I P LASMA and our detection approach provide the same result.

547
In general, this means that the entire method bodies are found VI. C ONCLUSIONS AND F UTURE W ORK
as envious by both techniques. However, in 2 cases, only In this paper we introduced an approach for the detection of
parts of the corresponding methods looked actually envious the Feature Envy design flaw at a smaller level of granularity
and thus, our mean of analysis needs some improvements. than other approaches, namely at the level of blocks of code.
The problem is that we group blocks without effectively In essence, a method body is split into a hierarchical structure
considering their correlation at the semantics level. Sure, this of blocks. Next, the corresponding detection strategy is applied
is difficult to be done at the current level of abstraction of to identify envious leaf blocks. Finally, these envious blocks
the code. However, one possible improvement would be to are extended in order to better localize the Feature Envy issue
consider the visibility domain of the variables involved in in the body of the analyzed method.
accessing the foreign data providers for better block grouping Our initial evaluation showed that the technique is feasible
and splitting. and can spot the envious areas within a method even if with
In 10 cases, the results given by I P LASMA were better. In not a very high accuracy regarding the boundaries of these
almost all these situations, the involved methods looked as areas. However, at least in cases where Brain Method and/or
Feature Envy instances at the entire method body level. How- Intensive Coupling flaws hiding the envious sub-method parts,
ever, none of these cases were detected by our approach. The our approach behaves promisingly and better than I P LASMA.
problem is that we decompose the code into leaf blocks that are In the next period of time we will try to address the issues
far too small, many times containing just one statement. Thus, and the improvement directions mentioned in Section V: han-
the Feature Envy detection strategy does not get activated and, dling the detection of very small envious blocks of code and
consequently, nothing is grouped to form larger fragments. improving the block splitting / grouping algorithms to detect
Several possible improvements are envisioned here. On the more accurate boundaries for the envious fragments. Next,
one hand we could stop the block splitting procedure when a we also consider other possible improvements, for instance,
block becomes too small from the number of statements point ranking the findings (i.e., the envious sub-fragments) to spot
of view. Another idea would be to vary the detection strategy the most important and more feasible ones from the Feature
thresholds based on the size of the analyzed blocks. Envy correction strategy point of view (e.g., what fragments
would require a smaller number of parameters if extracted
Finally, in 9 cases, we considered the results provided by our into a separate operation?). For this purpose, we may even
approach to be more relevant. In some situations, I P LASMA restrain the envious block detection by adding supplementary
also detected a Feature Envy at the entire method level. metrics in the Feature Envy detection strategy. The special
Nevertheless, our approach managed to better indicate towards case of polymorphic interactions between a method and a data
the envious sub-fragments. More important however, in 6 cases provider is also considered for future work and it will probably
related to the Brain Method / Intensive Coupling flaws, only involve the analysis of the entire class hierarchy to identify the
our approach detected the presence of some relevant envious actual data source. Last but not least, we will reevaluate our
sub-fragments in the corresponding methods. Thus, from the technique from the accuracy point of view on a higher number
perspective of the Feature Envy detection in I P LASMA, these of problem instances and case study programs, ideally with the
are false-negative cases. As a result, we can conclude that our involvement of real users.
approach looks feasible, relevant and might be used even in
this stage of development as a complementary technique for R EFERENCES
I P LASMA when detecting envious sub-fragments in methods [1] M. Fowler, K. Beck, D. Roberts, and E. Gamma, Refactoring: Improving
the Design of Existing Code. Addison-Wesley Professional, 1999.
affected by Brain Method and/or Intensive Coupling design [2] M. Lanza and R. Marinescu, Object-Oriented Metrics in Practice.
problems. Springer Verlag, 2006.
[3] N. Tsantalis and A. Chatzigeorgiou, “Identification of move method
We would like to also mention that while in some cases the refactoring opportunities,” IEEE Transactions on Software Engineering,
boundaries of the detected envious fragments looked exact, vol. 35, no. 3, pp. 347–367, May 2009.
[4] F. Palomba, G. Bavota, M. D. Penta, R. Oliveto, D. Poshyvanyk, and
in many situations further improvements are needed in this A. D. Lucia, “Mining version histories for detecting code smells,” IEEE
direction. The fragments looked a little too long or too short Transactions on Software Engineering, vol. 41, no. 5, pp. 462–489, May
and thus, the grouping and/or block splitting mechanisms need 2015.
[5] F. Palomba, A. Panichella, A. D. Lucia, R. Oliveto, and A. Zaidman, “A
some refining. This would not be an easy task since, due textual-based technique for smell detection,” in Proceedings of the 24th
to our limited understanding of the case study code, it is IEEE International Conference on Program Comprehension (ICPC), May
relatively difficult to exactly and objectively establish the right 2016, pp. 1–10.
[6] L. Yang, H. Liu, and Z. Niu, “Identifying fragments to be extracted
boundaries. from long methods,” in Proceedings of the 16th Asia-Pacific Software
Engineering Conference. IEEE, 2009, pp. 43 – 49.
Last, but not least, we would like to mention that we [7] N. Tsantalis and A. Chatzigeorgiou, “Identification of extract method
encountered a situation when two non-adjacent fragments of refactoring opportunities for the decomposition of methods,” Journal of
the same method looked correlated with respect to the same Systems and Software, vol. 84, no. 10, pp. 1757–1782, Oct. 2011.
[8] C. Marinescu, R. Marinescu, P. F. Mihancea, and R. Wettel, “iPlasma:
envied class. Thus, our grouping mechanism might have to be An integrated platform for quality assessment of object-oriented design,”
extended to also permit to capture this kind of cases (and not in Proceedings of the 21st IEEE International Conference on Software
only correlation between adjacent blocks as currently does). Maintenance (ICSM) - Industrial and Tool volume, 2005.

548

You might also like