

The world’s two worst variable names  - pavel_lishin
http://petdance.com/2012/04/the-worlds-two-worst-variable-names/

======
ef4
You can't talk about sensible naming without also talking about scope.

When a variable is only in scope for five lines, it's perfectly reasonable to
give it a really short name.

~~~
petdance
Nobody said anything against really short names.

~~~
pohl
You don't consider "data".length() == 4 to be short?

~~~
alex_c
Nobody said anything against really short names, the article argues against
non-descriptive variable names, whether short or long.

~~~
evincarofautumn
Indeed, provided you know the problem domain, the most descriptive name (xs,
x_rms) is almost always far shorter than the literal alternative
(horizontal_positions, quadratic_mean_of_horizontal_positions).

------
dcminter
For every rule an exception - data is a perfectly fine name for the scenario
where we don't _know_ what data the variable represents. For example in a
logging framework that has to loop over the contents of a collection and dump
it to the console then data or entry or something similarly vague would be a
perfectly reasonable variable name.

Then data2 might be a reasonable name in a unit test that checks to see if a
function can handle the traditional "1, 2, many" categories of failure.

Which is not to say that data and data2 are not, often, horrible names. But
the circumstances dictate (or excuse) all.

~~~
WiseWeasel
You could name it after the source of the data. The point the author was
making is it makes your code much more readable when you see the variable turn
up and you don't need to go back and see where it comes from to determine what
it is (ie you come accross a 'data.parameter < constant' condition or some
such). Even calling it log-data would be an improvement, as its source is
immediately obvious from the name.

~~~
eridius
Even the source is not always obvious. Cocoa has a class called NSData, which
is a container for arbitrary binary data (e.g. it basically holds a char* , a
length, and a bunch of methods for looking at this stuff). If I write a
function that needs to operate on an NSData instance, then I'm perfectly
justified in calling the variable 'data'. For example,

    
    
      NSData *calculateHash(NSData *data) {
          char result[CC_SHA1_DIGEST_LENGTH];
          CC_SHA1([data bytes], [data length], result);
          return [NSData dataWithBytes:result length:CC_SHA1_DIGEST_LENGTH];
      }

~~~
WiseWeasel
That's a good point. If in the scope of the variable, you're only concerned
with calculating its hash (or some similar function that is qualitatively
indifferent to it), and not testing or parsing it or making any specific use
out of it, then 'data' makes sense (though I'm guessing 'file' is arguably a
better descriptor of the data in this example, if indeed that's what you're
hoping to find in NSData, I get your point, and 'foo' might do just as well).
That has to be a fairly unusual case, though.

------
aerique
Are older blogs being regurgitated? In the past days I've read articles that
I'm sure are 90+% similar to ones I've read one or two years ago.

I'm on my phone currently so I can't really be bothered to find links to
support my case. Mostly curious whether others have got the same feeling.

