SlideShare a Scribd company logo
Building with
    JavaScript
write less by using the right tools


                                           Christian Heilmann
                   Framsia Meetup, Oslo, Norway, October 2010
Javascript has won the
title of most used web
programming language.
The reason is that its
syntax is forgiving and
that it comes with a few
incredibly powerful
tools.
The other reason is that
libraries allow us to
write JavaScript instead
of catering to browsers
and fixing them.
This comes with a few
dangers though.
The “write less, achieve
more” attitude is a
scam.
Less code does not
mean a better solution.
Less redundant code
means a good solution.
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.
If you are the only one
understanding what is
going on you used the
web as a dumping
ground.
You can write incredibly
small solutions that
work for everyone.
If you use the right
technology for the job.
If you want to build for
people, use progressive
enhancement.
Progressive
enhancement means
not making everything
work in IE6.
It means checking the
depth of the river before
diving in.
The physical world is in
a final state.
A pair of scissors
cannot change
themselves when a left-
handed person picks
them up instead of a
right-handed person.
Building with JavaScript -  write less by using the right tools
Web applications and
systems can do that - if
we allow them to.
A lot of the criticism of
progressive
enhancement is based
on people working in
walled-off
environments.
If all you write is an
iPad app or something
for the iPhone you have
it much easier than
building a web app.
A Chrome extension will
never have to work in
IE6 - there is no point in
that.
On the other hand an
Opera extension is a
W3C widget and also
works on Vodafone
mobile phones.
And all 20 users will be
very excited about that.
Anyways, let’s go
through a few examples
and how to write a
damn small amount of
JS to achieve a lot.
http://10k.aneventapart.com/Entry/185
http://github.com/codepo8/worldinfo
This is a system where I
was only allowed to use
JavaScript.
Were this to be a
product, I’d have taken
another approach -
more later.
So here’s what I did...
Get all the geographical
information of all the
countries in the world
using the Yahoo
GeoPlanet API.
Building with JavaScript -  write less by using the right tools
Store the information in
an object and loop
through it to create the
navigation.
Building with JavaScript -  write less by using the right tools
I am using buttons for
the navigation
elements. Why?
There is no real
navigation happening
here. Everything is
dependent on JS. This is
what buttons are for.
And they are keyboard
accessible.
This should be a huge
nested list. Why isn’t it
visible?
Use CSS instead of
simulating it.
Hide the loading
message, add the new
HTML and show it
again.
Then show the first
entry and make the
buttons trigger the right
functionality.
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.
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.
The differentiator is the
length of the button
text. If it is one
character show the
section - otherwise load
the country info.
Building with JavaScript -  write less by using the right tools
Next step was to show
the country info.
Fade the old container
and show a loading
message.
Read the country name
from the button and get
the value which is the
number of the data set.
Building with JavaScript -  write less by using the right tools
Building with JavaScript -  write less by using the right tools
That did it - a bit more
code than “real” jQuery
developers would have
done...
...but it is bullet proof
and does the least
amount of DOM lookups
possible.
However, it was slow as
heck as it takes time to
download the world.
As you’ve seen earlier, it
loads much faster the
second time you go to
the page.
The trick is that I am
using “HTML5 Storage”
to cache the whole
navigation.
Building with JavaScript -  write less by using the right tools
That way the interface
is loaded from HD and
not from the web.
You can extend that to
cache full HTML
interfaces.
http://www.wait-till-i.com/2010/08/26/using-html5-storage-to-
                cache-application-interfaces/
Other progressive
enhancement
approaches use a single
page as the data
container.
http://isithackday.com/demos/warwickshire/
http://github.com/codepo8/warwickshire
http://www.youtube.com/watch?v=_uAOyzw50PY
My favourite approach
uses the backend as a
simple API.
http://www.youtube.com/watch?v=i_1sVnNkN2M
http://isithackday.com/hacks/flickrcollector/
http://github.com/codepo8/flickrcollector
Write the app as a
simple form submit in
PHP...
Building with JavaScript -  write less by using the right tools
Then return chunks of
HTML according to the
parameters that came
in.
Building with JavaScript -  write less by using the right tools
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.
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.
And you still use the
goodness that is
JavaScript on top of
that.
The dangers of “useful”
shortcuts.
Getting JSON-P data in
jQuery:
$.ajax({
  url: url,
  dataType: 'jsonp',
  jsonp: 'callback',
  jsonpCallback: 'datain'
});
function datain(data){
}
Getting JSON-P data in
jQuery (shorter):

