• Home   /  
  • Archive by category "1"

Parameter Assignment Sonar Violation 23123a

Refactoring Remove Assignments to Parameters


Some value is assigned to a parameter inside method's body.


Use a local variable instead of a parameter.


int discount(int inputVal, int quantity) { if (inputVal > 50) { inputVal -= 2; } //... }


int discount(int inputVal, int quantity) { int result = inputVal; if (inputVal > 50) { result -= 2; } //... }


int Discount(int inputVal, int quantity) { if (inputVal > 50) { inputVal -= 2; } //... }


int Discount(int inputVal, int quantity) { int result = inputVal; if (inputVal > 50) { result -= 2; } //... }


function discount($inputVal, $quantity) { if ($inputVal > 50) $inputVal -= 2; ...


function discount($inputVal, $quantity) { $result = $inputVal; if ($inputVal > 50) $result -= 2; ...


def discount(inputVal, quantity): if inputVal > 50: inputVal -= 2 #...


def discount(inputVal, quantity): result = inputVal if inputVal > 50: result -= 2 #...

Why Refactor

The reasons for this refactoring are the same as for Split Temporary Variable, but in this case we are dealing with a parameter, not a local variable.

First, if a parameter is passed via reference, then after the parameter value is changed inside the method, this value is passed to the argument that requested calling this method. Very often, this occurs accidentally and leads to unfortunate effects. Even if parameters are usually passed by value (and not by reference) in your programming language, this coding quirk may alienate those who are unaccustomed to it.

Second, multiple assignments of different values to a single parameter make it difficult for you to know what data should be contained in the parameter at any particular point in time. The problem worsens if your parameter and its contents are documented but the actual value is capable of differing from what is expected inside the method.


  • Each element of the program should be responsible for only one thing. This makes code maintenance much easier going forward, since you can safely replace code without any side effects.
  • This refactoring helps to extract repetitive code to separate methods.

How to Refactor

  1. Create a local variable and assign the initial value of your parameter.
  2. In all method code that follows this line, replace the parameter with your new local variable.

Tired of reading?

No wonder, it takes 7 hours to read all of the text we have here.

Try our interactive course on refactoring. It offers a less tedious approach to learning new stuff.

Let's see...

Может быть, Беккер был среди. Халохот внимательно оглядывал согнутые спины. Он приготовился стрелять метров с пятидесяти и продвигался. El cuerpo de Jesus, el pan del cielo. Молодой священник, причащавший Беккера, смотрел на него с неодобрением.

One thought on “Parameter Assignment Sonar Violation 23123a

Leave a comment

L'indirizzo email non verrà pubblicato. I campi obbligatori sono contrassegnati *