91
GWT is Smarter Than You Robert Cooper Code Wrangler, REACH Heath CodeMash 2012

GWT is Smarter Than You

Embed Size (px)

DESCRIPTION

CodeMash 2012 version of my intro to GWT talk.

Citation preview

Page 1: GWT is Smarter Than You

GWT is Smarter Than You

Robert CooperCode Wrangler, REACH Heath

CodeMash 2012

Page 2: GWT is Smarter Than You

Introduction

Who is this guy?

2

Page 3: GWT is Smarter Than You

I wrote this...

3

Page 4: GWT is Smarter Than You

I work here…

4

Page 5: GWT is Smarter Than You

I also do this stuff…

5

Page 6: GWT is Smarter Than You

Jurassic Web

I’ve been doing web stuff since the time before time…

6

Page 7: GWT is Smarter Than You

Remember DCOM? Yeah.

I have been working mostly in Java since the last millennium…

7

Page 8: GWT is Smarter Than You

Then at JavaOne 2k6, I had my “Road to Damascus” experience…

8

Page 9: GWT is Smarter Than You

What’s it all about, Alfie?

It is my goal to convince you thatif you aren’t using GWT to build AJAX apps,

you’re doing it wrong.

(Probably) 9

Page 10: GWT is Smarter Than You

10

Page 11: GWT is Smarter Than You

Speed Advisory…

Hit me up later.

Life moves pretty fast. If you don't stop and look around once in a while, you could miss it. 11

Page 12: GWT is Smarter Than You

Who are you?

Taking your temperature…

Who are you?

Orally. 12

Page 13: GWT is Smarter Than You

Elevator Pitch

• JavaScript mostly sucks

13

Page 14: GWT is Smarter Than You

Elevator Pitch

• JavaScript mostly sucks

14

Page 15: GWT is Smarter Than You

Elevator Pitch

• JavaScript mostly sucks• Gets out of hand quickly• Part of a larger ecosystem to which it has no

relationships• JavaScript libraries are a pain to use and in too

many cases (read: mobile) cause more harm than good.

15

Page 16: GWT is Smarter Than You

Why You Care

I stole this next block of stuff…

(Ido Green is awesome)

16

Page 17: GWT is Smarter Than You

Mobile is teh hotness

You will lose 13% of your users after 2sec! and 25% of your users after 4sec of load time.

Whatever.

We have all seen versions of

these stats since ’97.

17

Page 18: GWT is Smarter Than You

Remember 8 sec rule on 56Kbd?

18

Page 19: GWT is Smarter Than You

Speed Matters

http://greenido.wordpress.com/2012/01/01/mobile-web-performance/

19

Page 20: GWT is Smarter Than You

I stole this slide, too.

20

Page 21: GWT is Smarter Than You

Still stealing slides…

21

Page 22: GWT is Smarter Than You

Why are you here?

Get all that for free?

22

Page 23: GWT is Smarter Than You

GWT History

• Introduced in 2006 (you knew that)• Cross Compiles Java to JavaScript (you

probably knew that)• Works in all browsers (duh)• Used for lots of recent Google projects

(Flights, Android Market, Wave (rip)) • Good bit of third party adoption

(Lombardi/IBM, OpenNMS, Seesmic, etc)

) 23

Page 24: GWT is Smarter Than You

Stuff you may not know…

On Day 0, GWT had:• Multiple, targeted builds for User-Agent (UA) and

i18n and more (including custom divisions)• A great caching strategy to let everything in your

app have perma-cache settings• Automagical spritemaps of all the images in your

application (read: 1 HTTP request for ImageBundles)

• Compiler optimization (not just obfuscation)• Testing, Packaging, Modules

( ) 24

Page 25: GWT is Smarter Than You

Other pains

• Timestamp localization• XSS security issues• Complex X-Browser eventing (Bubble vs Sink)• Server messaging• Referential object serialization

Oh wait. GWT 1.0 took care of all of that too.

(Confidential) 25

Page 26: GWT is Smarter Than You

I can do that myself!

Can you really?

( ) 26

Page 27: GWT is Smarter Than You

