sfscon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs
DESCRIPTION
Davide Montesin's talk about "write java, run javascript: create enterprise html5 apps without “undefined” bugs" during the #SFScon14TRANSCRIPT
write java, run javascript:
create enterprise html5 apps without "undefined" bugs
by Davide Montesin <[email protected]>
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 1 / 63
Who I am
Self employed senior consultant, teacher, programmer inBolzano/Bozen, Italy
Java - Android - HTML5/Js/Css3 - XML - JSON - Database
Open source - Linux
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 2 / 63
Javascript & browsers
Javascript ...
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 3 / 63
Javascript variable
Declaring a variable
var person = 'davide';console.log(person);
Output
davide
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 4 / 63
Javascript dynamic typing :-(
A variable can contain any value type during its life
var person = 'davide'; // Stringconsole.log(person);
person = 2; // Numberconsole.log(person);
person = {name: 'davide'} // Objectconsole.log(person);
person = ['davide']; // Arrayconsole.log(person);
Output:
davide2Object {name: "davide"}["davide"]
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 5 / 63
Javascript dynamic typing :-((
A typo
Output:
undefined
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 6 / 63
Javascript dynamic typing :-((
A typo
"person.name" is an error but javascript does not stop the execution
but declares implicitly a new attribute. A very bad bug!
Webstorm IDE and "use strict" does not help in this case :-(
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 7 / 63
Javascript dynamic typing :-((((((
Variables can contain any value, IDEs can't be exact with suggestions
In this case the method innerHTML makes no sense for a string
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 8 / 63
Javascript dynamic typing :-(((((
API: a javascript developer often consults documentation
or copy/paste from examples
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 9 / 63
Javascript dynamic typing :-(((
function parameters: the number and type of parameters
function sum(a, b) { return a + b }
console.log(sum(1,2));
console.log(sum(1));console.log(sum("1","2"));console.log(sum(1,2,3));console.log(sum({},[]));
Output:
3NaN123[object Object]
javascript does not stop the execution :-(
Wrong values are introduced into the execution flow.
The evidence of a problem can occur later in another part of the code(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 10 / 63
Javascript dynamic typing :-((((((
making a typo, using the wrong number/type of parameters
or other errors, can be very frustrating!
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 11 / 63
Javascript dynamic typing :-((((((
to catch a type or parameter error you need to execute
the code step by step with a debugger or with console.log
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 12 / 63
Javascript dynamic typing :-((((((
Problems increase with the line of code
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 13 / 63
Agile software development
#1 start with a small working software
#2 add new working features using short iteration (weeks)
#3 evaluate results with customer then go to #2
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 14 / 63
Javascript dynamic typing :-((((((
In an iteration API can be changed .i.e add a new required parameter
to a function or rename/refactor the code
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 15 / 63
Javascript dynamic typing :-((((((
If you want rename or change the signature of a function
you must remember/find all the parts where you have used the function
Do you remember? After 6 months? 1 year?
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 16 / 63
Javascript dynamic typing :-((((((
You want rename person's print to printName (but not book.print)
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 17 / 63
Javascript dynamic typing :-((((((
Without var types, Webstorm can't undestand and makes a mistake
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 18 / 63
Javascript dynamic typing :-((((((
If you don't remember where you have used the function ...
... the customer some weeks later ...
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 19 / 63
Javascript dynamic typing :-((((((
after each change you must test the software again
If you don't remember anymore, in the worst case you must test all again!
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 20 / 63
Javascript dynamic typing :-((((((
Or write test cases only to check typo, types and parameters
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 21 / 63
Java
Java ...
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 22 / 63
Java variable
Declaring a variable
String person = "davide";System.out.println(person);
Output
davide
Type of variable is mandatory
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 23 / 63
Java variable
Do you don't know which type you must use?
<ctrl+1+enter> the IDE thinks for you! WITHOUT errors/mistakes
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 24 / 63
Java static typing :-)
A variable can contain only a type during its life
During editing (not running) the ide informs of errors
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 25 / 63
Java static typing :-)
A variable isn't automatically created if it contains a typo
During editing (not running) the ide informs of errors
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 26 / 63
Java static typing :-)
A variable can contain only a type, ide can EXACTLY list the methods
During editing (not running) the ide informs of errors
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 27 / 63
Java
API guides are not required
You can use your second monitor for better things...
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 28 / 63
Java
Methods: all invalid calls are marked with errors
During editing (not running) the ide informs of errors
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 29 / 63
Java
How to find typo and methods wrong parameters?
Easy: follow the red signs ...
During editing (not running) the ide informs of errors
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 30 / 63
Java
Thanks to types, eclipse can EXACTLY find the methods to rename
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 31 / 63
Agile
Real case: in SASAbusHTML5 it finds all the screens that call a bus stop detail
After a change in the stop details is enough to check the 5 screens:
BusLinePanel, BusStationInRangeWidget, Favourite, BusStationPopup,MenuSearch
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 32 / 63
Agile
If you make an incompatible change to a signature of a method the IDE
can tell you the exact places where you must fix the problem manually
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 33 / 63
With java
The IDE helps you during editing, before the customer ...
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 34 / 63
I will convert
GREAT! I will convert my software from javascript to java
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 35 / 63
When and how convert?
When does it make sense to use java instead javascript?
How? The browser can't understand java ...
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 36 / 63
GWT Report 2013
http://vaadin.com/gwt/report-2013
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 37 / 63
GWT Report 2013
http://vaadin.com/gwt/report-2013
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 38 / 63
Conclusions: When Java, When Javascript
When Java
too much undefined errors with javascript
you are afraid to change your javascript project
your customers are angry
structured (complex) project (> 5k LOC)
intranet / internet web apps
need of easier maintenance over time
android, desktop and server
using java tools: refactor, debug, junit, ...
You can't anyway forget all about html5/css3/javascript
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 39 / 63
Conclusions: When Java, When Javascript
When Javascript
web sites animations, gallery, widgets
small project, copy&paste of code from internet
not much custom development
full control of javascript code needed: integration
need of dinamic features that java does not support
SEO not required
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 40 / 63
How
Google Web Toolkit (GWT) (production)
Micro Javascript Jvm (DMJsJvm) (experimental)
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 41 / 63
GWT - http://www.gwtproject.org
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 42 / 63
How does GWT works?
GWT translates Java SOURCE code into the equivalent Javascript code
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 43 / 63
GWT: client, server, shared and serialization
In a GWT project Java objects can run
server only: this code will run only the server side: i.e. servlets
client only: this code will run only the client side: i.e. Window,Location
shared: this code can run both server or client side
this data objects can be serialized and transmitted over thenetwork from one side to the other (i.e. bus stops)
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 44 / 63
Should I learn new API?
Should I learn new API?
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 45 / 63
Yes/No
Yes, if you use gwt widgets
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 46 / 63
Yes/No
GWT visual designer
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 47 / 63
Yes/No
No, if you prefer you can mimic html5/javascript api (with java)
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 48 / 63
Yes/No
With gwt you are open to use any existing javascript and css framework
It is very easy to mix javascript and java
There exist already a lot of wrapper projects
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 49 / 63
Yes/No
With gwt you can use any css framework by simply setting the right class
element.setClassName("btn btn-default");
Bootstrap, flat-ui, ecc ...
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 50 / 63
My GWT projects
Projects I made with GWT
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 51 / 63
SASAbus HTML5
HTML5 web app
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 52 / 63
SASAbus HTML5
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 53 / 63
The power of UI inherintance and composition
Java UI components/object in the SASAbus HTML5 app
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 54 / 63
SASAbus Android
Hybrid native + html5 (java + java)
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 55 / 63
SASAsightsee
Which POI do you want to see? I give you the bus route!
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 56 / 63
DMJsJvm
DMJsJvm: micro javascript jvm
The easiest way to convert java to javascript
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 57 / 63
DMJsJvm
Very small project: about 5k line of code
Good to learn how a jvm works i.e. students
Partial support for java 8 lambda
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 58 / 63
DMJsJvm
DMJsJvm translates bytecode instead of java source code
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 59 / 63
DMJsJvm
Why DMJsJvm is so small?
Because the jvm is a very simple machine based on only a stack and 256instructions
The bigger part are the system libraries ... I have replaced them with theequivalent calls to javascript
DMJsJvm intentionally support only a part of jre
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 60 / 63
Java and the javascript bytecode
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 61 / 63
DMJsJvm
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 62 / 63
Thanks
Thank you!
(C) 2013-2014 Davide Montesin <[email protected]> - License: CC BY NC ND write java, run javascript: create enterprise html5 apps without "undefined" bugs 63 / 63