PDA

View Full Version : trying to understand using numerals as variable names in objects. is it ok?



Shaedo
December 5th, 2009, 01:34 AM
EDIT I just found that Senocular did much the same thing so I guess that means it's ok...
http://www.senocular.com/flash/actionscript/?file=ActionScript_3.0/com/senocular/utils/KeyObject.as

I am curious about using numerals as variable names in objects.
I am not sure how to explain this but the code below shows what I mean:

var o:Object=new Object();
o['123']=true;// so is kinda an equivalent to 'o.123' ??
trace(o['123']);//true
//trace(o.123);//1084: Syntax error: expecting rightparen before .123.
o['abc']=true;
trace(o.abc);//true //works fine of course
trace(o['abc']);//trueso as long as I put my reference in square brackets [] is there any drawback from using numerals as variable names?

The reason I was wondering about this is because I am using a global static object and then assigning keyCodes to it eg:
private function KD(e:KeyboardEvent):void
{
Keys.KO[e.keyCode]=true;
} where Keys is my global class and KO is my object and I can just check say
if(Keys.KO['65']==true)trace('key a is down');and yes I know I could always concat with a letter eg

Keys.KO['k'+e.keyCode]=true;but I would still like to understand it all....

thank you for your consideration,
S.

Krilnon
December 5th, 2009, 02:54 AM
It's just fine to use them, in my opinion.

I think the reason for forbidding the first character of an identifier to be a number is a combination of convention and ease of scanner/parser creation. Language designers often want to limit the amount of lookahead necessary to classify a token or grammar element, but it could take quite a while (theoretically) to differentiate 45045904459405ab the identifier from 45045904459405 the number constant. If you require identifiers to start with a letter, $, or _, then you know the type of symbol after the first character instead of after (an unbounded-ish) n characters. Even if it might not be a problem for most programs, language designers seem to think a lot about running time in worst-case scenarios. And, like I said, it's probably more of a mix of that and convention. It seems like this would have been more of a problem 50 years ago than it is now.

You don't run into the same problem with using the array access syntax because you've already delimited the element as a string.


so as long as I put my reference in square brackets [] is there any drawback from using numerals as variable names?

Since you're using a plain Object, it's not like you'd get static type checking by using dot syntax either. Arguably you'd lose some clarity, but I don't think that it really applies in your case, since the other option that you're considering is placing some random(-ish) character in front of the number.

Edit: For convenience, you can drop the quotes because using array access notation for Objects results in the parameter being coerced into a String at some point anyway:
var o:Object = {};
o['55'] = 'hi';
trace(o[55]); // hi

Shaedo
December 5th, 2009, 03:19 AM
One of the many things I really like about your answers Krilnon, is ending up learning more than I set out to. This is a testament to both what you answer contains and how you explain things. Thank you!

≈ ≠ =
ah so very true in so many things.