Fwd: [dojo-contributors] [feedback wanted] Preferred Iteration forms

Sandro Magi smagi at naasking.homeip.net
Mon Apr 3 16:38:14 EDT 2006

Tom Trenka wrote:
> Right now the collections code has no dependencies whatsoever, and I'd
> prefer to keep it that way.  I also feel that writing a function to
> encompass something that's accomplished in two lines in other code is kind
> of a waste of time.

1. Dictionary.contains, Dictionary.clone, Dictionary.entry are all a 
single line of code. More lines of code does not imply usefulness. The 
question is: how often are those 3 lines of code likely to be repeated, 
and thus, how much typing will we be saving, not to mention download size.

2. Looking at the Dojo code at the moment, I see:
  a) DictionaryIterator (object with 2 methods & 5 properties: 33 lines 
of code)
  b) Dictionary.getValueList()  (8 lines of code)
  c) Dictionary.getKeyList()  (8 lines of code)
  d) Dictionary.getIterator()  (3 lines of code)
 Total: 52 LOC

Implementation of forEach for Dictionary:

this.forEach = function (callback) {
  dojo.lang.forEach(items, callback, true);

That's 52-3=49 lines of less code. If you still want getValueList() and 

this.getKeyList = function(){
  var arr = [];
  this.forEach(function(key) { arr.push(key); } );
  return arr;
this.getValueList = function(){
  var arr = [];
  this.forEach(function(key) { arr.push(items[key]); } );
  return arr;

52-13 = 39 lines of less code for this one object. Same deal with 
ArrayList and Iterator (though the savings are slightly less). A similar 
savings is available for all of the other collections if you adopt the 
same pattern. I'm not really seeing the downside of leveraging your 
existing code. Unless a dependency on dojo.lang.forEach is somehow a 


> I'm also not talking about adding new, untested functions; I'm asking
> because the iterator code in the Collections has been somewhat borken for a
> while, and I need to fix it (two or three bugs filed against it).  The
> imperative Iterator code has been there from the beginning, and the reason
> why it got borken in the first place is because I wrote it following one
> style, and someone tried to correct it to follow another.
> trt

More information about the dojo-contributors mailing list