3 ms·
Another common convention is to select a suitable letter: for types with no particular meaning, go T, U, V, &c.; for types with meaning, see if there’s a better
by chrismorgan 6y ago
Another common convention is to select a suitable letter: for types with no particular meaning, go T, U, V, &c.; for types with meaning, see if there’s a better letter, e.g. Map<K, V> for a key-value map.
Rust convention is to mostly use this style of single-letter generics, but not to shy away from using longer names like regular type where useful (but not to prefix them like TFoo).
- underwater 6y agoIt seems most coders agree that variables, functions, files, columns, classes, etc. should have unambiguous, explicit, names. Writing `m[k] = v;` would not pass many code reviews. I don't see what makes generic types any different.
- another-dave 6y agoContext matters too though — As a general rule "i" is a terrible name for a variable but in a simple unnested loop it's expected. All these shorthands (e.g. K,V = Key,Value) have to be learned so should be used sparingly but in some places I think having shorthands for things like generics & indices allow boilerplate code to "get out of the way" of the business logic. You expend less mental energy trying to keep track of "i" (as you know it's a simple loop counter) than "index". And reducing cognitive load is the point of having descriptive names in the first place.