Creating Happy Little Websites

77
Creating Happy Little Web Sites Maintainability, Performance and Predictability Christian Heilmann, Guardian Brown Bag , London, June 2008

description

A brown bag presentation I gave at the Guardian in London, England to explain some of the reasons and best practices that should be applied to successful web development.

Transcript of Creating Happy Little Websites

Page 1: Creating Happy Little Websites

Creating Happy Little

Web Sites

Maintainability,Performance and

Predictability

Christian Heilmann, Guardian Brown Bag , London, June 2008

Page 2: Creating Happy Little Websites

Happy Web Sites are working web sites.

Page 3: Creating Happy Little Websites

The definition of a working web site is out of your hands.

Page 4: Creating Happy Little Websites

You have no clue who uses your web sites with what and

in which state.

Page 5: Creating Happy Little Websites

This is 100% fact.

Deal with it.

Page 6: Creating Happy Little Websites

How can you deal with it?

Page 7: Creating Happy Little Websites

Keeping the damage as low as possible.

Page 8: Creating Happy Little Websites

Don’t rely on anything.

Page 9: Creating Happy Little Websites

Let those who care about a certain technology deal with

it.

Page 10: Creating Happy Little Websites

Without caring for the technologies we have to use to create web sites we will build unhappy web sites.

Page 11: Creating Happy Little Websites

These will lead to unhappy users, unhappy bosses and

you will lose your job and end up dancing half-naked for tourists in Covent Garden*

*YMMV

Page 12: Creating Happy Little Websites

The technologies:

HTML

CSS

JavaScript

Page 13: Creating Happy Little Websites

This is what will come out at the end of any development

framework.

Page 14: Creating Happy Little Websites

This is also what gets rendered and executed in the

unknown.

Page 15: Creating Happy Little Websites

HTML

Your foundation and structure

Page 16: Creating Happy Little Websites

HTML is markup and you need to tell the user agents what it

is and what to do with it.

Page 17: Creating Happy Little Websites

Therefore you need a valid DOCTYPE, a language

definition and encoding instructions.

Page 18: Creating Happy Little Websites

You also need to follow the rules of HTML.

Page 19: Creating Happy Little Websites

This ensures that user agents *should* do what you tell

them.

Page 20: Creating Happy Little Websites

More importantly it also removes one more source of problems and you can hand over to the next developer without having to explain

what naughty things you’ve done to the markup.

Page 21: Creating Happy Little Websites

HTML provides hooks for the other technologies:

Page 22: Creating Happy Little Websites

Element names and nesting structure.

Page 23: Creating Happy Little Websites

Use the right element for the right job, do not re-invent

HTML as you’d also need to re-invent the UA support!

Page 24: Creating Happy Little Websites

Nest as much as needed to provide enough hooks, but don’t nest for the sake of it.

Page 25: Creating Happy Little Websites

IDs and classes are there to define special elements and provide hooks for the other

technologies.

Page 26: Creating Happy Little Websites

ID = unique to the documenta very special case that carries a lot of CSS weight and is darn easy to access in JavaScript.

Page 27: Creating Happy Little Websites

class = defining difference or grouping

the odd one out in a group of elements or defining a collection of elements that are scattered around the document without being nested in the same parent.

Page 28: Creating Happy Little Websites

Use both sparingly, if you understand CSS, you don’t

need many.

Page 29: Creating Happy Little Websites

CSS does one thing terribly well:

It cascades down the document tree.

Page 30: Creating Happy Little Websites

Use this: define from the common down to the specific.

Page 31: Creating Happy Little Websites

body{}p{}#content p{}#content p.about{}

More of this sort of thing

Less of this sort of thing

Page 32: Creating Happy Little Websites

This will keep your CSS lean and mean and easy to

maintain.

Page 33: Creating Happy Little Websites

If you don’t structure your CSS, maintainers will simply tack on more selectors at the

bottom.

Page 34: Creating Happy Little Websites

They’ll also pad the selectors until the specificity is enough, or – even worse – add another

DIV with an ID to make it work.

Page 35: Creating Happy Little Websites

That sort of thing makes web sites very unhappy.

Page 36: Creating Happy Little Websites

Naming conventions:• Say what it is, not what it will look

like.

• Keep it short and understandable.

• Abvtns dnt wrk wll wv maintnrs.

Page 37: Creating Happy Little Websites

CSS defines the look and feel - not the behaviour.

Page 38: Creating Happy Little Websites

Roll-overs are OK, menu systems and clever

interaction are not clever “CSS only”.