Monolithic compilation is an idea whose time has come again.

(Confidential) 27

Page 28: GWT is Smarter Than You

More than Cross Compilation

• Deferred Binding is the core to how GWT works

• Handles DOM and Eventing implementations for the different browsers

• Handles i18n issues• Is why building the final deployment version

takes FOR-EVER.

28

Page 29: GWT is Smarter Than You

Module.gwt.xml

Yeah, I know. It is Java so there HAS to be an XML file.

( ) 29

Page 30: GWT is Smarter Than You

And code generation…

30

Page 31: GWT is Smarter Than You

Look sir, Buzzwords!

Dependency Injection!Meta-Programming!

31

Page 32: GWT is Smarter Than You

Deployable Compiles Are Slow

Many versions!

Lots of time.

SSDs and Quad i7s recommended. 32

Page 33: GWT is Smarter Than You

Caches PERFECTLY

• Each build spits out a bunch of MD5 hashed filenames

• File names based on the actual source that generated the file (AFTER UA/i18n/your stuff)

• All of these files can be cached permanently!• 1 “.nocache.js” bootstrap that picks the right

version• This file can be in-lined with standard JSP

@includes

33

Page 34: GWT is Smarter Than You

Optimized!

• Unreachable code is pruned• Staticification• Non-polymorphic calls are converted to “static”

(global scope functions)• myCircle.computeArea() becomes

computeArea(_this)• WAY, WAY faster.

• Method inlining• Getters/setters melt away

34

Page 35: GWT is Smarter Than You

35

Page 36: GWT is Smarter Than You

Progress

• 1.x line improvements• Multi-threaded compiler• SOYC• New compress-able naming (GZIP-targeted)• Java 1.5 language features (Generics)• JavaScript Overlays (more on this next)• Handlers rather than Listeners• Pluggable dev servers

36

Page 37: GWT is Smarter Than You

JavaScriptOverlays

Allows you to extend JavaScriptObject to work JS/JSON-P dataCode like this:

Becomes:

It HAS to be final because of the staticification logic. That means no interface implementation for JSOs!

37

Page 38: GWT is Smarter Than You

Could be better…

• Lots of things still sucked in 1.x• SWT based “Development Shell”

Limited you to 1 browser on a platform• No salvation from CSS Hell (nee, MSIE6)• Hard to fit into the established dev process,

assuming you have a real graphic design staff• The whole block of code for an app came down at