$.getJSON(url+'&callback=?',
 function(data){
});
$.ajax():
http://{...}&
format=json&callback=datain


$.getJSON():
http://{...}&
format=json&callback=jsonp1282497813335


                     random number
getJSON() breaks the
cache of the service you
request.
As each request has a
unique URL - even when
you ask for the same
data...
... you’ll end up being
banned for an hour very
quickly.
less code == better?
Analysing the impact
should be part of your
solution.
The danger of quick
solutions is that people
tend to copy and paste
them and not try to
integrate them.
And if we put together
solutions from lots of
copy and paste
examples they become
huge and hard to
understand.
Which is probably the
main reason of all the
massive CSS files out
there.
Use technologies to
their strengths to write
very short and efficient
code.
★   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.
Keep an eye out on how
libraries help you with
writing bullet proof code
- not how to write small
scripts.
Christian Heilmann
http://wait-till-i.com        Thanks!
http://developer-evangelism.com
http://twitter.com/codepo8

More Related Content

PDF
Using Web Services with JavaScript - Fronttrends 2010
PDF
Preparing your web services for Android and your Android app for web services...
PDF
PPTX
Introduction about-ajax-framework
PDF
Velocity dust
PDF
Teach Your Sites to Call for Help: Automated Problem Reporting for Online Ser...
KEY
Leaving jsps in the dust
PDF
Scraping with Python for Fun and Profit - PyCon India 2010
Using Web Services with JavaScript - Fronttrends 2010
Preparing your web services for Android and your Android app for web services...
Introduction about-ajax-framework
Velocity dust
Teach Your Sites to Call for Help: Automated Problem Reporting for Online Ser...
Leaving jsps in the dust
Scraping with Python for Fun and Profit - PyCon India 2010

What's hot (7)

PPT
LatJUG. Google App Engine
PDF
Analyse Yourself
PPTX
Before start
PDF
Web Performance in the Age of HTTP2 - Topconf Tallinn 2016 - Holger Bartel
PPSX
Putting SOAP to REST
PDF
Websocket in iOS application to create real-time applications
PPT
Dynamic website
LatJUG. Google App Engine
Analyse Yourself
Before start
Web Performance in the Age of HTTP2 - Topconf Tallinn 2016 - Holger Bartel
Putting SOAP to REST
Websocket in iOS application to create real-time applications
Dynamic website
Ad

Similar to Building with JavaScript - write less by using the right tools (20)

KEY
Mobile optimization
PDF
Understanding progressive enhancement - yuiconf2010
PDF
Node.js and the MEAN Stack Building Full-Stack Web Applications.pdf
PPT
(In)Security Implication in the JS Universe
PDF
Intro to BackboneJS + Intermediate Javascript
PDF
Dart By Example 1st Edition Davy Mitchell
PDF
Dart By Example 1st Edition Davy Mitchell
PDF
There is something about JavaScript - Choose Forum 2014
PDF
Meteoro de pegasuus! Desenvolvendo aplicações realtime com MeteorJS
PDF
Six reasons to learn JavaScript
PDF
Dart By Example 1st Edition Davy Mitchell 2024 scribd download
PDF
Angular js mobile jsday 2014 - Verona 14 may
PPTX
Building high performance web apps.
PPT
Techniques For A Modern Web UI (With Notes)
PDF
Beginning MEAN Stack
PPTX
Java script framework
PDF
Learn Programming Languages & Get Programming Assignment Sample Solutions PDF...
PPTX
Nodejs
PPTX
What is HTML 5?
PDF
Easing offline web application development with GWT
Mobile optimization
Understanding progressive enhancement - yuiconf2010
Node.js and the MEAN Stack Building Full-Stack Web Applications.pdf
(In)Security Implication in the JS Universe
Intro to BackboneJS + Intermediate Javascript
Dart By Example 1st Edition Davy Mitchell
Dart By Example 1st Edition Davy Mitchell
There is something about JavaScript - Choose Forum 2014
Meteoro de pegasuus! Desenvolvendo aplicações realtime com MeteorJS
Six reasons to learn JavaScript
Dart By Example 1st Edition Davy Mitchell 2024 scribd download
Angular js mobile jsday 2014 - Verona 14 may
Building high performance web apps.
Techniques For A Modern Web UI (With Notes)
Beginning MEAN Stack
Java script framework
Learn Programming Languages & Get Programming Assignment Sample Solutions PDF...
Nodejs
What is HTML 5?
Easing offline web application development with GWT
Ad

