As an Agile Coach, Miško is responsible for teaching his co-workers to maintain the highest level of automated testing culture, allowing frequent releases of applications with high quality. He is very involved in Open Source community and an author of several open source projects. Recently his interest in Test Driven Developement turned into http://TestabilityExplorer.org with which he hopes will change the testing culture of the open source community. Misko is a DZone MVB and is not an employee of DZone and has posted 38 posts at DZone. You can read more from them at their website. View Full User Profile

Move Over Java, I Have Fallen in Love With JavaScript

04.08.2010
| 8053 views |
  • submit to reddit

I spent the past year developing <angular/> in JavaScript (client) and Ruby on the server, and I have totally changed my opinion of dynamic languages, and I wanted to share some thought with you.

Tests are a must

Compilers are great at telling you that you have miss-typed something or that you assumptions about classes are wrong, but so is executing the code. So if all of my code is already exercised by the tests, than why do I need to have additional check of the compiler. Compile does not make the need for tests to go away, but the tests do make the need for compiler to go away. So if you have close to 100% coverage than you don’t need compiler to tell you that you mistyped something.

To get close to 100% coverage, you need to do tests first, and it has to be second nature to you. I run all of my JavaScript tests on every save across most browser using JSTestDriver and all my tests execute under a second. This instant feedback on every save is as good as having Eclipse underline your code that you made a mistake, and so I really don’t miss the compiler.

I can see how a dynamic language can be a nightmare if you don’t have tests.

Tests are easier to write

Since there are is no compiler to harrass you about types, it is very easy to fake out dependencies. Are you expecting a complicated class, but you know that in your test you are only exercising one method which should return a constant? Piece of cake! Create a nameless object with a single property which has the method and you are done.

Typing is a lot of typing

Typing as the name implies, is just that a lot of typing.

Java:

List<Book> books = new ArrayList<Book>();

JavaScript:

 var books = [];

Look how much longer java line is and how much shorter JavaScript is. Is there any information missing? Both say that I have a list of Books, but one says it by convention (variable names ending in ’s’ are arrays of the name) where as the other is explicit and makes me repeat myself by typing book three times and List twice. (Tests, prove that my code works, no need for compiler.)

Code Density

Code Density in dynamic languages are phenomenal! All of the endless casting, coercing and repetition in declaration simply is not there resulting in very dense code. But, I am master Eclipse jedi and  can generate all of that verbosity with few keystrokes so typing is not an issue! True, but reading/compression is! 10 lines of code are easier to understand than 100, and in JavaScript those 10 lines often do more than 100 in Java. (To start with 100 lines of code does not fit on my screen at once.) Turns out JavaScript is still wordy and CoffeeScript is even better!

Functions as first class citizens & Classes with single methods are functions

Having functions as first class citizens which can be passed around is awesome! See Execution in the Kingdom of Nouns! Functions are verbs, and you need both, verbs and nouns to make a sentence.

Recently I got an email from someone pointing out that doing proper Single Responsibility Principle, often resulted in classes which had exactly one method. Odd? Well, actually, it is a function in disguise.

Function closure == Constructor injection

If Class with one method is a function, than dependency injection for that class is the functions closure. Where the function is created specifies what are its dependencies and its visibility, nicely solving the dependency injection problem for functions.

JavaScript is so simple

I am constantly in awe, just how simple JavaScript really is! I can explain all of the rules of JavaScript to you in few hours, something which I can not do with Java. And yet, I feel like JavaScript is so much more powerful than Java because it has functions as first class citizens.

Everything is still true

All of the things which I have talked about in my blog is still true. Dependency Injection is a must and JavaScript’s dynamic nature does not make go away. Anyone who claims that DI is only for Java and that his language somehow makes it exempt form the rules of managing your dependencies, needs to lear more about DI. Similarly global state is a bad idea, and JavaScript is extra bad here, as global is the default and you have to do extra typing to get propers scoping, same goes to functions and their default affinity to global state. Bad JavaScript, Bad! Separation of wiring from logic still holds true and no amount monkey patching, will keep you sane in the long run.

Where did the inheritance go?

I have written 10,000’s of lines of JavaScript and I am yet to come across a need for inheritance. Where did it go? Proper use of inheritance == polymorphic behavior but one can get that with duck typing just by implementing the right kind of methods.

Scaling the Team

I have heard this many times before: I like (insert your dynamic language here) for the first 1000 lines than I miss my types. Well, I have written 10,000’s of lines and I don’t miss the compiler one bit, quite the opposite I have learned to loath when I am back in Java and the compiler gets in the way. So why is my experience different? One word: Tests! I am very serious about testing, and so are my team mates! As long as you have tests, lack of compiler is not a problem and the code scales just fine across more team members. But argument can be made that it scales better:

  1. Less code to maintain, write and understand
  2. If you can’t understand it, just rewrite it, after all its just couple of hundred lines, how long can it take you?

