18

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!

William
  • 351

4 Answers4

41

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, l for loop indices
  • k and v for the key and value in a map
  • n for a number (e.g. in Math.abs(n))
  • a, b, c for arbitrary objects (e.g. in max(a, b))
  • e for the element in a generic for each loop
  • f for the function in a higher-order function
  • p for the predicate function in a filter
  • T, T1, T2, … for type variables
  • E for type variables representing the element type of a collection
  • R for a type variable representing the result type of a function
  • ex for the exception in a catch clause
  • op for the operation in a map or fold
  • appending the letter s to indicate the plural, i.e. a collection (e.g. ns for a collection of numbers, xs and ys for 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.

Jörg W Mittag
  • 104,619
24

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.

Kilian Foth
  • 110,899
4

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?

gnasher729
  • 49,096
-1

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.

Nick
  • 101