Avoid reassigning parameters

Metadata

ID: java-best-practices/avoid-reassigning-parameters

Language: Java

Severity: Warning

Category: Code Style

Description

Avoid reassigning values to method parameters as it can make the code harder to understand. Typically, parameter values are expected to remain unchanged throughout the method’s execution, and any reassignment might be not be noticed by other developers.

We consider it acceptable to reassign parameters in small functions, smaller than 20 lines. Otherwise, consider using temporary local variables with clear naming to enhance code readability.

Non-Compliant Code Examples

public class Person {
  private void greet(String name) {
    System.println("this is a long method");
    System.println("this is a very long method");
    name = name.trim(); // reassigning parameter value
    System.println("this is a long method");
    System.println("this is a long method");
    System.println("this is a long method");
    System.println("this is a long method");
    System.println("this is a long method");
    System.println("this is a long method");
    System.println("this is a long method");
    System.println("this is a long method");
    System.println("this is a long method");
    System.println("this is a long method");
    System.println("this is a long method");
    System.println("this is a long method");
    System.println("this is a long method");
    System.println("this is a long method");
    System.println("this is a long method");
    System.println("this is a long method");
    System.println("this is a long method");
    System.println("this is a long method");
    System.println("this is a long method");
    System.println("this is a long method");
    System.println("this is a long method");
    System.println("this is a long method");
    System.println("this is a long method");
    System.println("this is a long method");
    System.println("this is a long method");
    System.println("this is a long method");
    System.println("this is a long method");
    System.println("this is a long method");
  }
}

Compliant Code Examples

public class Main {
    public static void subscribeResult(
        @Advice.Enter final int callDepth,
        @Advice.Origin final Method method,
        @Advice.FieldValue("bucket") final String bucket,
        @Advice.Return(readOnly = false) Observable result) {
      if (callDepth > 0) {
        return;
      }
      CallDepthThreadLocalMap.reset(CouchbaseCluster.class);

      result = Observable.create(new CouchbaseOnSubscribe(result, method, bucket));
    }
}
public class Main {
    private static void addStrSegment(List<LogProbe.Segment> segments, String str) {
        str = Strings.replace(str, "{{", "{");
        str = Strings.replace(str, "}}", "}");
        segments.add(new LogProbe.Segment(str));
    }
}
public class Person {
  private void greet(String name) {
    String trimmedName = name.trim();
  }
}
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis

PREVIEWING: may/unit-testing