Вопрос

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).

Это было полезно?

Решение

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.

Лицензировано под: CC-BY-SA с атрибуция
scroll top