sfscon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

63
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

Upload: south-tyrol-free-software-conference

Post on 10-Jul-2015

141 views

Category:

Technology


1 download

DESCRIPTION

Davide Montesin's talk about "write java, run javascript: create enterprise html5 apps without “undefined” bugs" during the #SFScon14

TRANSCRIPT

Page 1: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 2: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 3: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 4: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 5: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 6: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 7: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 8: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 9: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 10: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 11: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 12: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 13: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 14: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 15: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 16: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 17: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 18: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 19: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 20: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 21: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 22: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 23: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 24: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 25: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 26: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 27: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 28: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 29: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 30: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 31: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 32: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 33: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 34: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 35: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 36: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 37: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 38: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 39: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 40: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 41: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 42: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 43: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 44: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 45: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 46: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 47: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 48: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 49: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 50: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 51: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 52: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 53: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 54: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 55: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 56: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 57: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 58: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 59: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 60: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 61: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 62: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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

Page 63: SFScon14: write java, run javascript: create enterprise html5 apps without “undefined” bugs

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