← Back to context

Comment by adrianmsmith

15 hours ago

I never understood the convention of using single letter names for generic parameters. I guess this started in C++ and every language has copied that convention.

I think that code would be a lot easier to read if the types were called IN and OUT or In and Out or TIn and TOut or something like that.

I often use whole word for type annotation, when I can find meaningfull ones. I just type them in all caps to stay close to the convention.

I guess the single letter thing is laziness for a part. It's not simple to find words that represent the abstract idea behind the generic type without narrowing the possibilities. For array function, the Key Value from the sibling comment work but for more complex use case, it get complicated.

Swift generics tend to idiomatically use longer names, like Element or View or Content.

  • I’ve always done that in my typescript code bases too, and I’ve never regretted it

    • Lambdas usually have short variable names because the scope is small, typically half a line. And that is fine, even optimal.

Completely agree and I personally name generic type parameters as I would name types and parameters. It helps a lot.

For maps, a convention is to use K and V for key, respectively Value.

I think that’s best as you’ll soon learn the “single-character capital letter ⇒ generic parameter” convention

Im pretty sure it came from the MLs, where you usually have a/b/c instrad of the T,U etc combo.

I dont find it confusing, as its pretty clear that it only an placeholder.

In generics the name usually does not matter or is REALLY hard to name so that it makes sense.

More specifically in Go where you have interfaces, concrete types and generics.

  • Fairly sure it would predate even that, and go all the way to lambda calculus, and predicate logic before that, and that's where my knowledge stops and somebody else can tell us where the current conventions around variables in logic and mathematics come from.

  • In ocaml (and I assume SML) it helps that the generic types have a `'` before them, so

        val map : ('a Box) -> ('a -> 'b) -> 'b Box

What could be more idiomatic than:

for (int i=0; i<10; i++) { printf(”%d\n”, i); }

(Or the very similar Go equivalent)

If you having a hard time parsing that, due to the short variable name, i.e. if it’s a huge cognitive load for you, I suggest you switch career, b/c the IT industry is obviously not a good fit.

With that said, Go is explicit with suggesting short variable names for small scopes, and long variable names for bigger scopes. This a good practice in all languages.

In C# this is the convention.