Skip to content
Home » Remove Assignments to Parameters

Remove Assignments to Parameters


“Remove Assignments to Parameters” is a type of refactoring that deals with eliminating assignments to the parameters of a method. The general rule of thumb is that parameters should be treated as constants, not variables that can be changed. Altering parameter values can make code more difficult to understand, and in some cases, it might lead to unexpected behavior.

“Remove Assignments to Parameters” refactoring is typically applied when:

  • The codebase is hard to understand because parameters are being assigned new values.
  • A parameter is both used as an input and modified as an output, making the method’s behavior confusing.

Example in Java

Before refactoring

public int calculateSum(int a, int b) {
    a = a + 10;
    b = b + 20;
    return a + b;

In the above example, the parameters a and b are being modified inside the method.

After refactoring

public int calculateSum(int a, int b) {
    int newA = a + 10;
    int newB = b + 20;
    return newA + newB;

In the refactored example, instead of modifying the parameters directly, we create new variables and modify those instead. This is much clearer and avoids any potential confusion or problems caused by modifying the input parameters directly. It also makes the code more predictable because the parameters retain their original values.

Redundant Implementation in Programming Languages

In some programming languages, function parameters are immutable by default, which means that they cannot be modified within the function. In such languages, this refactoring is not required. Here are a few examples:

  1. Python: Python treats function parameters as immutable. If you pass an argument to a function, the function creates a new reference to the object, so if you try to modify the object itself, it will create a new one and won’t affect the original object. This is true for immutable Python objects such as integers, strings, and tuples.
  2. C++ (for certain cases): C++ itself doesn’t make function parameters immutable by default, but you can use the const keyword to make sure the function does not change the values of parameters.
  3. Rust: In Rust, variables are immutable by default. If you want to make them mutable, you have to explicitly declare them as such.
  4. Kotlin: In Kotlin, the parameters of a function are val by default, which means they are read-only (or effectively “immutable”). Once a value is assigned to a parameter, it cannot be altered within that function. However, just like Java, when you pass an object to a function, its properties can be altered unless they are declared as val. The reference to the object itself, though, cannot be changed within the function.
  5. Haskell: In Haskell, all values are immutable by default. There is no way to mutate a variable once it’s been assigned a value, so function parameters are always immutable.
  6. Scala: Similar to Haskell, Scala promotes immutability and function parameters are by default immutable.