once (made doing #! type code hard)

38

Page 39: GWT is Smarter Than You

Listeners Improved

• addXHandler() returns a HandlerRegistrationaddValueChangeHandler( ValueChangeHanlder<String>)

• HandlerRegistration has .removeHandler()• This is a lot easier to clean up than a bunch of

add/removeListener() calls • Store all your HandlerRegistrations for a state in a

collection• Iterate and .removeHandler() on each of them• No need to maintain target/listener mappings

39

Page 40: GWT is Smarter Than You

Core API Improved

• Core widgets got a cleanup:• CheckBox implements HasValue<Boolean>• TextBox implements HasValue<String>• etc

• IsWidget interface• More flexible that extending widget• Makes using non-GWT UI easier

• XXLayoutPanel• Improved, and CSS-accelerated layouts

40

Page 41: GWT is Smarter Than You

Heads up

If you haven’t played with GWT in a while, now is the time to start paying attention.

41

Page 42: GWT is Smarter Than You

GWT 2.x features

• OOPHM (or OOMPH, or DevMode, not GWTShell)

• Image in-lining (NOT spritesheets)

• Compiled CSS (yes, better than LESS-CSS or SASS)

• Developer guided point cuts• UIBinder• “MVP - Framework”

42

Page 43: GWT is Smarter Than You

OOPHM

Out of Process Hosted Mode (or DevMode)

43

Page 44: GWT is Smarter Than You

One ring to rule them all

I’m not a Windows fan, but...

Safari, FF, Chrome, MSIE(x), Opera

44

Page 45: GWT is Smarter Than You

“Super Draft Mode is Coming…”

45

Page 46: GWT is Smarter Than You

I like the DevMode tooling

I fear change. 46

Page 47: GWT is Smarter Than You

CSS Best Practices

47

Page 48: GWT is Smarter Than You

Images

You think getting your designers to make spritesheets makes you tough.

48

Page 49: GWT is Smarter Than You

Your designer went to art school

49

Page 50: GWT is Smarter Than You

How many do image in-lining in their CSS?

Bring down all your incidental images in the same HTTP Request as your CSS!

50

Page 51: GWT is Smarter Than You

GWT 2.x CssResource and ImageResource

Becomes:

(New name matches GZIP optimized naming)

51

Page 52: GWT is Smarter Than You

Next, You need a ClientBundle…

And a CssResource…

And to inject it…

52

Page 53: GWT is Smarter Than You

Wait, what just happened?

Now, your optimized CSS and all your incidental art assets are compiled into the JavaScript,

GZIPped, and downloaded in the single, initial HTTP Request for your app…

One (1) Request, and your app is done loading.

53

Page 54: GWT is Smarter Than You

How much would you pay for all this?

But wait, there’s more!

54

Page 55: GWT is Smarter Than You

@def – Define those repeated bits

Now include these in your styles…

55

Page 56: GWT is Smarter Than You

@if/@elif/@else – Do conditional CSS based on the compile time options

Works for user.agent, language, even your own custom compile options

56

Page 57: GWT is Smarter Than You

@url – Lets you embed arbitrary data for all kinds of stuff

ClientBundle:

CSS File:

Great for Fonts, Cursors, etc…

57

Page 58: GWT is Smarter Than You

@noflip – Surpresses flipping on CSS directions for automagic Right-Left i18n flipping (did I mention automagic RTL/LTR language support?)

58

Page 59: GWT is Smarter Than You

59

Page 60: GWT is Smarter Than You

Maybe that is all too much to download…

Did you try runAsync()?

60

Page 61: GWT is Smarter Than You

You want to be a good web citizen.

You want your app to support deep links(without hash-bangs, plzkthxbai)

but you don’t need the whole app to render,

say, one “product page” or “blog post”.

61

Page 62: GWT is Smarter Than You

GWT.runAsync() allows for “developer guided point-cuts” in your compiled output.

62

Page 63: GWT is Smarter Than You

63

Page 64: GWT is Smarter Than You

Eliminate initial code download

• All the code *only* reachable from inside the RunAsyncCallback.onSuccess() method is compiled into another set of script files

• This can include CSS/Image/DataResources• It is only loaded *once* and cached properly• Shared code between any two callbacks OR

code reachable from onModuleLoad() still makes it into the base script (core CSS, java.util.ArrayList, GWT’s UIObject, etc)

64

Page 65: GWT is Smarter Than You

Pay attention

Not a silver-bullet, BUT…• Way better than JavaScript library

dependency semantics• Still includes the UA/i18n/Custom compile

optimizations• For big* code bases, truly a must-have• Look at SOYC to see what you can tweak

65

*for EXTREMELY large values of “big”

Page 66: GWT is Smarter Than You

99 problems, but process ain’t 1

OK, but what about all that SWING widgeting? I gotta work with “HTML+ designers”?

Use UIBinder.

66

Page 67: GWT is Smarter Than You

UIBinder

• Lets you use XHTML files for widget templates (OR do declarative mixing with GWT widgets)

• Toss whatever HTML you need to in there.

67

Page 68: GWT is Smarter Than You

Two ways to do it…

• Work with the DOM directly (sucks)

68

Page 69: GWT is Smarter Than You

Two ways to do it…

• Create GWT Widgets (rules)

69

Page 70: GWT is Smarter Than You

Moar speed!

Using GWT widgets lets you work with the new CSS-engine accelerated LayoutPanel stuff, and

that is awesomesauce.

70

Page 71: GWT is Smarter Than You

Framework

• GWT 1.x was decidedly “non-frameworky”• It was a UITK at best• It was a fancy compiler/DI/MP system at worst

(not that that was a bad thing)• GIN/Guice did a lot to improve this

• GWT 2.x is slightly more opinionated• Model – View – Presenter framework • (NOT MVC)• Not a fan

71

Page 72: GWT is Smarter Than You

Places

• GWT 1.x included a “History” system• This lets you deal with anchor slugs

(“#somethings”) in the URL to allow the back button to work

• GWT 2.x encourages you to use these as “Places” (standard slugs rather than roll-your-own)

72

Page 73: GWT is Smarter Than You

Boilerplate… Ugh…

The “Places” System is made up of four main components

• The PlaceHistoryMapperThis is pretty much just a config file

• The ActivityMapperThis returns an Activity for a Place

• ActivitiesMini-Apps that are mapped to a render area

• HistoryObserverA Place-aware HistoryTokenListener replacement

73

Page 74: GWT is Smarter Than You

PlaceHistoryMapper (gets generated)

The tokenizers just turn an anchor slug to a string.

74

Page 75: GWT is Smarter Than You

ActivityMapper return an activity for a Place

.getActivity(Place,Place) here is just a big if structure, but you can do better.

75

Page 76: GWT is Smarter Than You

Activities have a .start() method that takes the UI parent and a reference to the app-global EventBus.

76

Page 77: GWT is Smarter Than You

Using HistoryObserver to do transitions:

77

Page 78: GWT is Smarter Than You

These examples from m-gwt:http://www.m-gwt.com/

You should use this, and maybe GWT-PhoneGap:

http://code.google.com/p/gwt-phonegap/

78

Page 79: GWT is Smarter Than You

M-GWT on Phones

79

Page 80: GWT is Smarter Than You

Android Tablet

80

Page 81: GWT is Smarter Than You

iPad

81

Page 82: GWT is Smarter Than You

My United States of Whatever

82

Page 83: GWT is Smarter Than You

Enterprise-y!

Spring Roo integration gives you Rails-like autogen of basic app scaffolding

Geordi not included. 83

Page 84: GWT is Smarter Than You

MOAR SPEED

SpeedTracer (Even if you don’t use GWT, you should use this)

84

Page 85: GWT is Smarter Than You

…and Enterprise-y!

If you are using Spring Server Tools/Roo or Google App Engine,

you can drill from the browser into the the calls on the server

and see why your server is slow, too!

85

Page 86: GWT is Smarter Than You

Eat it, FireBug

SpeedTracer +

WebKit tools=

Best Dev Tools Available

86

Page 87: GWT is Smarter Than You

Drawbacks

SpeedTracer only works in Chrome.

Finding exactly what is slow in Brand X browser?

YMMV.

87

Page 88: GWT is Smarter Than You

Coming Soon

• SourceMaps• Developed by Google and Mozilla• Maps compiled JavaScript to original source files• CoffeeScript, Dart, GWT, Closure• Makes “Super Draft Mode” less WTF

• Unified Compiler Infrastructure• Google is getting Dart, GWT, and Closure on the

same toolchain• Future Dart backends for GWT code

I study nuclear science. I love my classes. 88

Page 89: GWT is Smarter Than You

Still more to explore

• Google Tools for Eclipse• Instantiations purchase brought in all kinds of

awesome• I still hate the plugin

• New Widgets• Better Browser Support• HTML5 Stuff• PlayN (formerly ForPlay)

(This is totally awesome, build games for HTML5, Flash, Java, and Android with 1 codebase)

• Third Party stuff (SmartGWT, EXT-GWT)

89

Page 90: GWT is Smarter Than You

Summary

• GWT at launch was technically impressive, but still seemed small.

• GWT is not small anymore.• GWT is filled with Google institutional

knowledge.• GWT does all best practices.• GWT can build better JavaScript than you.• GWT future-proofs your application.

90

Page 91: GWT is Smarter Than You

Links

• http://code.google.com/webtoolkit/

• http://greenido.wordpress.com/2012/01/01/mobile-web-performance/

• http://m-gwt.com/

• http://code.google.com/p/gwt-phonegap/

• http://code.google.com/p/playn/

• http://www.sencha.com/products/extgwt

• http://code.google.com/p/smartgwt/

• http://profiles.google.com/kebernet

• http://www.slideshare.net/kebernet/gwt-is-smarter-than-you

91