Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

This is a web application based on actionscript 3.

I have all of my modules enumerated in a class, but I never know which modules actually come from database.

Let's say I have 20 modules in my application, but the user only has access to 10 of them. All I get are 10 strings from database (the module names) whenever the user clicks a module this string goes through the twenty else if statements until it matches the enumerated value and then it is displayed on screen.

Is there a way to optimize this?

The modules are not flex modules per say, they are just lazy instantiated components.

share|improve this question
add comment

1 Answer

up vote 6 down vote accepted

You've got a key and you're trying to match it to a particular module using ifs, this is a key-value problem which should be solved by a dictionary or hashtable. Not familiar with Actionscript, but it must have dictionaries or hashtables. Does it have polymorphism or is it even strongly typed? Ideally you can put the databases string key into the dictionary as the key, and the associated module in as the value, but this may require the modules use a base type if Actionscript is strongly typed.

share|improve this answer
I have very little experience with hashtables outside of actionscript. It's equivalent is the Dictionary class, which I'm actually using in this factory. Thing is, the modules need to be instantiated first in order to get them later from the dictionary object. In order to get rid of the if statements I have to instantiate the 20 modules inside the dictionary, to simply get the value using the module name as key, and that is not an option. Oh, and the dictionary is not strongly typed. It's dynamic. –  overmann Dec 24 '12 at 16:37
@overmann this is exactly the correct approach. have 20 hard-coded lines that add each module and it's associated string to the dictionary at startup of the whole app, something like modulesDictionary.Add("storeModule", new StoreModule()); modulesDictionary.Add("mailModule", new MailModule()); It's a declarative approach, you are defining the associations, then when you need them you request them. Somewhat dangerous because the hard-coded keys need to be certain to match what's in the DB, this can be tested at startup by querying the DB to ensure all keys match and erroring early if not. –  Jimmy Hoffa Dec 24 '12 at 16:45
@overmann don't optimize prematurely, if having all the modules loaded poses neither security nor performance risk, don't worry about it. Optimize them away if having them loaded at run time causes insecurity or if your testing actually shows performance degradation from doing so. –  Jimmy Hoffa Dec 24 '12 at 16:54
@overmann: It should be relatively straight-forward to implement lazy-loading, thus addressing your concern. This does not significantly change Jimmy's answer. Before performing such an optimization, I would first ask how expensive it is to load those modules, and how likely the user is to not load a particular module. It is probably unnecessary. –  Brian Dec 24 '12 at 16:57
@overmann in that case my preference is the functional approach, I do not know if actionscript has anonymous functions or lambdas etc, but I would make your dictionary be the string key with the value being a function(closure) that does the instantiation and returns the module. Alternatively lots of if/else-ifs can be a sign you want a case but I prefer the declarative approach of using associative arrays(dictionaries, hashtables, what-have-you) –  Jimmy Hoffa Dec 24 '12 at 17:06
show 3 more comments

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.