psychic stalker
The Deuteragonist
Disconsented
Can somebody explain to me why people use single letters for variable names, I've always been under the impression that it is good practice the have the name relate to its purpose.
My theory is that it's probably a practice carried over from learning algebra. I've never seen any algebra textbook use more than a single character to denote a variable.
In mathematics, there are conventions: For example, "𝑥" is typically used as a placeholder for a general unknown quantity. "𝑑𝑥" is not a variable, but rather denotation of a derivative, as in Calculus. "𝑖" is used to denote the constant √-1 or, along with "𝑗" and "𝑘", to denote a variable subscript. "𝐴" is almost always used to denote a set. And so on.
In programs, "i" is frequently used to denote an iterator of some kind. "x", "y", and "z" usually denote 3D spatial coordinates. ... But little else is common. When someone uses a single-character variable name in a context that is not purely mathematical, particularly in matching the values described by a well-known equation, they're being stupid and lazy.
Well yeah. I guess I should've been clearer: I was referring specifically to middle/high-school algebra word problems found in textbooks, where the given solution would almost always use single letters to represent unknown variables. (Stupidly easy) Example: Alfred has twice as many marbles as Betty. Betty and Alfred together have 27 marbles. How many marbles does Alfred have? Solution: Let a (or x) denote the number of marbles Alfred has, and b (or y) denote the number of variables Betty has... a chosen, of course, because Alfred, and b, because Betty.
There is some purpose to it. After all, the concatenation of two variables is taken to denote multiplication, i.e. xy = x × y, and no one wants to use the times sign so familiar to arithmetic due to its resemblance to the commonly-used "x" and association with the vector cross product. You may see a subscript or two on occasion, but really, I think I've only seen those in Olympiad-level training materials and physics books, and there is usually no way to "legitimately" use a subscript in a programming context (i.e., a2 won't have the same effect).
Unfortunately, people who have been used to the idea of a variable being a single character in algebra may be a bit shocked at the use of multiple-character variable names in code, and may take a while to adjust fully. I wouldn't be so quick to write these people off as "stupid and lazy"; I'd rather think of them as victims of prevailing pedagogical/notational conventions.