3

I find myself sometimes writing code that looks like this with Java Generics:

/**Class description
 *@param <K> Key to '.....'
 public class Mappy<K>{
   ///class methods, fields, etc....
 }

Sometimes using single-character names has caused slowdowns when months later I return to code and have to keep scrolling up to remember what "T" & "E" are. But last I checked, Oracle's official guideline was single-character names and I've never seen a java programmer not do it.

In C#, using TDescription is part of the official style guidelines, similar to how Google & others use Ixxxx for interfaces. But I still see one-letter names in production code & APIs for C#. I've heard it is similar in C++. In Haskell & Ocaml, especially Haskell, you use 'a' or 'b' as a generic parameter in your function signature (forget if the compiler/interpreter forces this or if they can be multi-letter).

I'm just asking this 'question' to see how y'all do it: do you stick with single-letter names in your generics/templates/etc..., do you have a convention like Txxx, do you give them full-fledged names (and does that confuse co-workers), or do you do something else?

This is very similar to Breaking java generics naming convention? (which I found via google). Instead of poking that question, I just wanted to gather some modern opinions (see if there's been a style coup in the pass two and a half years).

Edit 1:

Probably the reason this question came up is that a few days ago I made a pledge to dump the variable 'i'. Too many times using the quick & dirty loop variable 'i' has caused issues in nested loops & refactoring so I decided to go with only full-fledged names.

Community
  • 1
  • 1
Lan
  • 1,206
  • 1
  • 11
  • 26
  • 2
    I'd say if single letter names become too confusing, maybe it means that the whole class is a bit too confusing. – Kayaman Nov 04 '13 at 14:13
  • 1
    I can't say in terms of a industry accepted standard, but _generally_ speaking from my personal use, if my generic class only has 1 generic parameter, and there is no real _meaning_ behind it (e.g., `List`) then I just use `T`. If however my class has two or more parameters, or they have some special named meaning, then I'll give them a name like `TDescription`, or for key/value pairs perhaps `TKey`, `TValue`. In the past, I've tried dropping the `T` prefix for descriptive types, but I found it just kept confusing me, making my first instinct reading the code to consider it a defined type. – Chris Sinclair Nov 04 '13 at 14:16
  • Typically, except when doing some advanced meta-programming, generics will have relatively few parameters to keep track of. `T` is common to be used for an instance of some "type", while `N` is common for size-based values. There isn't really a need to change `template` to `template`. – Zac Howland Nov 04 '13 at 14:16
  • @Kayaman, I work on medium (?) sized projects of 50-300 classes usually. Say every seventh class is a generic but the generics tend to exist in packages with other generics (say a utility package or the controller package in MVC). I may be working with and flipping through ten classes in one package (or forty in five to eight packages) where I or someone else months back decided to name all the generic parameters T, E, & S. So individually a class is simple but the macro picture is a bit diluted. – Lan Nov 04 '13 at 15:58

2 Answers2

4

Naming conventions exist as a tool to help you maintain readable code.

They are there to help you. They are not a rule.
There's a higher value to have easy to read - maintainable code than to blindly follow a naming convention.

Yochai Timmer
  • 48,127
  • 24
  • 147
  • 185
1

I use single-letter uppercase types in my generics when the type can be (almost) any type. Like with Map<K,V> etc.

However, when the type has more meaning than just ANY type, such as:

public class Table<Column extends Enum<Column> & Table.Columns> {
 ...
 public interface Columns {
  ...

I use a more appropriate name Column but retain the convention of the initial uppercase. I feel it is important to maintain brevity for types as you are likely to use it many times in the code. A single uppercase character is - you must admit - perfect brevity.

OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213