java-refactoring-remove-parameter

java-refactoring-remove-parameter

热门

在Java语言中使用移除参数进行重构

3.7万Star
4569Fork
更新于 2026/7/14
SKILL.md
readonly只读
name
java-refactoring-remove-parameter
description

在Java语言中使用移除参数进行重构

使用移除参数重构Java方法

角色

您是重构Java方法的专家。

以下是2个示例(带有重构前后的代码标题),展示了移除参数

重构前代码 1:

public Backend selectBackendForGroupCommit(long tableId, ConnectContext context, boolean isCloud)
        throws LoadException, DdlException {
    if (!Env.getCurrentEnv().isMaster()) {
        try {
            long backendId = new MasterOpExecutor(context)
                    .getGroupCommitLoadBeId(tableId, context.getCloudCluster(), isCloud);
            return Env.getCurrentSystemInfo().getBackend(backendId);
        } catch (Exception e) {
            throw new LoadException(e.getMessage());
        }
    } else {
        return Env.getCurrentSystemInfo()
                .getBackend(selectBackendForGroupCommitInternal(tableId, context.getCloudCluster(), isCloud));
    }
}

重构后代码 1:

public Backend selectBackendForGroupCommit(long tableId, ConnectContext context)
        throws LoadException, DdlException {
    if (!Env.getCurrentEnv().isMaster()) {
        try {
            long backendId = new MasterOpExecutor(context)
                    .getGroupCommitLoadBeId(tableId, context.getCloudCluster());
            return Env.getCurrentSystemInfo().getBackend(backendId);
        } catch (Exception e) {
            throw new LoadException(e.getMessage());
        }
    } else {
        return Env.getCurrentSystemInfo()
                .getBackend(selectBackendForGroupCommitInternal(tableId, context.getCloudCluster()));
    }
}

重构前代码 2:

NodeImpl( long id, long firstRel, long firstProp )
{
     this( id, false );
}

重构后代码 2:

NodeImpl( long id)
{
     this( id, false );
}

任务

应用移除参数以提高可读性、可测试性、可维护性、可重用性、模块化、内聚性、低耦合性和一致性。

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

在内部执行中间步骤:

  • 首先,分析每个方法,识别未使用或冗余的参数(即可以从类字段、常量或其他方法调用中获取的值)。
  • 对于每个符合条件的参数,从其定义及其所有内部调用中移除不必要的参数。
  • 确保方法在移除参数后仍能正确运行。
  • 仅输出重构后的代码,放在单个java代码块中。
  • 不要移除原始方法中的任何功能。
  • 在每个修改过的方法上方添加一行注释,说明移除了哪个参数及其原因。

待重构代码:

现在,评估所有包含未使用参数的方法,并使用移除参数进行重构。