JavaScript Tooling – Coping with Lack of Tools for Programmers Used to Static Languages

javajavascriptproductivity

I have programmed pretty much exclusively in compiled languages, particularly Java, for most of my career. One of my favourite things about Java is how productive you can be, and how little code you actually have to write, when using tools like Eclipse.

You can:

  • Easily and automatically refactor your methods and classes
  • View instantly all the places where a method is invoked, or a constant is used (Open Call Hierarchy/Show References)
  • Static typing means you can use code completion to show all the parameters/functions available on an object
  • Control-click on a function/member/class name to go straight to its definition

All these facilities make me feel like the IDE is my best friend. Writing Java code and particularly understanding other peoples' programs becomes far easier.

However, I am being called on more and more to use Javascript, and my experience so far has been quite negative.

In particular:

  • No immediate way of finding a function's entry point
    (other than a plain text search, which may then result in a subsequent searches for methods further up the call hierarchy, after two or three of which you've forgotten where you started)

  • Parameters are passed in to functions, with no way of knowing what properties and functions are available on that parameter
    (other than actually running the program, navigating to the point at which the function is called, and using console.logs to output all the properties available)

  • Common usage of anonymous functions as callbacks, which frequently leads to a spaghetti of confusing code paths, that you can't navigate around quickly.

  • And sure, JSLint catches some errors before runtime, but even that's not as handy as having red wavy lines under your code directly in the browser.

The upshot is that you pretty much need to have the entire program in your head at all times. This massively increases the cognitive load for writing complex programs. And all this extra stuff to worry about leaves less room in my brain for actual creativity and problem solving.

Sure, it's faster to just throw an object together rather than write an entire formal class definition. But while programs may be slightly easier and quicker to write, in my experience they are far harder to read and debug.

My question is, how do other programmers cope with these issues? Javascript is clearly growing in popularity, and the blogs I read are about how productive people are being with it, rather than desperately trying to find solutions to these issues.

GWT allows you to write code for a Javascript environment in Java instead, but doesn't seem to be as widely used as I would expect; people actually seem to prefer Javascript for complex programs.

What am I missing?

Best Answer

The IDE-based niceties are not available* in a dynamic language such as javascript. You have to learn to do without them. You'll have to replace tool support with better design.

Use a module pattern -- either by hand, or with a tool like requirejs. Keep the modules small, so that you can reason about them easily.

Don't define as many types -- use anonymous objects created close to the point of call. Then you can look at the caller and the callee and know what's going on.

Try to avoid coupling your code to the DOM -- Try hard to limit the amount of DOM manipulation you do in your code. If you can pass in selectors or jQuery collections, do that rather than having your code know about the page structure.

*If you're using a popular library, you can get fake autocomplete, but it's more like "show all jquery methods" than like "what properties does this object have". It saves typing, but offers no guarantee of correctness.