views:

114

answers:

2

So in actionscript 3, instances of the Object class can be used an as associative array:

var doNotHaveSexWith:Object = new Object();
doNotHaveSexWith['mum'] = new Person(...);
doNotHaveSexWith['dad'] = new Person(...);
doNotHaveSexWith['dave'] = new Person(...);

Say I have some class, and one of it's members is a read only 'Object' which contains my collection of people.

I think code readability takes a major hit if I return this 'Object', as how would the programmer know what to do with it? The only way someone is going to know that it is a collection is if they read the code or the comments...

What's the best way to signal that an Object is a collection, rather than a simple object?

Options:

  • Create a dynamic class, simply extending from Object, called "AssociativeArray" or something, just so the code becomes more readable...

  • Use something like the AS3 Datastructures Library, though this seems like a bit of overkill.

  • Just append the word Collection to the end of the variable name?

For example:

var hotPeopleCollection:Object = new Object();
hotPeopleCollection['me'] = new Person(...); 
hotPeopleCollection['sandrasully'] = new Person(...);

What do you think?

Update: I've decided to go with a custom class extending Dictionary. This way I can wrap a sensible access function around the searching function: hasOwnProperty and give the class a meaningful name.

I chose Dictionary over Object for two reasons:

  1. Dictionary makes more intuitive sense for a collection
  2. Dictionary appears to perform at O(1) for searching. See this fairly informal dictionary vs array vs object performance benchmark
+2  A: 

Use a dictionary.

http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/flash/utils/Dictionary.html

You can still use a string for your key, or any object for that matter.

var dict:Dictionary = new Dictionary();
var obj:Object = new Object();
var key:Object = new Object();
key.toString = function() { return "key" }

dict[key] = "Letters";
obj["key"] = "Letters";

dict[key] == "Letters"; // true
obj["key"] == "Letters"; // true 
obj[key] == "Letters"; // true because key == "key" is true because key.toString == "key"
dict["key"] == "Letters"; // false because "key" === key is false
delete dict[key]; //removes the key

A lot of developer ( myself included ) will tell you: Never use an Object. You are basically blindfolding your compiler. Always either use a built in datatype or make your own. Now obviously you didn't know about dictionaries in this case, but as a general rule, if you think you want to use a plain old Object datatype, think again.

Update:

Another link you might find helpful:

http://www.gskinner.com/blog/archives/2006/07/as3_dictionary.html

Ryan Guill
Great, exactly what I need thanks. Is it just me or is AS3 lacking in data structures?
secoif
Yes and no. Flash player 10 brings vectors which is the only real data type that I have been missing. ArrayList is another thats coming soon ( I guess its in flex4 ) which will be nice too as an alternative to the heavyweight ArrayCollection. But for the most part anything else that I need is domain specific which means I need to write it anyway ;)
Ryan Guill
I'm not one of those developers, but I see your point. On the other hand, it's like saying "don't use array" because you don't know what's in it. An Object _is_ an associative array. It's just so flexible that everything inherits from it. Of course, there _is_ a performance cost sometimes because of the compiler blindfold you mention. It's all in the use.
Glenn
Well, I would say you should only put the same "type" of objects in an array. Even though you can put different kinds of data in the same array, surely you could see how that would be a bad idea. Although I would agree, it would be nice to know what is in an array. Thats where vectors in FP10 come in. Don't get me wrong, there are times where dynamic objects can be useful, but those times are few and far between.
Ryan Guill
A: 

I'll be contrary and spout the much-hated dynamic-type stance. Just use an Object. It is an associative array. The names of the variables and the context should make it clear enough. Don't make your code any more complicated than it needs to be. If you think it should be a class, make it a class. It it's a simple map, use an Object.

There's no reason to go overboard.

Glenn
The problem I am having is that accepting an Object as a parameter or returning an Object, gives no intuitive insight as to wtf is going on. My primary concern is readability and understanding.
secoif
You have the function name and the variable name. Getting passed an "ArrayCollection" doesn't tell me anything about the object either. You still don't know what's in it. Functional programmers will lean one way and OO programmers will lean another way. It's all preferences.
Glenn