Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Avoid reassigning parameters

Tags:

java

pmd

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.

like image 914
someone Avatar asked Oct 22 '25 20:10

someone


1 Answers

In your case, there's a parameter named bankRequest. Inside the method, you are assigning bankRequest a value.

By some, it is considered an ill approach to assign values to parameters within a method's body, as it is, at times, confusing. Some developers prefer always assuming that a parameter is never assigned any value during a method's run.

To avoid that, you can declare an alternative variable of type BankDTO:

BankDTO updatedRequest = bankRequest;
...
...
updatedRequest = reloadBankRequest(bankRequest, fee);
...
...
return updatedRequest;
like image 128
Isaac Avatar answered Oct 24 '25 09:10

Isaac



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!