-2

I have been reading the source code of some Java library classes, specifically CompletableFuture. I noticed that the authors are making extensive use of cryptic (single-letter) variables in the code, which makes the code arguably unreadable and difficult to track.

Is there a specific reason/rationale behind this approach ? (since the official Java naming conventions suggest semantically-rich variables naming, avoiding single letter variables, except in specific contexts, such as for loops)


I found this related question, but I think it's not a duplicate, since I am talking about the standard libraries of Java (not snippets or tutorials), which should ideally be optimal in terms of maintainability and readability.


I also think that answers to these questions are not highly opinion-based, because they have to be backed up with data in order to be solid. For instance, @Jorg's answer below is backed up by a very thorough explanation, showing how single-letter variables might be part of the domain model and instead be intent-revealing to experienced engineers (especially for non-public, internal components).

  • 1
    Possible duplicate of Is the use of one-letter variables encouraged? – gnat Feb 3 '17 at 16:53
  • Are you reading the actual source, or reading the output from a disassembler? – whatsisname Feb 3 '17 at 17:23
  • @whatsisname The real code (careful – huge website) uses single letter names for local variables. I find it mostly OK (“small names for small scopes”), especially since the code is superbly documented. But I'd absolutely flag some parts during a code review. – amon Feb 3 '17 at 17:27
  • 2
    I agree, this is not a duplicate of that question. However, it is not answerable by anyone on this site unless said answerer happens to be a former Sun employee who wrote the code to begin with. – user22815 Feb 3 '17 at 17:36
  • 1
    @Dimos we get this question on a regular basis - not every day, but often enough - "why does this code in some standard library published by vendor or open-source group X violate some quality standard I was taught?" The answer is always the same: 1. ask the people who wrote it, because your guess is as good as mine and 2.a. my guess is because we all have deadlines to meet and sometimes we rush and quality is not as good as it could be or 2.b. just because someone is writing standard library code does not mean they are a good programmer. – user22815 Feb 3 '17 at 17:45
5

Identifiers should describe the semantics of the things they are identifying in the domain language such that they can be easily grasped by domain experts. I am not an expert in the domain of low-level concurrent standard library API implementation on the HotSpot JVM, so I cannot say authoritatively whether or not those variable names make sense.

There are a few I can comment on, however:

  • fn is a widely-used abbreviation of "function".
  • It is common in functional programming to simply call an entity of an unknown type x and a collection of such entities xs (i.e. "exes" with a plural-"s" to indicate a collection). If it is a value that has the type of a type variable, then it will often be named after that type variable, i.e. the two arguments of a function of type (A, B) → C will often be called a and b. You can see that in some instances here:

    • t is an unknown value of the unknown type T,
    • u is an unknown value of the unknown type U,
    • tr is r casted to T.
  • ex is a standard name for an Exception object in Java.

  • e is an Executor.
  • r is the result.
  • I believe p is a promise.

In general, coming up with names in highly abstract, general code can be challenging, because, well, it's highly abstract and general: it is supposed to work in many different contexts with many different things of many different types, completely independent of any particular domain.

  • Thanks for your answer, very insightful! So, it seems few-letter variables might sometimes improve readability indeed, due to being close in a specific domain model (thus easily understandable by experts) and also due to referring to abstract notions. – Dimos Feb 3 '17 at 18:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.