the blog for developers

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.

You can leave a Reply here. Of course, you should follow me on twitter here.

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.

About the author: Stephan Schmidt is head of development at brands4friends. He has more than 15 years of internet technology experience and 10 years experience in agile. He was head of development, consultant and CTO and is a speaker, author and blog writer. He specializes in organizing and optimizing software development helping companies by increasing productivity with lean software development and agile methodologies. Want to know more? All views are only his own.
Leave a reply.

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

What people wrote somewhere else:

Additional comments powered by BackType

Guide to CodeMonkeyism

Over the last 4 years I wrote many articles on this blog. To make it easier for you to find the relevant ones, I've organized them into topics.

Top 10

6 reasons why my VC funded startup did fail

Go Ahead: Next Generation Java Programming Style

Java Interview questions: Write a String Reverser

The dark side of NoSQL

7 Bad Signs not to Work for a Software Company or Startup

Is Java dead?

Scala vs. Clojure

Never, never, never use String in Java

No future for functional programming in 2008 – Scala, F# and Nu

Clojure vs Scala, Part 2

Job Seeker

Another Good (Java) Interview Question

7 Bad Signs not to Work for a Software Company or Startup

Java Interview questions: Write a String Reverser (and use Recursion!)

Java Interview questions: Multiple Inheritance

As a Manager: What I value in developers

Top 10 Tips (+1) to Get a Pay Raise

Java Developer

Is Java Dead?

Go Ahead: Next Generation Java Programming Style

Be careful with magical code

All variables in Java must be final

Never, never, never use String in Java

Bending Java: More readable code with methods that do nothing?

Startup/CTO

Development Dream Teams

6 reasons why my VC funded startup did fail

American vs. European style of Software Development

12 Things to Reduce Your Lead Time and Time to Market

The high cost of overhead when working in parallel

Essential storage tradeoff: Simple Reads vs. Simple Writes

Agilist

What Developers Need to Know About Agile

5 Practices Better to Change in Your Scrum Implementation

Scrum is not about engineering practices

ScrumMaster and ZenMaster: The joke of certification

What is Trans-Scrum?