Page 39: Creating Happy Little Websites

This is what JavaScript can do for you.

Page 40: Creating Happy Little Websites

Rules about JavaScript:• Avoid if possible.

• Use when it benefits the end user – and only then.

• Expect it to fail on every step of the process.

• Play it to its strengths, do not expect it to work like Java/Ruby/Python...

Page 41: Creating Happy Little Websites

Happy web sites come from mature development

processes and methodologies.

Page 42: Creating Happy Little Websites

Progressive Enhancement

stuffthatworks.build();

enhancements = [‘a’,’b’,’c’];

var c = 0;

while(environment.supports(enhancement[c]){

stuffthatworks.apply(enhancements[c]);

c++;

}

Page 43: Creating Happy Little Websites

Accessibility is not a nice-to-have or something to add on

to the end.

Page 44: Creating Happy Little Websites

Build with accessibility in mind from the start and everybody will benefit.

Page 45: Creating Happy Little Websites

Don’t make things appear like interactive elements – use interactive elements that

work across all input devices.

Page 46: Creating Happy Little Websites

Build processes

Page 47: Creating Happy Little Websites

Validation, Collation and Documentation are not nice-

to-haves.

Page 48: Creating Happy Little Websites

Validation ensures that code is very likely to work.

Page 49: Creating Happy Little Websites

More importantly it means that the next developer can

also use the code without knowing your hacks.

Page 50: Creating Happy Little Websites

If you make a distinct differentiation between development code and

deployment code you will build much happier web sites.

Page 51: Creating Happy Little Websites

development code is for humans:

Readable, documented, well-structured and following defined code standards.

Page 52: Creating Happy Little Websites

deployment code is for machines.

collated, minified, without comments, possibly optimised for the environment.

Page 53: Creating Happy Little Websites

Happy web sites are separated from the backend

and the business logic.

Page 54: Creating Happy Little Websites

Do not mix them, it will mean unhappy front-end code and a high possibility for security

holes.

Page 55: Creating Happy Little Websites

If you can, build with APIs internally, this allows for

parallel development and you can release them for the outside world in the future.

Page 56: Creating Happy Little Websites

Happy web sites are well performing web sites.

Page 57: Creating Happy Little Websites

Performance on the web is not a small subject.

Page 58: Creating Happy Little Websites

But there are some easy rules:

Page 59: Creating Happy Little Websites

CSS in the head – as few as possible.

Page 60: Creating Happy Little Websites

JavaScript if possible collated into a single include at the

end of the body (browsers stop rendering and smell the flowers for every

script node they find).

Page 61: Creating Happy Little Websites

Cut down on the amount of images you use – every HTTP

request slows down your page.

Page 62: Creating Happy Little Websites

Use CSS sprites.

Page 63: Creating Happy Little Websites

Avoid interaction with the DOM in JavaScript until it is

absolutely necessary.

Page 64: Creating Happy Little Websites

For example, instead of looping over a document and changing a lot of settings of every element matched by your criteria add a class to the parent element and let

CSS do the work!

Page 65: Creating Happy Little Websites

When it comes to event handling, use event

delegation, don’t apply a handler to each element.

Page 66: Creating Happy Little Websites

Make the site available as fast as possible – then get the

nice-to-haves.

Page 67: Creating Happy Little Websites

Lazy loading of scripts and images has a massive impact on perceived availability of

the web site.

Page 68: Creating Happy Little Websites

Lazy loading

=

load scripts when and if they are needed by generating

script nodes using the DOM.

Page 69: Creating Happy Little Websites

Lazy loading of images

=

load a placeholder image and replace it with the real one

when it comes into the viewport of the browser.

Page 70: Creating Happy Little Websites

The final and most important tip:

Page 71: Creating Happy Little Websites

Build on what others have done well.

Page 72: Creating Happy Little Websites

For example the Yahoo User Interface Library.

http://developer.yahoo.com/yui/

Page 73: Creating Happy Little Websites

• CSS framework (predictable layouts and typography)

• JavaScript framework (make all browsers work the same)

• Graded Browser support (give those who can support what they support)

• Developer tools (logger, profiler, test-framework)

Page 74: Creating Happy Little Websites

• Validation and deployment tools (JSLint / YUI packer)

• Modularized with a lazy module and image loading module option.

• Widget framework

• Open Source

• Massive Community and documentation.

Page 77: Creating Happy Little Websites

Questions?Christian Heilmann | http://wait-till-i.com | twitter: codepo8

Thanks to: Apelad (http://www.flickr.com/photos/apelad/2048858745/)