What's in a name?

Tue 01 April 2008
By stu

...or why "my" is not a valid prefix for a variable

While I'm greatful for examples like this J2Me GameCanvas example I'm not so happy about this bit of it:

/** Starts the canvas by firing up a thread
*/
public void start() {
Thread myThread = new Thread(this);

// Make sure we know we are running
running = true;
done = false;

// Start
myThread.start();
}

What's wrong ? Quite simply 'myThread'.  You see the 'my' prefix used in code all over the web - it's extraneous... extra noise, you may as well add 'the' to the beginning of every variable.

Variable naming is to a certain extent a matter of taste; a lot of people would say that it is worrying about nothing. The problem is, that most of the time spent with code is reading it, not writing it; as such we should try and be as succinct as possible 'my' adds absolutely nothing here.

In the example above it would be better to either leave out the 'my' or call the thread canvasThread.

Fuzzy

Where possible variable names should reflect what you want to use them for, if your naming is fuzzy then others will use them for things you did not intend and then your on the downward spiral...

This is the other crime in variable naming, over generalising... quite often you see variables called 'data' when the name of the type would do fine... usually it is only in IO based applications dealing with arbitrary data that the name is appropriate.

Of course, if you can't think what to name them, then don't pad them out as well, it's pointless.

My my my

A quick search on coders (excluding mysql) finds 301,040 hits for 'my*'.

That's a lot of noise... stop the madness: ban 'my' now... if microsoft can remove it from Vista, how difficult can it be?