Be careful when you name a method ’set’ because this could cause unexpected behaviour. I stumbled across this when trying to figure out why my setAll-method in the org.as2lib.data.holder.list.PriorityList-class was not working. All I did was looping over a list and calling the set-method for each element. But the set-method strangely never got invoked.
-
/**
-
* @author Simon Wacker
-
*/
-
class PriorityList {
-
public function set(index:Number, value):Void {
-
trace("About to invoke set(..).");
-
}
-
public function setAll(Void):Void {
-
set(0, "value");
-
}
-
}
As you can see there are only two methods ’setAll’ and ’set’. And the setAll-method calls the set-method. One would expect the output ‘About to invoke set(..).’ when using the following test code.
-
var list:PriorityList = new PriorityList();
-
list.setAll();
But what happens is actually - nothing. You must change the call to the set-method as follows.
-
/**
-
* @author Simon Wacker
-
*/
-
class PriorityList {
-
public function set(index:Number, value):Void {
-
trace("About to invoke set(..).");
-
}
-
public function setAll(Void):Void {
-
this.set(0, "value");
-
}
-
}
Note the added ‘this’.
So, be careful when naming your methods like a keyword.

7 Responses to “Naming a method ’set’”
“Be careful”? “could cause”? you “stumbled”?
- variable/method/type names 101 –
1. NEVER use KEYWORDS as var/method/type names
2. …
…
;P
Hi Emmanuel,
you are definitely right with your golden rule to never use keywords as var, method or type names. And I also normally stick to that rule.
But when working with data holders I like slim names like: set, get, put, push, .. . If it were in any other class that is not a data holder I would describe the method with a more meaningful name.
And because the compiler was not complaining about anything I didn’t see a problem here. Until, of course, I discovered the problem with internal method invocations (that’s what the article is about).
Greetings
Simon
i’m sticking my tongue out there at the end, so that that was meant to be more on the funny (duh) side of things, than pass on anything _really_ relevant… but actionscript is quite forgiving (and sometimes inconsistent), which is why bad practice can sometimes be OK (even desirable), and sometimes, just as well, it lead to AHA! moments like this one… for instance, i had always known you can hack your way around strict typing… (foo[’bar’] = fooBar)-type thing… and in fact, I use it every now and then, because I have found typecasts problematic… so that when I’m sure of what’s coming in, I just go with it and insert a hack… not saying that what you have here _is_ a hack… but you get my drift… but imagine how surprised I was, when chatting with darron schall, he dropped this one on me:
this[function(){}] = function() {trace(”hi”); }
function(){}(); //outputs hi
AHA!
Hi Emmanuel,
Things like that are definitely one of the reasons why I like Flash.
I arguably took it too seriously. Whatever, damn, Darrons sample, never thought of that one.
Cheers
Simon
This is where well designed old languages like lisp and smalltalk are superior - they feature a minimal set of keywords - in contrast to adhoc languages like lingo, where nearly everything is a keyord. Actionscript positions itself somewhere in between, mostly because of backwardcompatibility and Macromedias experiences with Director/Lingo. Every now and then i also stumble over “add” or “default”, but they give you an error message at least (even if it is misleading like it is the case with default). Anyway, I wonder if the compiler couldn’t derive the “keywordness” from the context.
The compiler could definitely distinguish between ‘keywordness’ depending on the context. For example in the case of ‘add’ and ‘default’. If someone still uses ‘add’ he does it like this: “’s1′ add ’s2′” and default like this: “default: doSomething()”. But a method call would in both cases have two brackets after the ‘keyword’, add() and default(). Thus it shouldn’t be that difficult to add support for this and still preserve backward-compatibility.
But luckily there are many new Flash versions to come and maybe a more intelligent compiler will be implemented then. There’s always much room for hope.
Cheers
Simon
btw. I always wanted to learn Smalltalk. The language just fascinates me.
For reference, here is a list of ECMA keywords:
http://www.mozilla.org/js/language/es4/core/lexer.html#reserved-word