Search code examples
javapmd

Avoid reassigning parameters


I’m getting PMD red colored violation

Avoid reassigning parameters such as 'bankRequest'

This is my method

   @Override
public BankDTO loadTariff(BankDTO bankRequest, int[] executionLevels) {
    double[] fee = null;
    for (int level : executionLevels) {

        // Check the tariff availability from execution level one to .....
        fee = loadCokaAndBankFee(bankRequest,level);

        if (fee != null) { // if fee found reload the bank request with new
                            // amount
            bankRequest = reloadBankRequest(bankRequest, fee);
            break; // no need to go for any other level deep level cover //
                    // here.
        } // if tariff not found use the esb provided amounts
    }

    return bankRequest;
}

Could someone explain what wrong with this code. If I ignore it what is the impact.


Solution

  • You should avoid re assigning variables for the reasons already given. Anyway,instead of assigning a new variable consider just returning from the loop when finding the correct value. Thiswould also make "break" redundant.