I've had a couple of discussions with a co-worker about the use of single letter variable names in certain circumstances inside our codebase, at which we both disagree.
He favours more verbose naming convention for these, and I do not.
There are three scenarios in my opinion where I use single letter variable names:
- Loops -
i
for(int i = 0; i < 10; i++) { ... }
- Lambda expressions in C# -
x/y/z
:.Where(x => x == 5)
- Exceptions -
e
:try { ... } catch(ExceptionType e) { /* usage of 'e' */ }
These are the only scenarios where I would use it, and I obviously use more verbose naming conventions elsewhere.
My colleague put forward the following arguments for exceptions and loops:
i
- it doesn't mean anything.e
- it's the most common letter in the English language. If you wanted to search the solution for exceptions, you'd find lots of undesired instances ofe
.
I accept these arguments, but have retorts that, if one does not know what i
means in a for loop, then they probably shouldn't be a programmer. It's a very common term for loops and exceptions, as is e
. I have also mentioned that, if one wanted, they could search for catch
in the case of the exception.
I realise that this is subjective, but then, one could argue that coding standards are just that - opinions, albeit opinions by academics.
I would be happy either way, and will forward the results to him, but would rather that we (our company) continue to use a single coding standard, rather than have two developers with different opinions on what to use.
ex
was the idiomatic name for an exception variable. – Cody Gray - on strike Apr 27 '11 at 11:13x
,y
,z
for coordinates, for instance, and if I'm computing something using a mathematical formula I'll store the intermediate results in variables that match what's used in the formula. – user13278 Apr 27 '11 at 13:03t
for interpolators, orp
for pixels. When a project has 18,000 lines of code usingp
for a pixel value everywhere, I see absolutely no need to usepixel_value
instead. – sam hocevar Apr 27 '11 at 16:19i
by itself looks to much likel
and1
. After about the 20th time of debugging to find it was an error between those three I stopped usingi
andl
. – Hogan Apr 27 '11 at 17:27ex
for exceptions ande
for event arguments. – Peter Olson Apr 27 '11 at 22:23template <typename T, typename U, int N> ...
. – David Hammen Jun 02 '14 at 15:47f
forfruit
, which is also a single letter convention where the first character is used to abbreviate with. – jxramos Oct 11 '19 at 20:37i
is a standard variable denoting the iteration index of the object being enumerated. – Dan Atkinson Feb 19 '24 at 10:19