Sunday, June 26, 2016

GWT

I like GWT. At Onedesk we used to have a big Flex web application with a Java backend. And then management decided that installing a plugin was too much of a barrier for our client, so they decided that we would re-do our entire client side in HTML/CSS/Javascript from scratch. Well, that was a big change since about 4 persons have been working on this app for about 5 years.

For choosing the new technology, we had 2 big concerns (among others):
  • we were all Java developers and we didn't know much about Javascript/HTML/CSS
  • our application is big (hundreds thousands of lines of code)
After that, the choice was pretty much obvious that GWT was on the top of the list. The "big application" factor was pretty much reducing our choice to GWT, Closure, and pure Javascript (with minification tools). So we went for GWT (version 2.4 at the time I think), and we haven't really been disappointed since. I'm spending about a third of my time on GWT, and I do like it.

The beast itself


GWT is a set of tools to produce an optimized HTML/CSS/Javascript web site. GWT take some Java code as input and gives you a web site (HTML/CSS/Javascript) as output.
The best part is that you can do that without having to do any HTML and Javascript (almost). And you can even debug with a very good Java debugger.

Here is an example of what GWT Java code look like (taken from the GWT web site):

public class StockWatcher implements EntryPoint {

  private VerticalPanel mainPanel = new VerticalPanel();
  private FlexTable stocksFlexTable = new FlexTable();
  private HorizontalPanel addPanel = new HorizontalPanel();
  private TextBox newSymbolTextBox = new TextBox();
  private Button addStockButton = new Button("Add");
  private Label lastUpdatedLabel = new Label();

  public void onModuleLoad() {
    // Create table for stock data.
    stocksFlexTable.setText(0, 0, "Symbol");
    stocksFlexTable.setText(0, 1, "Price");
    stocksFlexTable.setText(0, 2, "Change");
    stocksFlexTable.setText(0, 3, "Remove");

    // Assemble Add Stock panel.
    addPanel.add(newSymbolTextBox);
    addPanel.add(addStockButton);

    // Assemble Main panel.
    mainPanel.add(stocksFlexTable);
    mainPanel.add(addPanel);
    mainPanel.add(lastUpdatedLabel);

    // Associate the Main panel with the HTML host page.
    RootPanel.get("stockList").add(mainPanel);

    // Move cursor focus to the input box.
    newSymbolTextBox.setFocus(true);
  }
}

As you can see, this is pure Java code and really easy to understand.

The good


What I like about GWT:
  • Strongly type language: I can't empathize enough how good it is to use strongly type language (yeah I'm not a fan of Javascript). 2 days ago I had to move a class to a different package and rename it. It took about 10s for IntelliJ to do it, and after a quick 2s compile (just to be sure), I didn't even launch the app to check if anything was broken, I just committed the 130 files changed. I was that confident. And it felt great.
  • RPC: We have our client side and server side in Java, but there is the Internet in the middle. Fortunately GWT include RPC which take care of the communication between the client and the server without having to deal with the all serialization/deserialization shenanigan.
  • widgets: You can very easily create widgets and composite of widgets and reuse them everywhere. You can even define different appearances for your widgets. And you can also use library of widgets available out there (we use Sencha GXT, but there is also others).
  • Dev mode: you can directly debug your web site in the IDE with a great Java debugger. Yes this is a big deal to have a nice debugging experience. The GWT browser plugin allow you to run the Java code directly into your browser..
  • CSS: Managing CSS could be a pain in the ass. For a small web site there is no problem, but for large application it become difficult. There is some tools to help with that (CSS preprocessor) but in GWT, this is completely integrated into the framework and optimized.

The bad


What I don't like about GWT:
  • Super dev mode: SDM is an alternative to Dev mode. Since Google chrome doesn't allow the GWT plugin (wich make Dev mode work), thw GWT team had to find an alternative. This alternative is indead SDM. Instead of running your java code, the browser run the already transpiled Javascript code and you can debug it in your browser like any other web site. There is a source map to alow you to put break point in you source code. I'm not a fan of this replacment debugging tool... First of all, when you put a break point you get the javascript debugger (worst than the Java debugger). Then, finding you data indie a simple HashSet or HashMap is a big pain (I often give up). Finally, the worst of it all, sometime, your change is not reflected in the code after a refresh. This is a bug of the SDM and quite unpredictable (can't reproduce when you want to take a look at it). Which means you can't trust the tool. If your change doesn't fix your issue, you don't know if it's because you didn't fix it or because your change has not been applied. I'm not the only one not liking SDM, an other reason for that is probably because most GWT developers are Java developers, and SDM bring us closer to the Javascript feeling that we don't like.
  • compiling time & memory: We have a big project. It take more than 45mn to compile our project for production. Also, the compiler needs minimum 8Go of ram otherwise the compile time is even longer. So our CI server cannot do anything else when we compile the project. At a time when "time to production" is becoming more and more important, a 45mn compilation is too much.

The future


GWT 2.8 will be out soon (eventually), with full Java 8 support and a final version of JsInterop. That's great news specially for Streams support that will definitely reduce code size and improve maintainability. We don't really plan to use JsInterop, at least for now, but it's still a very cool feature. There is some plan for GWT 3.0, and from what I have heard there is big changes incoming. Changes as big as from Angular 1 to Angular 2. GWT 3.0 will be a totally different framework. So different that they may as well call it something else. GWT will only take care of generating Javascript from Java, then it will let the Closure compiler take care optimizations.

So we will probably not migrate anytime soon, but that's ok GWT 2.x is still great for us.