Recently, I've been trying Intellij IDEA to program Java, and one thing I've noticed is the large amount of completely useless warnings the compiler gives me out of the box. Just like AI code completion, these things waste my attention and make me lose focus on what I'm doing because the warnings appear as you type instead of appearing only after you press the build button. Yes, this variable isn't used by anything yet, because I haven't written the code to use it yet. Why would you "warn" me about it when I just wrote the code? Why not only warn me about things that actually matter? Among these warnings, one of which has particularly annoyed me has been "Local Variable ... is Redundant."
The redundancy warning appears in Intellij IDEA when you write code like this:
public Foo getFoo() {
var foo = createFoo();
return foo;
}
The "fix" rewrites the code like this:
public Foo getFoo() {
return createFoo();
}
In other words, the compiler warns that the variable foo is redundant because you only use foo to store the value of createFoo, which you return immediately after without doing anything to the variable. Considering that it gives you a "fix" for it that implies that this sort of redundancy is bad and that you should "fix" it every time it appears. After all, what could possibly be the point of having a variable named foo just to return it immediately after?
Personally, that's just how I write code, and I have my reasons for this.
First of all, if I need to debug this later, I can just set a breakpoint on the return line to inspect what the result of createFoo is.
Second, it's easier to add comments explaining why you're getting a value in some way if you use a variable, although to be honest I rarely do this.
Third, it makes lines shorter, because I really dislike having a function call that has a dozen function calls to get its parameters. One reason I dislike this is that it makes debugging this very annoying: go inner, go outer, go inner again, go outer.
Fourth, if the compiler is doing its job optimizing this, which should be extremely easy to optimize considering it can issue a warning about it, then it makes no difference as far as performance is concerned if I use a redundant variable or not, because the compiler can just remove the redundancy during the build step.
By contrast, what is the benefit of NOT using a redundant variable? As far as I'm concerned, there is no benefit. It's only downsides. Of course, I'm not the only person in the world. Other people have their own opinions in the matter.
- https://www.yegor256.com/2015/09/01/redundant-variables-are-evil.html (accessed 2025-06-02)
- https://www.reddit.com/r/PHP/comments/enoo4r/do_unnecessary_variables_matter_that_much/ (accessed 2025-06-02)