Professional Documents
Culture Documents
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.
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
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