java-refactoring-extract-method

java-refactoring-extract-method

热门

在Java语言中使用提取方法进行重构

3.6万Star
4556Fork
更新于 2026/7/13
SKILL.md
readonly只读
name
java-refactoring-extract-method
description

在Java语言中使用提取方法进行重构

使用提取方法重构Java方法

角色

您是重构Java方法的专家。

以下是2个示例(标题分别为重构前的代码和重构后的代码),展示了提取方法

重构前代码 1:

public FactLineBuilder setC_BPartner_ID_IfValid(final int bpartnerId) {
	assertNotBuild();
	if (bpartnerId > 0) {
		setC_BPartner_ID(bpartnerId);
	}
	return this;
}

重构后代码 1:

public FactLineBuilder bpartnerIdIfNotNull(final BPartnerId bpartnerId) {
	if (bpartnerId != null) {
		return bpartnerId(bpartnerId);
	} else {
		return this;
	}
}
public FactLineBuilder setC_BPartner_ID_IfValid(final int bpartnerRepoId) {
	return bpartnerIdIfNotNull(BPartnerId.ofRepoIdOrNull(bpartnerRepoId));
}

重构前代码 2:

public DefaultExpander add(RelationshipType type, Direction direction) {
     Direction existingDirection = directions.get(type.name());
     final RelationshipType[] newTypes;
     if (existingDirection != null) {
          if (existingDirection == direction) {
               return this;
          }
          newTypes = types;
     } else {
          newTypes = new RelationshipType[types.length + 1];
          System.arraycopy(types, 0, newTypes, 0, types.length);
          newTypes[types.length] = type;
     }
     Map<String, Direction> newDirections = new HashMap<String, Direction>(directions);
     newDirections.put(type.name(), direction);
     return new DefaultExpander(newTypes, newDirections);
}

重构后代码 2:

public DefaultExpander add(RelationshipType type, Direction direction) {
     Direction existingDirection = directions.get(type.name());
     final RelationshipType[] newTypes;
     if (existingDirection != null) {
          if (existingDirection == direction) {
               return this;
          }
          newTypes = types;
     } else {
          newTypes = new RelationshipType[types.length + 1];
          System.arraycopy(types, 0, newTypes, 0, types.length);
          newTypes[types.length] = type;
     }
     Map<String, Direction> newDirections = new HashMap<String, Direction>(directions);
     newDirections.put(type.name(), direction);
     return (DefaultExpander) newExpander(newTypes, newDirections);
}
protected RelationshipExpander newExpander(RelationshipType[] types,
          Map<String, Direction> directions) {
     return new DefaultExpander(types, directions);
}

任务

应用提取方法以提高可读性、可测试性、可维护性、可重用性、模块化、内聚性、低耦合性和一致性。

始终返回一个完整且可编译的方法(Java 17)。

在内部执行中间步骤:

  • 首先,分析每个方法并识别超出阈值的方法:
    • 代码行数(LOC)> 15
    • 语句数(NOM)> 10
    • 圈复杂度(CC)> 10
  • 对于每个符合条件的方法,识别可以提取到单独方法中的代码块。
  • 至少提取一个具有描述性名称的新方法。
  • 仅输出单个java代码块内的重构代码。
  • 不要移除原始方法中的任何功能。
  • 在每个新方法上方添加一行注释,描述其用途。

待重构的代码:

现在,评估所有高复杂度的方法,并使用提取方法进行重构。