Advertisement

Open Mike: Prefixes

This Cyber Monday Tuts+ courses will be reduced to just $3 (usually $15). Don't miss out.

To underscore, or not to underscore? This is Open Mike, a series of discussion posts to throw the cat amongst the pigeons. These posts are all about you — we want to hear your opinions, ideas, and thoughts. Due to popular demand, this Open Mike includes a poll to help us analyze opinion, so let's hear what you have to say about prefixes.


Do You Put Underscores Before Variables?

AS3 developers often use underscores as prefixes for private or protected variables in a class:

private var _playerID:String;

Some coders find this a useful way to tell at a glance whether the var is available to other classes; others deride it as useless and ugly. Whose side are you on?



How About Method Arguments and Interfaces? What Else?

Other common prefixes are a_ for method arguments (or p_ for parameters), and I for interfaces.

public function takeDamage(a_damage:Number):void
{
	_damage-=a_damage;
}
package
{
	public interface IPlayerCharacter
	{
		function takeDamage(a_damage:Number):void;
	}
}

The "I for interface" prefix is pretty common, but argument prefixes are rarer.



Any others you use often? Let us know in the comments.


Hungarian Notation: Yea or Nay?

If those didn't bring out strong opinions, I've got two words for you: Hungarian notation.

No, I'm not talking about Systems Hungarian, where the data type of the object is encoded into a prefix of the name:

var intCounter:int = 0;
var boolBestScore:Boolean = false;
var nHeight:Number = 120;
var btnOK:SimpleButton = new Button();

No-one's still using that, are they? With a decent IDE you can tell the data type of any variable easily; there's little need to have that information in the name of the variable.

I'm talking about Apps Hungarian, where the purpose of the object is encoded into a prefix of the name:

var dHeight:Number = newHeight - oldHeight;	//d for 'difference'
var cChildren:int;	//c for 'count'
var degAngle:Number;	//deg for 'degrees'
var radAngle:Number;	//rad for 'radians'

(Check out Joel Spolsky's excellent article Making Wrong Code Look Wrong for more information on this.)

Do you use any form of Hungarian notation, or is it completely unnecessary in an OOP language like AS3?


Advertisement