JavaScript on the Server

Developing web-applications means that you have to be expert in both Java and JavaScript, but lets fix that problem by moving JavaScript to the server. Node.js is a perfect example of that, and I love it.

Non-blocking API

Non-blocking API are a stroke of Genius! In short it makes it impossible to write slow tests. After all how can you make the tests slow if all method calls return immediately? That is the reason why I can execute 400 test in under 500ms an every save.

From http://misko.hevery.com/2010/04/07/move-over-java-i-have-fallen-in-love-with-javascript

Published at DZone with permission of Misko Hevery, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Clure Rogre replied on Thu, 2010/04/08 - 4:18am

Isn't this 7 days late?

Jacek Furmankiewicz replied on Thu, 2010/04/08 - 8:27am

Well, this is the opposite experience of the Google Maps team (that did all of it in Javascript).

They had so much issues with large Javascript projects not scaling across teams that for Google Wave they decided to go for GWT (good old Java) instead.So take this article with a grain of salt.

BTW, I am not against dynamic languages if they offer a worthy benefit. For our team, after we seriously played with Python Django and its admin interface, something like JSF2 just seemed brutal.

I can do in 10 lines of Python code what would take me hundreds in GWT or JSF2. For typical portal CRUD applications, Django is light years ahead of anything else in terms of productivity. So far ahead that it was actually worth adding a new language like Python to our Java team.

 

Chris Roberts replied on Thu, 2010/04/08 - 11:01am

Angular looks like a cool project, but I noticed a typo on the homepage:  You wanted "third-party sites such AS blogs," not "...such AD blogs."  Sorry to post that here, but I didn't see a way of emailing you from the site itself.

Ricky Clarkson replied on Fri, 2010/04/09 - 4:16am

Statically-typed languages don't have to be verbose like Java is. Consider C#: var list = new List(); or Haskell: list = [] Java is good in many ways, but is frankly awful in terms of verbosity. I agree completely about first-class functions.

Tim O'farrell replied on Fri, 2010/04/09 - 4:27am

As somebody who writes a lot of java and javascript, I have to say I totally disagree with this post. I feel that there are definitely more things wrong with javascript than with java, but that strong typing can be advantageous in cases where you have extensive test coverage.

With regard to understandability, take your example:

var books = [];

Is this an array of book objects? Book names? Id numbers for books from a database? When reading this for the first time, I have to guess, whereas with your java example java it is implicitly obvious.

Then there is the issues with the 'this' pointer, which can really trip up noobs (and occasionally even experienced developers). Try explaining to an unexperienced developer why the this pointer for a function may change depending on how a function is called, and you will undoubtedly see them create a defensive copy of it in the parent scope, leading to the next issue...

...memory management being by reference counting, meaning that you have to explicity deallocate circular references or you will get memory leaks (not an issue with small applications, but this can rapidly become more serious on larger app.)

Granted that the domain for javascript is smaller, but when it comes to explaining core concepts I sincerely doubt that explaining these issues would be easier than explaining the rules for the java language.

 In closing - I feel the way forward is in strongly types less verbose langauges (Groovy being my current favourite), not javascript.

Fab Mars replied on Sun, 2010/04/11 - 9:43am

I totally disagree with that article, for ~ the same reasons as the comment above.

The first thing that comes to mind, when you're not the kind to have fun on some sample project alone in your room, is that JS code is amazingly hard to bugfix and maintain. With mid-size and big web applications (where 99.99999% of the JS code worldwide is) and even with specific libs, the time and pain dealt with making something a) functional b) reliable c) secure, is such that it should definitely stop anyone from coding in JS.

With hindsight I'm so thankful GWT, Richfaces or other frameworks doing the JS for you do exist. They're here and used for a reason, believe me.

If you're working with a team, mainly the fact the language isn't typed leads to all sorts of crazy chaotic situations. Even with competent people. Even with tests. No matter how much your "processes" are enforced it will screw up, and even sooner than you think.

Should I even mention memory management issues on top of that ?

Furthermore, JS on the server really doesn't allow you to do what Java or C# allow you. It will make you feel limited and frustrated.

 

I'd only recommend JavaScript for very specific websites where frameworks like GWT don't allow you to do all you want. Example: chiptune.com ...which was coded by a single person, figure why.

Apart from that, and with all due respect for the article's author, I consider JS as a masochist thing.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.