3 ms·
Next question: what other single character variable names do people use? I've always wanted to work up a master list somewhere. Speaking to my own usage, I use
by philwelch 16y ago
Next question: what other single character variable names do people use? I've always wanted to work up a master list somewhere.
Speaking to my own usage, I use i and j for indices, x, y, and z for Cartesian coordinates, n for "number", k and v for "key" and "value" when iterating through a hash, and r for a variable I'm using for a return value later on.
- nailer 16y agoI use a description of what I;m iterating over. If it's a list of people, I use 'person'. If the keys are session IDs, I use 'sessionid'. I do this because it's easier for me and I think I'm being nicer to the next person to come along.
- philwelch 16y agoI do that if I'm doing collection.each in Ruby or something, but if I'm in C or even if I'm doing collection.each_with_index, I use "i" for the index.
- rogerclark 16y agomaster list = the alphabet
- philwelch 16y agoYes, I was hoping for a list of what each letter is used for.
- jules 16y agoA t for a type variable. I sometimes name variables to the data structure that they are holding, e.g. p for pair, h for heap. The names x, y and z for things that have a generic type, and xs ("axes"), ys ("whys"), zs ("zeds") for collections of general things (and xss for collections of collections of things). For example, an F# pairing heap: type 't heap = Empty | Heap of 't * 't heap list let findmin (Heap(x,_)) = x let merge h1 h2 = match (h1,h2) with | Empty,h | h,Empty -> h | Heap(x,h1s),Heap(y,h2s) -> if x < y then Heap(x,h2::h1s) else Heap(y,h1::h2s) let deletemin (Heap(_,hs)) = List.fold merge Empty hs let insert h x = merge h (Heap(x,[])) First line: t for type. Second line: x for something of generic type t. Third line: h1 and h2 for heap. Fifth line: h1s and h2s for lists of heaps corresponding to h1 and h2. This saves time coming up with names, and it ends up being more readable if you're used to it. Of course these short names will only be used for variables that are used internally in a function, not for function names and such.