Saturday, July 9, 2016

Romance between Java and Javascript

I recently read about JavaPoly.js, a javascript library, I could not stop myself writing poem on it! It lets you write or use java code (as-is/compiled/jarred) in the browser itself, in place of Javascript !!! Hopefully it will finally fulfill need of strongly typed language for browser applications.

So here is the poem :) I hope I could express my feelings about the war against Javascript me and my team is facing for decades :)

JavaScript lets me sleep, while I write my code,
                But I am sleepless after release, in "firebug" mode!
Java showed me how object oriented is cool,
                When JavaScript tried same, reminds me of an ugly mule!
Why was JavaScript created? I think to kill,
                Java was created, to make coding a zeal!
Why can’t I use, my strong and steady java code,
                In that browser, which is plagued by JavaScript code!
But then I read about JavaPoly,
                I remember my school sweet-heart Dolly,
She talked “verbose”, but everything made sense,
                Now between front-end and back-end, there is no more a fence!  

Having burned my hands (actually finger-tips) while dealing with the whims of "loose" Javascript compared to Java, I used to wonder why was even Javascript devised ?  That too with the name so close to Java. Please ! I totally agree the phrase "Java is to Javascript as Ham is to Hamster" ! I heard Javascript was originally created  in just 10 days when Netscape was released in 90s. No wonder it has some crazy birth defects and even it's dad, Douglas Crockford had to take his child to "shrink" (psychiatrist) and hide those birth-defects and write a bible on only good side of Javascript !

Can It Be A Major Breakthrough ?

I thought JavaPoly is an incredible concept and if it works good, it would be a major breakthrough in front-end development! Imagine this line in your HTML and calling methods of somejava.jar from Javascript !

<script src="https://www.javapoly.com/javapoly.js"></script>

<script type="text/java" src="http://www.verient.com/somejava.jar"></script>

<script type="text/javascript"> com.your.package.doSomething()</script>


Now if you are really into it and got a quick hint what it can do, you will surely appreciate that poem came to my dumb poet mind !

As usual when you dig bit deeper, you always find that all beautiful things have foundation of something very robust on which these cute aromatic plants like JavaPoly grow. Here, there is Doppio JVM. This is JVM written in Javascript ! JavaPoly is actually polyfill on top of the JVM (Polyfill out of the scope of this article)

All JVM Languages ! Not just Java.

I got so excited about the concept, I contacted project-lead John Vilk directly and got some more information. Since this is a pure JVM. It supports all JVM based languages, for example, Scala, Jython, Go, Clojure etc. Are you kidding ? I mean, imagine if this really works ! It will change total Javascript/HTML5 Ecosystem !

I believe too flexible nature of Javascript plus combination of HTML5/CSS3 features and need for Native development, have given rise of just too many patterns giving rise to too many Javascript frameworks. It has also given birth (and deaths) to many strongly typed Javascript based languages, called "compile-to-javascript". But it didn't seem to work except little bit for CoffeScript and now TypeScript. Remember Dart by Google ? When was it's funeral ? All these approaches brought the annoying feeling of "please, no more new language to learn". But with DoppioJVM the new language is nothing but our best old friend called "Java".

This is JVM written in javascript, to be specific, in TypeScript (just like typical JVM is written in C) which you include in your HTML just like any other js. And now you are free to write Java code instead of Javascript. 

Don't Remind Me Of Those Applets ! 

I understand readers might still ask question "you mean, you dont need to download jvm on the machine?" The answer is of course not ! Don't you remember ugly days of Applets development ? Why applets failed miserably ? The idiotic installation issues of Sun JVM on user's machine ! And because of that, the "Mad King" ruler called "Flash" reigning for decade in browser productivity ? But the king was always outsider (plug-in) and finally was defeated and ousted by real world Silicon Valley king with name "Steve Jobs"! 

So defeat of "Flash" was one more reason browser development companies like Google/FireFox and apple itself, started focusing on HTML5 and Javascript. 

Advantages and Of Course, Disadvantages 

The obvious advantages of this appraoch of JVM written in Javascript,

  1. "Finally, the final" strongly typed language for complex browser applications
  2. Real Multi-threading
  3. Familiar File I/O which use browser storage
  4. Familiar Network I/O using browser Websockets
Of course, performance still looks like major hurdle in getting ahead of the game. I tried few java classes/code in the shell provided on DoppioJVM's site, it still has a way to go as far as performance is concerned.

15 years back, I had heard somebody saying he was into "Object Oriented Javascript", I laughed at that time, felt like the guy was just trying to use the buzzword "Object oriented". But then later, when I realized power of Javascript in Object Oriented, I was shocked and was impressed. I did start to respect JavaScript as very powerful language. But still as a Java professional, was not happy with the way Object Oriented was being achieved. I understand there is ES6 coming which is very close again to Java and will make things easier. But still the very big advantage of JavaPoly and Doppio is, reusing the vast work done by millions of developers in Java and reusing that. 

Java on server and JavaScript on browser, always sounded like good marriage but the fact that JavaScript just used "Java" as part of it's name, always sounded cheating too ! :) The JavaPoly/Doppio will revive the romance between the two, I hope, I wish and I really wish (did I say the same thing again again ? ) 

My tweets on similar topics