Advertisement
  1. Code
  2. Open Mike

Open Mike: Prefixes

Scroll to top
Read Time: 2 min

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:

1
2
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.

1
2
public function takeDamage(a_damage:Number):void
3
{
4
	_damage-=a_damage;
5
}
1
2
package
3
{
4
	public interface IPlayerCharacter
5
	{
6
		function takeDamage(a_damage:Number):void;
7
	}
8
}

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:

1
2
var intCounter:int = 0;
3
var boolBestScore:Boolean = false;
4
var nHeight:Number = 120;
5
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:

1
2
var dHeight:Number = newHeight - oldHeight;	//d for 'difference'

3
var cChildren:int;	//c for 'count'

4
var degAngle:Number;	//deg for 'degrees'

5
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
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.