Ask
20
@entropy_elif ·

Why can I not attach a description to a variable instead of encoding everything into its name?

There is a permanent tension between names that are descriptive and names that are short enough to read in an expression. A name carrying the unit, the source, the constraint and the meaning becomes unreadable inside any real line of code.

What I keep wanting is a way to attach a longer description to a variable — held by the language or the editor — so the short name stays in the expression and the full explanation is available on hover.

This does not seem technically difficult. Why does no mainstream language or editor do it?

4 answers Share
Report

Answering anonymously — a moderator will review it first.

  • @maintainer_mika · 3w ago

    Parts of this exist already, which is worth knowing before building anything.

    Documentation comments attached to declarations are shown on hover by most editors. That covers functions, types, fields and, in several languages, local variables too. It is not a language feature so much as a convention the tooling understands, and it does most of what you described.

    Types carry a lot of it. A value of type Metres rather than float carries the unit in a way that is checked rather than merely documented, and hovering shows the type. In languages with expressive type systems this absorbs a large fraction of what you would otherwise put in a name.

    So the honest answer to "why does no one do this" is that they partly do, through two mechanisms that arrived from different directions and are not usually presented as solving your problem.

    27
    Share
    Reply

    Answering anonymously — a moderator will review it first.

    Report
  • @architect_ayla · 3w ago

    The deeper answer, though, is that the problem is usually a symptom and the metadata would treat the symptom.

    When a variable needs a paragraph of explanation, that is normally a signal about the surrounding code rather than about naming. The usual causes:

    • The scope is too large. A variable living across two hundred lines needs a name that survives the journey. The same value inside a six-line function can be called total and be perfectly clear, because its entire life is visible.
    • The function does too much. Decompose it and each piece has few enough values that short names are unambiguous.
    • The concept has no name. If you keep wanting to write a description, you may have discovered a type that should exist. Turning it into one moves the explanation to a place where it is written once and reused.

    That is why the tooling never grew this feature. The people who most wanted it kept finding that fixing the structure removed the need.

    26
    Share
    Reply

    Answering anonymously — a moderator will review it first.

    Report
  • @js_runtime_jonas · 3w ago

    There is also a maintenance argument that is fatal in practice: detached descriptions rot faster than names.

    A misleading name is at least visible at every use site, so somebody eventually fixes it. A description that only appears on hover is invisible during ordinary reading, so when the meaning changes, nobody updates it. Within a year you have confident, authoritative, wrong documentation attached to a variable — which is worse than a terse name, because people believe it.

    This is the same reason comments explaining what code does are discouraged while comments explaining why are encouraged. The why does not change when you rename something; the what does.

    20
    Share
    Reply

    Answering anonymously — a moderator will review it first.

    Report
  • @ports_adapters_pia · 3w ago

    One practical technique that gets most of what you want today: put the explanation on the type or the function, not on the variable, and keep variable names short.

    Type and function documentation is read by the tooling, shown on hover at every use, and lives next to the thing it describes. Then rate inside a function whose signature and doc comment explain everything is perfectly readable, and you have not invented anything.

    12
    Share
    Reply

    Answering anonymously — a moderator will review it first.

    Report