building with javascript - write less by using the right tools

87
Building with JavaScript write less by using the right tools Christian Heilmann Framsia Meetup, Oslo, Norway, October 2010

Upload: christian-heilmann

Post on 01-Dec-2014

5.475 views

Category:

Education


9 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Building with JavaScript -  write less by using the right tools

Building with JavaScript

write less by using the right tools

Christian HeilmannFramsia Meetup, Oslo, Norway, October 2010

Page 2: Building with JavaScript -  write less by using the right tools

Javascript has won the title of most used web programming language.

Page 3: Building with JavaScript -  write less by using the right tools

The reason is that its syntax is forgiving and that it comes with a few incredibly powerful tools.

Page 4: Building with JavaScript -  write less by using the right tools

The other reason is that libraries allow us to write JavaScript instead of catering to browsers and fixing them.

Page 5: Building with JavaScript -  write less by using the right tools

This comes with a few dangers though.

Page 6: Building with JavaScript -  write less by using the right tools

The “write less, achieve more” attitude is a scam.

Page 7: Building with JavaScript -  write less by using the right tools

Less code does not mean a better solution.

Page 8: Building with JavaScript -  write less by using the right tools

Less redundant code means a good solution.

Page 9: Building with JavaScript -  write less by using the right tools

If writing a few more lines of code makes it easier for others to use what you have done then you built a professional product.

Page 10: Building with JavaScript -  write less by using the right tools

If you are the only one understanding what is going on you used the web as a dumping ground.

Page 11: Building with JavaScript -  write less by using the right tools

You can write incredibly small solutions that work for everyone.

Page 12: Building with JavaScript -  write less by using the right tools

If you use the right technology for the job.

Page 13: Building with JavaScript -  write less by using the right tools

If you want to build for people, use progressive enhancement.

Page 14: Building with JavaScript -  write less by using the right tools

Progressive enhancement means not making everything work in IE6.

Page 15: Building with JavaScript -  write less by using the right tools

It means checking the depth of the river before diving in.

Page 16: Building with JavaScript -  write less by using the right tools

The physical world is in a final state.

Page 17: Building with JavaScript -  write less by using the right tools

A pair of scissors cannot change themselves when a left-handed person picks them up instead of a right-handed person.

Page 18: Building with JavaScript -  write less by using the right tools
Page 19: Building with JavaScript -  write less by using the right tools

Web applications and systems can do that - if we allow them to.

Page 20: Building with JavaScript -  write less by using the right tools

A lot of the criticism of progressive enhancement is based on people working in walled-off environments.

Page 21: Building with JavaScript -  write less by using the right tools

If all you write is an iPad app or something for the iPhone you have it much easier than building a web app.

Page 22: Building with JavaScript -  write less by using the right tools

A Chrome extension will never have to work in IE6 - there is no point in that.

Page 23: Building with JavaScript -  write less by using the right tools

On the other hand an Opera extension is a W3C widget and also works on Vodafone mobile phones.

Page 24: Building with JavaScript -  write less by using the right tools

And all 20 users will be very excited about that.

Page 25: Building with JavaScript -  write less by using the right tools

Anyways, let’s go through a few examples and how to write a damn small amount of JS to achieve a lot.

Page 27: Building with JavaScript -  write less by using the right tools

This is a system where I was only allowed to use JavaScript.

Page 28: Building with JavaScript -  write less by using the right tools

Were this to be a product, I’d have taken another approach - more later.

Page 29: Building with JavaScript -  write less by using the right tools

So here’s what I did...

Page 30: Building with JavaScript -  write less by using the right tools

Get all the geographical information of all the countries in the world using the Yahoo GeoPlanet API.

Page 31: Building with JavaScript -  write less by using the right tools
Page 32: Building with JavaScript -  write less by using the right tools

Store the information in an object and loop through it to create the navigation.

Page 33: Building with JavaScript -  write less by using the right tools
Page 34: Building with JavaScript -  write less by using the right tools

I am using buttons for the navigation elements. Why?

Page 35: Building with JavaScript -  write less by using the right tools

There is no real navigation happening here. Everything is dependent on JS. This is what buttons are for. And they are keyboard accessible.

Page 36: Building with JavaScript -  write less by using the right tools

This should be a huge nested list. Why isn’t it visible?

Page 37: Building with JavaScript -  write less by using the right tools

Use CSS instead of simulating it.

Page 38: Building with JavaScript -  write less by using the right tools

Hide the loading message, add the new HTML and show it again.

Page 39: Building with JavaScript -  write less by using the right tools

Then show the first entry and make the buttons trigger the right functionality.

Page 40: Building with JavaScript -  write less by using the right tools

Instead of storing data in DOM elements with custom attributes I have two variables to store the currently shown section and the full data set.

Page 41: Building with JavaScript -  write less by using the right tools
Page 42: Building with JavaScript -  write less by using the right tools

