Code Monkeyism

Programming is hard by Stephan Schmidt

Static Tools for JS please! DOJO please use them!

When browsing through the DOJO sourcecode I found this gem:

dojo.io.argsFromMap = function(map, encoding, last){
  var enc = /utf/i.test(encoding||"") ? encodeURIComponent : dojo.string.encodeAscii;
  var mapped = [];
  var control = new Object();
  for(var name in map){
    var domap = function(elt){
      var val = enc(name)+"="+enc(elt);
      mapped[(last == name) ? "push" : "unshift"](val);
    }
    if(!control[name]){
      var value = map[name];
      // FIXME: should be isArrayLike?
      if (dojo.lang.isArray(value)){
        dojo.lang.forEach(value, domap);
      }else{
        domap(value);
      }
    }
  }
  return mapped.join("&");
}

Not only is it disturbing to find FIXME tags in released code, but also to find method call magic with push and unshift, which is hard to read. But the best part is the control variable. It’s declared, but as far as I can see it’s read but never
written.

As DOJO does unit testing, this code shows that JUnit tests are not sufficient for quality assurance. What Javascript needs are static checker tools, either in the IDE (hopefully IDEA will find such bugs in the future as it does in Java) or as a standalone tool like PMD, FindBugs or Checkstyle.

About the author: Stephan Schmidt is currently a team manager at ImmobilienScout24 in Berlin. Stephan has been working as a head of development and CTO. He has used a lot of different technologies in the last 20 years including Java, Rails and Python. Stephans main field of interest is maintainablity and productivity in software development. Want to know more? All views are only his own.

If you did like this article but you don't want to subscribe to new articles with your reader, you can follow me on Twitter or subscribe to new posts with your email:

Comments

Steve

Will be interesting if the DOJO guys pick this up. Good catch and a very interesting issue, how well can static checking be done on Dynamically typed languages.

I guess things like un-used variables could be checked..

stephan

Update: 2008, looking for a new project into Dojo, the code quality is the same.

Update 2: 2008, IDEA does support static checks for Javascript, e.g. would have found the unused variable.

Leave a Reply