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.

If you liked this post, subscribe to my free full RSS feed.
Filed under: Java

You can share this post!
Do you want to tell others about this article? Use the social bookmark icons to submit this artice to the service of your choice. Thanks.

Get free updates by email

If you did like this article you can get free updates with your RSS reader, you can follow me on Twitter or get free update to new posts by email. Enter your email:

 
About the author: Stephan has been working as a head of development and CTO. He has experiences in different technologies since 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.

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