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%。实验结果证实了所提方法在提取单一职责方法方面的适用性。