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 preservations 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.
翻译:方法的职责是执行所需的计算并通过各种交付物(包括输出指令中的对象字段和变量)将结果传递给其调用者。基于这一定义,本文提出了一种新算法,用于将长方法重构为单一职责的方法。我们提出了一种向后切片算法,将长方法分解为轻微重叠的切片。每个切片针对每个输出指令计算,代表委托给该方法的职责结果。如果切片准则涉及同一输出变量,则这些切片将不重叠。切片进一步被提取为独立方法,在保持特定行为的前提下由原始方法调用。该方法已在GEMS提取方法重构基准测试和三个真实项目上进行了评估。实验表明,与现有最先进方法相比,我们的方法在发现重构机会的精确率上平均提升至少29.6%,召回率平均提升12.1%。此外,重构后我们的工具将方法级内聚度量平均提高了20%。实验结果证实了所提方法在提取单一职责方法方面的适用性。