Skip to main content
Log in

Supporting single responsibility through automated extract method refactoring

  • Published:
Empirical Software Engineering Aims and scope Submit manuscript

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.

This is a preview of subscription content, log in via an institution to check access.

Access this article

Price excludes VAT (USA)
Tax calculation will be finalised during checkout.

Instant access to the full article PDF.

Fig. 1
Fig. 2
Fig. 3
Fig. 4
Fig. 5
Fig. 6
Fig. 7
Fig. 8
Fig. 9
Fig. 10
Fig. 11
Fig. 12
Fig. 13
Fig. 14
Fig. 15
Fig. 16
Fig. 17
Fig. 18
Fig. 19
Fig. 20
Fig. 21
Fig. 22
Fig. 23
Fig. 24

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

  1. 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.

  2. 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

    Article  Google Scholar 

  • Al Dallal J (2010) Measuring the discriminative power of object-oriented class cohesion metrics. IEEE Trans Softw Eng 37(6):788–804

    Article  Google Scholar 

  • Allen FE (1970) Control flow analysis. ACM Sigplan Not 5(7):1–19

    Article  Google Scholar 

  • 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

    Article  Google Scholar 

  • 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

    Article  Google Scholar 

  • 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

    Article  Google Scholar 

  • 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

    Article  Google Scholar 

  • Fortunato S (2010) Community detection in graphs. Phys Rep 486(3–5):75–174

    Article  MathSciNet  Google Scholar 

  • Fowler M (1999) Refactoring: Improving the design of existing code. Addison-Wesley, Boston, MA, USA

    Google Scholar 

  • 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

    Article  Google Scholar 

  • 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

    Article  Google Scholar 

  • 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

    Article  Google Scholar 

  • Mens T, Tourwé T (2004) A survey of software refactoring. IEEE Trans Softw Eng 30(2):126–139

    Article  Google Scholar 

  • 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

    Article  Google Scholar 

  • 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

    Article  Google Scholar 

  • 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

    Article  Google Scholar 

  • Robert M (2017) Clean architecture: a craftsman’s guide to software structure and design. Prentice Hall

    Google Scholar 

  • 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

    Article  Google Scholar 

  • 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

    Article  Google Scholar 

  • 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

    Article  Google Scholar 

  • 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

Download references

Funding

This study has received no funding from any organization.

Author information

Authors and Affiliations

Authors

Corresponding author

Correspondence to Saeed Parsa.

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.

Table 19 The confusion matrix elements of the proposed refactoring opportunities based on the experts’ opinions

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.

Reprints and permissions

About this article

Check for updates. Verify currency and authenticity via CrossMark

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

Download citation

  • Accepted:

  • Published:

  • DOI: https://doi.org/10.1007/s10664-023-10427-3

Keywords

Navigation