Instead of looping a lot of elements and assigning redundant event handlers one event does the whole job.

Page 43: Building with JavaScript -  write less by using the right tools

The differentiator is the length of the button text. If it is one character show the section - otherwise load the country info.

Page 44: Building with JavaScript -  write less by using the right tools
Page 45: Building with JavaScript -  write less by using the right tools

Next step was to show the country info.

Page 46: Building with JavaScript -  write less by using the right tools

Fade the old container and show a loading message.

Page 47: Building with JavaScript -  write less by using the right tools

Read the country name from the button and get the value which is the number of the data set.

Page 48: Building with JavaScript -  write less by using the right tools
Page 49: Building with JavaScript -  write less by using the right tools
Page 50: Building with JavaScript -  write less by using the right tools

That did it - a bit more code than “real” jQuery developers would have done...

Page 51: Building with JavaScript -  write less by using the right tools

...but it is bullet proof and does the least amount of DOM lookups possible.

Page 52: Building with JavaScript -  write less by using the right tools

However, it was slow as heck as it takes time to download the world.

Page 53: Building with JavaScript -  write less by using the right tools

As you’ve seen earlier, it loads much faster the second time you go to the page.

Page 54: Building with JavaScript -  write less by using the right tools

The trick is that I am using “HTML5 Storage” to cache the whole navigation.

Page 55: Building with JavaScript -  write less by using the right tools
Page 56: Building with JavaScript -  write less by using the right tools

That way the interface is loaded from HD and not from the web.

Page 57: Building with JavaScript -  write less by using the right tools

You can extend that to cache full HTML interfaces.

Page 59: Building with JavaScript -  write less by using the right tools

Other progressive enhancement approaches use a single page as the data container.

Page 61: Building with JavaScript -  write less by using the right tools

My favourite approach uses the backend as a simple API.

Page 63: Building with JavaScript -  write less by using the right tools

Write the app as a simple form submit in PHP...

Page 64: Building with JavaScript -  write less by using the right tools
Page 65: Building with JavaScript -  write less by using the right tools

Then return chunks of HTML according to the parameters that came in.

Page 66: Building with JavaScript -  write less by using the right tools
Page 67: Building with JavaScript -  write less by using the right tools
Page 68: Building with JavaScript -  write less by using the right tools

All you need to do in JS then is to override the requests with Ajax calls.

Page 69: Building with JavaScript -  write less by using the right tools
Page 70: Building with JavaScript -  write less by using the right tools

You maintain your whole app on the server and can monitor and cache like heck.

Page 71: Building with JavaScript -  write less by using the right tools

And you still use the goodness that is JavaScript on top of that.

Page 72: Building with JavaScript -  write less by using the right tools

The dangers of “useful” shortcuts.

Page 73: Building with JavaScript -  write less by using the right tools

$.ajax({ url: url, dataType: 'jsonp', jsonp: 'callback', jsonpCallback: 'datain'});function datain(data){}

Getting JSON-P data in jQuery:

Page 74: Building with JavaScript -  write less by using the right tools

$.getJSON(url+'&callback=?', function(data){});

Getting JSON-P data in jQuery (shorter):

Page 76: Building with JavaScript -  write less by using the right tools

getJSON() breaks the cache of the service you request.

Page 77: Building with JavaScript -  write less by using the right tools

As each request has a unique URL - even when you ask for the same data...

Page 78: Building with JavaScript -  write less by using the right tools

... you’ll end up being banned for an hour very quickly.

Page 79: Building with JavaScript -  write less by using the right tools

less code == better?

Page 80: Building with JavaScript -  write less by using the right tools

Analysing the impact should be part of your solution.

Page 81: Building with JavaScript -  write less by using the right tools

The danger of quick solutions is that people tend to copy and paste them and not try to integrate them.

Page 82: Building with JavaScript -  write less by using the right tools

And if we put together solutions from lots of copy and paste examples they become huge and hard to understand.

Page 83: Building with JavaScript -  write less by using the right tools

Which is probably the main reason of all the massive CSS files out there.

Page 84: Building with JavaScript -  write less by using the right tools

Use technologies to their strengths to write very short and efficient code.

Page 85: Building with JavaScript -  write less by using the right tools

★ Progressive Enhancement with the correct HTML

★ Event Delegation ★ Adding CSS classes instead of

using hide()★ Rendering on the server side.★ Caching on the client side.★ Configuration objects and

variable caches instead of custom attributes.

Page 86: Building with JavaScript -  write less by using the right tools

Keep an eye out on how libraries help you with writing bullet proof code - not how to write small scripts.

Page 87: Building with JavaScript -  write less by using the right tools

Christian Heilmann http://wait-till-i.com http://developer-evangelism.com http://twitter.com/codepo8

Thanks!