collint25
collint25

Reputation: 311

"Local variable is redundant" using Java

Why is the following giving me a "local variable is redundant error"?

public double depreciationAmount() {
    double depreciationAmount = (cost * percentDepreciated);
    return depreciationAmount;
}

Upvotes: 26

Views: 45464

Answers (5)

Stephen C
Stephen C

Reputation: 718886

Why is the following giving me a "local variable is redundant error"?

Because you can trivially write this without using a local variable.

public double depreciationAmount() {
    return cost * percentDepreciated;
}

Hence the local variable is deemed to be unnecessary / redundant by the checker.


However, I surmise that this is not a compiler error. It might be a compiler warning, or more likely it is a style checker or bug checker warning. It is something you could ignore without any risk to the correctness of your code ... as written.

Also, I would predict that once that the code has been JIT compiled (by a modern Hotspot JIT compiler ...) there would be no performance difference between the two versions.


I won't attempt to address the issue as to whether the warning is appropriate1. If you feel it is inappropriate, then "Local variable is redundant" using Java explains how to suppress it.


1 - Except to say that it is too much to expect current generation style checkers to know when so-called explaining variables are needed. First you'd need to get a statistically significant2 group of developers to agree on measurable3 criteria for when the variables are needed, and when they aren't.
2 - Yea, I know. Abuse of terminology.
3 - They must be measurable, and there needs to be consensus on what the thresholds should be if this is to be implemented by a checker.

Upvotes: 36

P_M
P_M

Reputation: 2942

Why is the following giving me a "local variable is redundant error"?

I believe this message is wrong. Your depreciationAmount variable assignment is totally fine. Moreover, I always prefer this kind of assignment before return, because it helps to avoid confusion while debugging.

In this example, the getValue() method returns an expression result, instead of assigning the expression result to a variable.

Enter image description here

Now when I use debugger watch, to know the result of an expression, I got a confusion. My program ends with the wrong result and debugger watch values are inconsistent. It would be easy to avoid this, if I would have a variable assigned before the returning expression:

Integer value = 1 + getCounter();
return value;

instead of:

return 1 + getCounter();

Now I can put a breakpoint at the return statement and know what was the result of the expression, before it was returned. Also I do not need the expression in the watch any more, and code will be executed correctly while debugging.

Upvotes: 3

Sithum Dilshan
Sithum Dilshan

Reputation: 51

In computer programming, redundant code is source code or compiled code in a computer program that is unnecessary. In the above code, you can simply return:

(cost * percentDepreciated)

Upvotes: 0

dramzy
dramzy

Reputation: 1429

You only use the value of percentDepreciated to return it when you could have just done return (cost * percentDepreciated).

Upvotes: 1

seekingStillness
seekingStillness

Reputation: 5093

Although not the case here, if having a redundant local variable is desired (I've had one time where this was the case - without getting into specifics), here's how to suppress this specific warning.

@SuppressWarnings("UnnecessaryLocalVariable")
public double depreciationAmount() {
    double depreciationAmount = (cost * percentDepreciated);
    return depreciationAmount;
}

Upvotes: 23

Related Questions