More from Christian Heilmann (20)

PPTX
Develop, Debug, Learn? - Dotjs2019
PDF
Hinting at a better web
PDF
Taking the "vile" out of privilege
PDF
Seven ways to be a happier JavaScript developer - NDC Oslo
PDF
Artificial intelligence for humans… #AIDC2018 keynote
PDF
Killing the golden calf of coding - We are Developers keynote
PDF
Progressive Web Apps - Techdays Finland
PDF
Taking the "vile" out of privilege
PDF
Five ways to be a happier JavaScript developer
PDF
Taking the P out of PWA
PDF
Sacrificing the golden calf of "coding"
PDF
You learned JavaScript - now what?
PDF
Sacrificing the golden calf of "coding"
PDF
Progressive Web Apps - Covering the best of both worlds - DevReach
PDF
Progressive Web Apps - Covering the best of both worlds
PPTX
Non-trivial pursuits: Learning machines and forgetful humans
PDF
Progressive Web Apps - Bringing the web front and center
PDF
CSS vs. JavaScript - Trust vs. Control
PDF
Leveling up your JavaScipt - DrupalJam 2017
PDF
The Soul in The Machine - Developing for Humans (FrankenJS edition)
Develop, Debug, Learn? - Dotjs2019
Hinting at a better web
Taking the "vile" out of privilege
Seven ways to be a happier JavaScript developer - NDC Oslo
Artificial intelligence for humans… #AIDC2018 keynote
Killing the golden calf of coding - We are Developers keynote
Progressive Web Apps - Techdays Finland
Taking the "vile" out of privilege
Five ways to be a happier JavaScript developer
Taking the P out of PWA
Sacrificing the golden calf of "coding"
You learned JavaScript - now what?
Sacrificing the golden calf of "coding"
Progressive Web Apps - Covering the best of both worlds - DevReach
Progressive Web Apps - Covering the best of both worlds
Non-trivial pursuits: Learning machines and forgetful humans
Progressive Web Apps - Bringing the web front and center
CSS vs. JavaScript - Trust vs. Control
Leveling up your JavaScipt - DrupalJam 2017
The Soul in The Machine - Developing for Humans (FrankenJS edition)

Recently uploaded (20)

PPTX
Lesson notes of climatology university.
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Pharma ospi slides which help in ospi learning
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Basic Mud Logging Guide for educational purpose
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Cell Structure & Organelles in detailed.
PDF
Classroom Observation Tools for Teachers
PPTX
Institutional Correction lecture only . . .
PPTX
master seminar digital applications in india
PPTX
GDM (1) (1).pptx small presentation for students
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Supply Chain Operations Speaking Notes -ICLT Program
Lesson notes of climatology university.
O7-L3 Supply Chain Operations - ICLT Program
Computing-Curriculum for Schools in Ghana
Pharma ospi slides which help in ospi learning
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Basic Mud Logging Guide for educational purpose
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
VCE English Exam - Section C Student Revision Booklet
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Cell Structure & Organelles in detailed.
Classroom Observation Tools for Teachers
Institutional Correction lecture only . . .
master seminar digital applications in india
GDM (1) (1).pptx small presentation for students
TR - Agricultural Crops Production NC III.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Supply Chain Operations Speaking Notes -ICLT Program

Building with JavaScript - write less by using the right tools