A redundant variable is one that exists exclusively to explain its value. I strongly believe that such a variable is not only pure noise but also evil, with a very negative effect on code readability. When we introduce a redundant variable, we intend to make our code cleaner and easier to read. In reality, though, we make it more verbose and difficult to understand. Without exception, any variable used only once is redundant and must be replaced with a value.
Here, variable fileName
is redundant:
String fileName = "test.txt";
print("Length is " + new File(fileName).length());
This code must look differently:
print("Length is " + new File("test.txt").length());
This example is very primitive, but I’m sure you’ve seen these redundant variables many times. We use them to “explain” the code—it’s not just a string literal "test.txt"
anymore but a fileName
. The code looks easier to understand, right? Not really.
Let’s dig into what “readability” of code is in the first place. I think this quality can be measured by the number of seconds I need to understand the code I’m looking at. The longer the timeframe, the lower the readability. Ideally, I want to understand any piece of code in a few seconds. If I can’t, that’s a failure of its author.
Remember, if I don’t understand you, it’s your fault.
An increasing length of code degrades readability. So the more variable names I have to remember while reading through it, the longer it takes to digest the code and come to a conclusion about its purpose and effects. I think four is the maximum number of variables I can comfortably keep in my head without thinking about quitting the job.
New variables make the code longer because they need extra lines to be declared. And they make the code more complex because its reader has to remember more names.
Thus, when you want to introduce a new variable to explain what your code is doing, stop and think. Your code is too complex and long in the first place! Refactor it using new objects or methods but not variables. Make your code shorter by moving pieces of it into new classes or private methods.
Moreover, I think that in perfectly designed methods, you won’t need any variables aside from method arguments.
More variables with longer self-explainable names make code... #elegantobjects
— Yegor Bugayenko (@yegor256) December 16, 2018
P.S. “Avoid temporary variables. The fewer temporary variables in a program, the less chance there is that one will not be properly initialized, or that one will be altered unexpectedly before it is used.”—The Elements of Programming Style, Brian Kernighan and Phillip James Plauger, 1974.
P.P.S. You may find this study by Raymond Buse et al. relevant: they found out that the biggest negative impact on Java code readability has the number of identifiers used in the code.