~~~
mhb
Same guy, 8 years ago;
[http://www.oreillynet.com/onlamp/blog/2004/03/the_worlds_two...](http://www.oreillynet.com/onlamp/blog/2004/03/the_worlds_two_worst_variable.html)

~~~
petdance
Not only that, but that guy also linked back to the original and said it was a
modification of the previous post.

~~~
mhb
That guy put the reference to the original far enough down that people who
enjoyed the article the first time around may have had a strong enough feeling
of deja vu that they stopped reading before reaching it.

------
thought_alarm
I thought this was going to be about lParam and wParam.

~~~
cpeterso
How about "BURGERMASTER", the name of the root memory segment in Windows 1.0
memory manager, or "BOZOSLIVEHERE", the name of the winproc for the edit
control?

<http://www.kraigbrockschmidt.com/mm/Chapter06.htm#_ftn1>

[https://blogs.msdn.com/b/oldnewthing/archive/2003/10/15/5529...](https://blogs.msdn.com/b/oldnewthing/archive/2003/10/15/55296.aspx)

------
gruseom
Grep says the variable name "data" occurs about 80 times in the program I'm
working on.

I used to prefer descriptiveVariableNames, but as my programs have gotten
shorter, my variable names have gotten much shorter. I found that
longVariableNames were starting to overwhelm everything else and obscure the
shape of the code. They were still communicating theIntentOfTheVariable like
they always did, but in a way that felt less helpful and more like lexical
noise.

Code shape communicates meaning through a different channel than variable
names do. As code gets less verbose, its shape emerges more and more, that
channel becomes more and more informative, and one begins to use shorter names
so as not to drown it out.

This is very different than verbose programs written in verbose languages.
There, descriptiveVariableNames don't take up an unseemly amount of space
relative to everything else. The shape of the code is less meaningful because
you can't see the forest for the trees. And you're far more dependent on
intentionClarifyingNames to orient yourself in that forest.

This is why programs written in concise languages look absurdly cryptic to
people who haven't spent time in that language's world. The markers of meaning
they're used to relying on are missing, and their eyes haven't adjusted to the
meaning that is there. The program may have been crafted to maximize overall
meaning across several channels, but one needs time and practice to get what
those channels are. And the tradeoffs are probably different in each language.

------
NickPollard
I sometimes use data, but normally that's for a specific purpose - when I have
a function that takes a void* in order to fit to a generic function template,
which I then cast to something in every function of that type.

e.g.

    
    
      typedef void (*function_t)( void* );
    
      void someFunction( void* data ) {
        myStruct_s* myStruct = data;
        
        // do something with myStruct
        
      }

~~~
cpeterso
I would write something like:

    
    
      void someFunction( void* pvMyStruct ) {
        myStruct_s* myStruct = pvMyStruct;

~~~
blahedo
Please explain why this is better.

~~~
cpeterso
The name "pvMyStruct" implies intention and suggests to the reader that the
function parameter is a MyStruct pointer disguised as a void pointer. The name
"data" does not provide any hints.

------
telent
I think I agree with the general tone of the article, but his ordering is
upside down: "data" is bad, but "data2" is worse, not better.

------
aeurielesn

      $conn = mysql_connect(...);
      $res = mysql_query($conn);
      while($row = mysql_fetch_array($res)){...}
    

I think these are by far the three most common name conventions, _at least_
for beginners in PHP.

------
arethuza
Personally the worst variable name I have seen was in the line:

boolean alanIsABastard = true;

Where I am the Alan in question and this was written by someone who worked for
me. :-)

~~~
thebigshane
I am curious on the usage of this variable or was it never referenced
afterwards?

Perhaps the author sets it to false later symbolizing some sort of
transformation you went through or at least a change in his perception of you.

Perhaps it was in a "bipolar" loop:

    
    
       while (true):
          sleep(1000 * 60 * 24)
          alanIsABastard = !alanIsABastard

~~~
arethuza
I must admit that I never considered this point in any great depth.

Perhaps it should have been a constant (and it was Java):

    
    
         public static final boolean ALAN_IS_A_BASTARD = true;

------
gweinberg
You can do much worse than "data" for a variable name. A name that gives no
clue as to what the variable is isn't nearly as bad as one which gives a
misleading impression.

------
kenthorvath
Sorry, but my vote is for $foo and $bar, by far.

~~~
Achshar
but those are filler names. i believe they are rarely used in production.

~~~
joshuahedlund
Not likely:
[https://github.com/search?utf8=%E2%9C%93&q=%24foo&re...](https://github.com/search?utf8=%E2%9C%93&q=%24foo&repo=&langOverride=&start_value=1&type=Code&language=)

~~~
Achshar
Most of them are tests, unit tests or examples.. very few are actually being
used.

------
kittxkat

        > Of course it’s data! That’s what variables contain! That’s all they ever contain. It’s like if you were packing up your belongings in moving boxes, and on the side you labeled the box “matter.”
    

Nope, if he goes with that analogy that data contains data, the box should be
labeled with box, imho.

------
K2h
I have boxes in the garage labeled junk, and that is exactly what it is. I
agree a more meaningful name could be given, but its accurate. Same so with
data or other such nonsense variable names. I could give a better name, but
i'm busy trying to get my first prototype done!

------
kittxkat
Site is down for me, google cache link:
[http://webcache.googleusercontent.com/search?q=cache:http://...](http://webcache.googleusercontent.com/search?q=cache:http://petdance.com/2012/04/the-
worlds-two-worst-variable-names/)

------
ojbyrne
I think I've seen far worse. For example, I saw some code where the variables
were a, b, c, d, e, f... z, a1, b1, c1...z1, etc.

Never underestimate the willingness of programmers to obfuscate in the name of
(supposed) job security.

~~~
Achshar
maybe it was from some kind of code minimizer? like gwt renames variable names
to reduce file size when pushing for production.

~~~
ojbyrne
That would be a valid excuse. It was not the case.

------
alexchamberlain
$data is fine for the name of the data variable of a generic container.

Again, using $data2 to represent the square of $data is fine too.

I understand your point, but there are definitely exceptions.

------
Harkins
Huh, I've used nearly this example for years, except my two worst are "var"
and "var3" - with no var2, leaving the reader to wonder what happened to it.

------
mikedougherty
Negatively named bools bother me. e.g.:

    
    
      while notDone: { if condition() notDone = false; };

------
mgkimsal
In PHP, I sometimes giggle when I see $hit as a variable name, or when people
catch exceptions as $ex.

------
pavel_lishin
My personal favorite was always $hold, and its cousins, $hold1, $hold2 ...
$holdn

------
michaelochurch
"Metadata" can also be painful. One abstraction's metadata is another's "data
data".

For example, there was a structured store where each entity was a byte array
("blob") with certain searchable, indexed "metadata". (The "data data" was the
blob itself.) However, demands on the structure required certain additional
fields regarding the content of that "metadata" that were allowed to break the
contract of the original "metadata" and were therefore metadata on the
metadata.

I fell out of my chair, hoping the "kick" would bring me up a level. It
didn't. I wasn't dreaming.

------
tucif
I've seen one much worse: $stuff

------
hk_kh
char mander;

It intrigues me why this is not on the article

