A coding challenge

Just finished writing an article called “Polymorphism Without Interfaces”. I challenge you to review and explain the code below:

for(var i:uint = 0; i < workers.length; i++) {

var p:* = workers[i][“increasePay”];

output.text+=p==undefined?”no luck”:p(5);


The person who writes the best explanation, will get a free copy of my e-book “Java programming for kids, parents and grandparents “. I’m sure people understand the syntax of this code snippet, but I’m looking for an explanation of WHY it’s written this way.

Good luck,


14 thoughts on “A coding challenge

  1. Although, “polymorphism without interfaces” sounds like a counter-oo concept, here’s an explanation of the code snippet:

    You iterate over some instances in the workers array and for each instance get the “increadePay” function reference. If the function is not available for a particular object instance, you append “no luck” to the output field, otherwise, you add whatever increasePay( 5 ) returns.


  2. I understand it. You have an array of objects which may or may not have an “increasePay” method. You want to loop through it and if the object has that method call it and add the result to the text field. If not, add “no luck” to the text field.

    As for WHY it’s written the way it is… obfuscation?

  3. The code iterates through an array (workers).
    For each object in the array, you are testing to see if it has a property called increasePay
    If the object has a property increasePay, you treat it as if it were a function (although this is not checked), and invoke it, and append the result to the string output.text. If it does not have the property increasePay, then you add “no luck” to the string output.text instead.

    So, the intent of the code (although without checking p to see if it is a Function object, I’d venture that the code is incomplete), is to check if the objects in the array support the function increasePay. If so, you take further action.

    Wouldn’t it be better to have the objects implement an interface defining increasePay, (e.g. IPayRiseable)? That way you could check:

    if(worker[i] is IPayRiseable)

    I like your blog, btw.


  4. I guess the reason you didn’t use an interface is because the title of your article is “Polymorphism Without Interfaces”. Doh!

    You still need to check that (p is Function) rather than if (p==undefined)… ideally you’d introspect to check that the signature of p is p(n:Number) as well. Pretty messy if you ask me!


  5. Aside from the typo (should be workers[i] instead of worker[i] I think) here’s my try:

    the approach illustrates how to invoke common methods on objects that don’t have to share a common type. So instead of checking each worker object against a type (or interface), the construct tries to resolve increasePay on the current object. As this dynamic construct either evaluates to undefined or a Function reference, the shorthand if/else is then used to invoke p (in this example a Function reference) with an argument and return its return value or just uses a default String return value if the method increasePay cannot be resolved.

    Of course, if the object implements a ”increasePay” field which is not a Function object but e.g. a String, then this causes a runtime error.

  6. //you could also write it like this
    //I’ll leave the implementation of implementsMethod to you
    for(var i:uint = 0; i

  7. I’ve emailed my e-book to Dirk ans spender already for providing more detailed answers than others.

    I’ll be happy to give away more copies of the e-book to those of you who will provide detailed explanations of why some particular syntax constructs were used in this example. Just analyze each line of this code and provide your reasoning/alternatives.

  8. last but not least, polymorphism with interfaces allow precompilation error checking.
    Does this old-school obfuscated code snippet tries to highlight bad code practices ? :p

  9. here is my guess if i’m not that much late.

    there is a base class Worker of lets say all workers in a company. there are some other classes extending Worker like Secretary, Engineer, Manager.. Let’s say just Manager and Engineer classes implement increasePay function.

    In such a situation we would probably keep an array of workers in the company for some batch processing needs(f.e paySalary..)
    these loop increasesPay of the workers that can increasePay

  10. It’s written that way because reducing lines of code was more important than readability For each worker, increase their pay by 5, if possible, appending the result to the output. Otherwise append “no luck” to the output. How about this version:

    for(var i:uint = 0; i

  11. View source on my previous comment to get the rest of it. No HTML escaping provided automatically, it seems, so my < got interpreted literally. To test……

    alert(“uh, oh!”);

Comments are closed.