The world's two worst variable names
oreillynet.com
oreillynet.com
class packFile { var name:String; var location:String; var downloadPriority:Int; var data:Array<Byte>; }
There may be a better name than data, but I can't think of one off the top of my head, and it's certainly not a bad one.
For example: int add(int operand1, int operand2) { return operand1+operand2; }
Personally, the worst variable name I've ever seen was "C". This was a vector of concentrations. Unfortunately, the language was Fortran-77-- where a "C" in column 1 indicates a comment line.
I'd personally vote for i and j
Mea Culpa: I've written my fair share of horrid code in PHP and Perl.
The clearest thing for me would be:
channel.open()
Which indicates to me that a channel is being opened.Rather than
i.open()
Which means nothing to me unless I find and read some other line.Personally, I much prefer the certainty of the current situation to the relative uncertainty of a free-for-all. That and for (counter = 0; counter < N; counter++) seems very longwinded.
You're right, I missed the 'indices' part of your original comment. Unfortunately I've worked at a couple of places where they're used in every for loop.
Runners up include value, bytes, and number.
FOR FOR = WHILE WHILE WHILE = FOR
Now THAT'S bad variable names. :)
Isn't there an example like this in the Dragon Book?
Yes, I know you're iterating over something.
Best case scenario: you named that something well so all I have to do in your great big for loop is scroll up, work out what you meant, and apply that to the code I care about, being mildly annoyed.
Worst case scenario: the something is also badly named and I have to open up a debugger and set a trace to work out whether the iterable is people, channels, cows, or dinosaurs.
Yes I know this is common behaviour. So are many other bad things.
Code is read much more than it's written and should be created for maximum comprehension. A simple one-line method call should be understandable without having to read any surrounding line.
>Best case scenario: ... all I have to do in your great big for loop is scroll up...
Once you have to scroll up, you are toast. I mean within a function, not within a loop.
There is a rule of thumb of how many things can the mind process at the same time: It is 7 plus-minus 2. You can treat the loop as one element in the function context and analyze the loop context at a different stack frame inside your head.
While working in a loop, you have to think about the list, the index and the element, so you are down to 4 more things you can do before "running out of memory". I think that's why the "for each" loop has been successful across many languages. I also think that that's why "i" is preferred over "index"; it is a known idiom that helps you think about the "i-th element" and saves one slot in your brain.
>Worst case scenario: the something is also badly named and I have to open up a debugger and set a trace to work out whether the iterable is people, channels, cows, or dinosaurs.
Eventually, anyone who has been around for a while inherits some abomination like that. It is not funny. However, no amount of coding standards prevent morons for doing their thing. I would not sacrifice an useful and well known artifact for the sake of idiot-proofing.
Anything postfixed with 2 is more shady, I agree, but the absolute worst variable names are the ones that say they are something they're not - such as a column named "tablename_key" - which is actually a foreign key, but not in "tablename".
Generally, I'd disagree, as does the article. If you can specify what kind of data it is, thats useful. customer_data, sample_data, etc...
The only time I'd say you might be right is if you are using it in a generic method for retrieving data, but even then I think result_data is better.
Data is the variable name you use when you don't know, or can't specify, what you're dealing with.
...seriously