Coping with Life Without The Compiler

For developers who are used to compiled programming languages like Java, C#, or C++, Javascript's loosely-typed nature can be off-putting. Common complaints I've heard are

  1. it doesn't catch obvious errors like parameter type mismatch or misspelled names
  2. some popular libraries - like jQuery - silently ignore bad inputs rather than throwing exceptions/errors

Invariably, discussions turn into laments on why Javascript is so bad, and what we can do to improve the language, and that maybe we should all just use Coffeescript, or Dart, or Objective-J, etc, etc.

I have a different take on this, which is best expressed with a quote from the great Steve Jobs

You're holding it wrong.

Dynamic languages have different affordances than compiled languages, and their users tend to have different usage patterns. If you are programming Javascript the same way you were programming Java, chances are, you are missing out.

Compiled vs Dynamic (100% Unbiased)

Compiled languages are... compiled. To test your program, you have to first compile it, and then run it. Compiling is sometimes a lengthy process, but the good news is the compiler can find errors for you - using type checking for example, and, it will stop at the first error encountered, so that you don't have to wait through an otherwise lengthy compile. So, for compiled languages: the more mistakes the compiler can catch for you, the more pleasant life will be.

Dynamic languages on the other hand, are interpreted - there is no compile step. In order to check your program for mistakes, you have to run it. Although there is no compilation step, it is still true that the earlier you can catch your mistakes the better. A syntax error is better than a runtime error, and a runtime error that appears right away is far better than one that appears after several interactions with the UI. It turns out there's a shortcut to uncover your mistakes earlier - the interactive console.

The Interactive Console

The interactive console, a.k.a. REPL(read-eval-print-loop) allows you to execute small snippets of code and see their results quickly. It works very much like a command line shell for any unix or Windows: you type in a command, hit enter, it executes the command, prints out the result, and then you start over again.

When should you use the console? All the time. It's great for figuring out small bits of code that are tricky to get right, like regular expressions, or for examining the DOM on the page using the DOM API. It is also great for figuring out how to use a certain API in a third party library like jQuery.

Firebug has a Javascript console, and all other major browsers have one built-in as well. But seeing is believing, so here is a video of Firebug in action.

Conclusion

If you haven't already, integrating the JS console into your workflow will give you a big productivity boost. If you find yourself feeling frustrated that Javascript or jQuery is not catching errors earlier for you, you are probably not using the interactive console nearly enough. Use it. Use it a lot!

blog comments powered by Disqus