Abstract
The responsibility of a method/function is to perform some desired computations and disseminate the results to its caller through various deliverables, including object fields and variables in output instructions. Based on this definition of responsibility, this paper offers a new algorithm to refactor long methods to those with a single responsibility. We propose a backward slicing algorithm to decompose a long method into slightly overlapping slices. The slices are computed for each output instruction, representing the outcome of a responsibility delegated to the method. The slices will be non-overlapping if the slicing criteria address the same output variable. The slices are further extracted as independent methods, invoked by the original method, if certain behavioral preservation are made. The proposed method has been evaluated on the GEMS extract method refactoring benchmark and three real-world projects. On average, our experiments demonstrate at least a 29.6% improvement in precision and a 12.1% improvement in the recall of uncovering refactoring opportunities compared to the state-of-the-art approaches. Furthermore, our tool improves method-level cohesion metrics by an average of 20% after refactoring. Experimental results confirm the applicability of the proposed approach in extracting methods with a single responsibility.
Similar content being viewed by others
Data Availibility Statement
The datasets generated and analyzed during the current study are available in the SBSRE GitHub repository, https://github.com/Alireza-Ardalani/SBSRE.
Notes
The slice is defined for a slicing criterion \(C=(x,v)\) where x is a statement (line number) in program P and v is a variable in x. A static slice includes all the statements that can affect the value of variable v at statement x for any possible input.
Expansion in backward slicing refers to tracing backward from a slicing criterion (which includes a statement and a variable within it) to discover all statements that affect the variable’s value.
References
Aho AV, Sethi R, Ullman JD (2007) Compilers: principles, techniques, and tools (vol 2). Addison-wesley Reading
Alcocer JPS, Antezana AS, Santos G, Bergel A (2020) Improving the success rate of applying the extract method refactoring. Sci Comput Program 195:102475
Al Dallal J (2010) Measuring the discriminative power of object-oriented class cohesion metrics. IEEE Trans Softw Eng 37(6):788–804
Allen FE (1970) Control flow analysis. ACM Sigplan Not 5(7):1–19
Ampatzoglou A, Tsintzira A-A, Arvanitou E-M, Chatzigeorgiou A, Stamelos I, Moga A, . . . Kehagias D (2019) Applying the single responsibility principle in industry: modularity benefits and trade-offs. Proceedings of the evaluation and assessment on software engineering, pp 347–352
Arcelli Fontana F, Mäntylä MV, Zanoni M, Marino A (2016) Comparing and experimenting machine learning techniques for code smell detection. Empir Softw Eng 21:1143–1191
Brdar I, Vlajkov J, Slivka J, Grujić K-G, Kovačević A (2022) Semi-supervised detection of long method and god class code smells. 2022 ieee 20th jubilee international symposium on intelligent systems and informatics (sisy), pp 403–408
Charalampidou S, Ampatzoglou A, Chatzigeorgiou A, Gkortzis A, Avgeriou P (2016) Identifying extract method refactoring opportunities based on functional relevance. IEEE Trans Softw Eng 43(10):954–974
Cheng BH, Sawyer P, Bencomo N, Whittle J (2009) A goal-based modeling approach to develop requirements of an adaptive system with environmental uncertainty. Model driven engineering languages and systems: 12th international conference, models 2009, Denver, CO, USA, October 4-9, 2009. Proceedings 12, pp 468–483
Cohen J (2013) Statistical power analysis for the behavioral sciences. Academic press
De Lucia A (2001) Program slicing: methods and applications. Proceedings first ieee international workshop on source code analysis and manipulation, pp 142–149
Fayad ME, Hamza H, Sánchez H (2003) A pattern for an effective class responsibility collaborator (crc) cards. Proceedings fifth ieee workshop on mobile computing systems and applications, pp 584–587
Fernandes S, Aguiar A, Restivo A (2022) A live environment to improve the refactoring experience. Companion proceedings of the 6th international conference on the art, science, and engineering of programming, pp 30–37
Ferrante J, Ottenstein KJ, Warren JD (1987) The program dependence graph and its use in optimization. ACM Trans Program Lang Syst (TOPLAS) 9(3):319–349
Fontana FA, Ferme V, Zanoni M, Roveda R (2015) Towards a prioritization of code debt: a code smell intensity index. 2015 ieee 7th international workshop on managing technical debt (mtd), pp 16–24
Fontana FA, Zanoni M (2017) Code smell severity classification using machine learning techniques. Knowl-Based Syst 128:43–58
Fortunato S (2010) Community detection in graphs. Phys Rep 486(3–5):75–174
Fowler M (1999) Refactoring: Improving the design of existing code. Addison-Wesley, Boston, MA, USA
Fundation A (2011) Apache xerces-j 2. Retrieved from http://xerces.apache.org/index.html
Gilbert D (2017) Jfree chart. Retrieved from https://github.com/jfree/jfreechart
Green P, Lane PC, Rainer A, Scholz S (2009) An introduction to slice-based cohesion and coupling metrics
Hadj-Kacem M, Bouassida N (2018) A hybrid approach to detect code smells using deep learning. Enase 137–146
Han J, Kamber M, Pei J (2012) Data mining concepts and techniques third edition. University of Illinois at Urbana-Champaign Micheline Kamber Jian Pei Simon Fraser University
Hora A, Robbes R (2020) Characteristics of method extractions in java: a large scale empirical study. Empir Softw Eng 25:1798–1833
Hotta K, Higo Y, Kusumoto S (2012) Identifying, tailoring, and suggesting form template method refactoring opportunities with program dependence graph. 2012 16th European conference on software maintenance and reengineering, pp 53–62
Hubert J (2019) Implementation of an automatic extract method refactoring (Unpublished master’s thesis)
Komondoor R, Horwitz S (2000) Semantics-preserving procedure extraction. Proceedings of the 27th acm sigplan-sigact symposium on principles of programming languages, pp 155–169
Lacerda G, Petrillo F, Pimenta M, Guéhéneuc YG (2020) Code smells and refactoring: a tertiary systematic review of challenges and observations. J Syst Softw 167:110610
Lanza M, Marinescu R (2007) Object-oriented metrics in practice: using software metrics to characterize, evaluate, and improve the design of object-oriented systems. Springer Sci Bus Med
Lopes M, Hora A (2022) How and why we end up with complex methods: a multi-language study. Empir Softw Eng 27(5):115
Martin RC (2003) Agile software development: principles, patterns, and practices. Prentice Hall PTR
Martin RC (2009) Clean code: a handbook of agile software craftsmanship. Pearson Education
Maruyama K (2001) Automated method-extraction refactoring by using block-based slicing. Proceedings of the 2001 symposium on software reusability: putting software reuse in context, pp 31–40
Meananeatra P, Rongviriyapanish S, Apiwattanapong T (2018) Refactoring opportunity identification methodology for removing long method smells and improving code analyzability. IEICE Trans Inf Syst 101(7):1766–1779
Mens T, Tourwé T (2004) A survey of software refactoring. IEEE Trans Softw Eng 30(2):126–139
Mkaouer MW, Kessentini M, Bechikh S, Cinnéide Ó M, Deb K (2016) On the use of many quality attributes for software refactoring: a many-objective search-based software engineering approach. Empir Softw Eng 21:2503–2545
Mongiovi M, Gheyi R, Soares G, Ribeiro M, Borba P, Teixeira L (2017) Detecting overly strong preconditions in refactoring engines. IEEE Trans Softw Eng 44(5):429–452
Orailoglu A, Gajski DD (1986) Flow graph representation. Proceedings of the 23rd acm/ieee design automation conference, pp 503–509
Ott LM, Thuss JJ (1993) Slice based metrics for estimating cohesion. [1993] proceedings first international software metrics symposium, pp 71–81
Ottenstein KJ, Ottenstein LM (1984) The program dependence graph in a software development environment. ACM SIGPLAN Not 19(5):177–184
Palomba F, Panichella A, De Lucia A, Oliveto R, Zaidman A (2016) A textual-based technique for smell detection. 2016 ieee 24th international conference on program comprehension (icpc), pp 1–10
Pecorelli F, Di Nucci D, De Roover C, De Lucia A (2020) A large empirical assessment of the role of data balancing in machine-learning-based code smell detection. J Syst Softw 169:110693
Robert M (2017) Clean architecture: a craftsman’s guide to software structure and design. Prentice Hall
Shahidi M, Ashtiani M, Zakeri-Nasrabadi M (2022) An automated extract method refactoring approach to correct the long method code smell. J Syst Softw 187:111221
Silva D, Terra R, Valente MT (2014). Recommending automated extract method refactorings. Proceedings of the 22nd international conference on program comprehension, pp 146–156
Tiwari O, Joshi R (2022) Identifying extract method refactorings. 15th innovations in software engineering conference, pp 1–11
Tsantalis N, Chaikalis T, Chatzigeorgiou A (2018) Ten years of jdeodorant: lessons learned from the hunt for smells. 2018 ieee 25th international conference on software analysis, evolution and reengineering (saner), pp 4–14
Tsantalis N, Chatzigeorgiou A (2011) Identification of extract method refactoring opportunities for the decomposition of methods. J Syst Softw 84(10):1757–1782
Wakaito (2011). Weka 3: machine learning software in java. Retrieved from http://old-www.cms.waikato.ac.nz/ml/weka/
Weiser M (1984) Program slicing. IEEE Trans Softw Eng 4:352–357
Xu S, Sivaraman A, Khoo S-C, Xu J (2017) Gems: an extract method refactoring recommender. 2017 ieee 28th international symposium on software reliability engineering (issre), pp 24–34
Funding
This study has received no funding from any organization.
Author information
Authors and Affiliations
Corresponding author
Ethics declarations
Ethical Approval
This article does not contain any studies with human participants or animals performed by any of the authors.
Conflict of Interest
All of the authors declare that they have no conflict of interest.
Additional information
Communicated by: Andy Zaidman.
Publisher's Note
Springer Nature remains neutral with regard to jurisdictional claims in published maps and institutional affiliations.
Appendix A: Extended Data
Appendix A: Extended Data
Table 19 shows the confusion matrix elements obtained by expert evaluations of different extract method refactoring tools, including the proposed SBSRE tool, JDeodorant, SEMI, JExtract, and GEMS.
Rights and permissions
Springer Nature or its licensor (e.g. a society or other partner) holds exclusive rights to this article under a publishing agreement with the author(s) or other rightsholder(s); author self-archiving of the accepted manuscript version of this article is solely governed by the terms of such publishing agreement and applicable law.
About this article
Cite this article
Ardalani, A., Parsa, S., Zakeri-Nasrabadi, M. et al. Supporting single responsibility through automated extract method refactoring. Empir Software Eng 29, 28 (2024). https://doi.org/10.1007/s10664-023-10427-3
Accepted:
Published:
DOI: https://doi.org/10.1007/s10664-023-10427-3