Is the use of one-letter variables encouraged in Java? In code snippets or tutorials, you often see them. I cannot imagine using them is encouraged because it makes the code relatively harder to read and I never see them being used in other programming languages!
4 Answers
Properly naming things is hard. Very hard. If you look at it the other way, you can also take this to mean that properly named things are important. (Otherwise, why would you have spent the effort naming it?)
But, sometimes, the names of things just aren't important. That's why we have stuff like anonymous functions ("lambdas"), for example: because sometimes it just isn't worth it naming stuff.
There are a lot of examples, where single letter (or very short) variable names are appropriate:
i,j,k,lfor loop indiceskandvfor the key and value in a mapnfor a number (e.g. inMath.abs(n))a,b,cfor arbitrary objects (e.g. inmax(a, b))efor the element in a genericfor eachloopffor the function in a higher-order functionpfor the predicate function in a filterT,T1,T2, … for type variablesEfor type variables representing the element type of a collectionRfor a type variable representing the result type of a functionexfor the exception in acatchclauseopfor the operation in a map or fold- appending the letter
sto indicate the plural, i.e. a collection (e.g.nsfor a collection of numbers,xsandysfor two arbitrary collections of generic objects)
I never see them being used in other programming languages!
They are very common in pretty much every language I know (and likely also in those I don't know.) Haskell, F#, ML, Ruby, Python, Perl, PHP, C#, Java, Scala, Groovy, Boo, Nemerle, D, Go, C++, C, you name it.
- 104,619
If your loop does nothing but use a variable for counting
for(int i = 0; i < 10; i++) {
System.out.println("Stop it! I really mean it!!");
}
then yes, this is the best name you could use. Anything longer cannot possibly make the semantics any more obvious, but takes much longer to read.
If the variable is used inside the loop, a meaningful name can be useful.
for(int door = 0; door < 3; door++) {
int reward = gauge(door);
if(reward > max) {
max = reward;
best = door;
}
}
If your variable is used method-wide, its name should be longer; if it's used class-wide, its name had better be totally self-explanatory, otherwise it will almost certainly decrease the clarity of your code.
In short, the bigger the scope of the variable, the longer its name must be.
- 110,899
Reading the code in Kilian's answer, I don't think "there is a loop, and there is a variable, and it has type int, and it has a name, and the name is i, and it is initialised to 0, ...". I just think "loop..." where the three dots stand for unimportant details that I don't even think about. In my programmer's mind, that variable doesn't really exist, it is just an artefact that I have to type, like the for (;;) {} that makes it a loop.
Since that variable doesn't even exist in my mind, why would I give it a name, beyond what is absolutely necessary?
- 49,096
If you are referring to the letters i,j,k as indices within a loop, that is a common practice in Java and other programming languages. Though there its probably better to use a for each style loop, such as
for(User user: users) {
doSomethingWithTheUser(user);
}
instead of
for(int i=0; i<users.size(); i++) {
doSomethingWithTheUser(users.get(i));
}
In Java that style of looping was only introduced fairly recently. That may be why you are noticing it more for Java, as looping over the indices used to be the most common way to loop over an array or list.
There are a few other cases where single letters may be appropriate, such as when implementing a mathematical function where single letters such as n, x, or y, may already be common. But in general, no, name your variables something meaningful.
- 101