front end development best practices

79
Front-end Development Best Practices Back to Basics Karolina Coates, Pádraig Buckley 25/09/2014 Our Awards:

Upload: karolina-coates

Post on 15-Feb-2017

50 views

Category:

Technology


0 download

TRANSCRIPT

Our Awards:

Front-end Development Best PracticesBack to Basics

Karolina Coates, Pádraig Buckley25/09/2014

Our Awards:

Presenter
Presentation Notes
Ireland opening slide

AgendaGeneral guidelinesHTML markupCSSJava ScriptAccessibilityPerformanceCross-browser compatibilityCode ReviewsTools & resources

Introduction to Front End Development

This is not about forcing you to believe what I

believe

Image & slide credit: Christian Heilmann

Introduction

This is the standard we expect.

Please contribute.

General Guidelines

Introduction

General Guidelines

Browser Device Resolution

General Guidelines

• All front-end code should display clear separation of presentation, content,

and behaviour

Presenter
Presentation Notes
You know where to fix a problem when it occurs. Easier maintenance keep their code modular. Changes to one part of the application often inadvertently break unrelated parts downstream.

General Guidelines

Markup

• Markup should be well-formed, semantically correct

– Describe the content rather than the way it is presented.

– Provide Meaning

• Valid

– e.g. W3C Validatior

– Identify missing tags, broken links

Semantic Non-Semantic<form>, <img>, <section> <div>, <span>

Presenter
Presentation Notes
Semantic - Clearly defines its content. Non Semantic- Tells nothing about its content GO through new semantic elements later on with HTML5 – help the page render as intended

General Guidelines

• JavaScript should progressively enhance the experience

– Make a static webpage interactive and enhance usability

– Load on Demand

• Response Times

– Test Everything

• The environments

• The displays

Presenter
Presentation Notes
 This makes the third level menu inaccessible to mouse users (keyboard users — for once — wouldn't be that bothered). If you try to scroll the document to reach the third menu level, the menu will collapse again — a frustrating experience. Load on Demand – retrieve code as necessary Response times 0.1 – user will feel the page is responding instantaneously 1.0 second is about the limit for the user's flow of thought to stay uninterrupted, even though the user will notice the delay. NetForecast’s APDEX uses 4.0 seconds as the dividing line as to where users become frustrated.
Presenter
Presentation Notes
Defines the content of a web page

HTML Mark-up

General Guidelines

• Doctype

– <!DOCTYPE html>

• Structure and Outline of a Document

• Don’t use tables for layout

• Use <p> instead of </br>, &nbsp

Presenter
Presentation Notes
Doctype has been simplified Tables are still used for tabular data layout. Not good for responsive websites search engines to identify the correct web page content.

HTML 5

General Guidelines

• Comment on closing tags to indicate what element you are closing

• Continuously Test

• Readability vs Compression

– Maintenance

HTML 5

New Form Types

• Email, date, range, tel, required

• For further info, go to

– http://www.w3schools.com/html/html5_intro.asp

Presenter
Presentation Notes
Add a sample next to this
Presenter
Presentation Notes
control over the look and feel of their web sites.

CSS

Best Practices

• Create a Style guide

– Headings

– Buttons

– Colour Scheme etc.

Presenter
Presentation Notes
Headings - Declare these at the top of CSS document and modify as necessary

CSS

General Guidelines

– Use External style sheets

– Include CSS files in the HEAD of the document

– Establish standards for coding style and practices

– Ensure consistency across browsers

• normalize.css to make rendering more consistent across browsers.

Presenter
Presentation Notes
Add CSS through external files, minimizing the number of files, if possible. Insert image Inline should be avoided – easier to maintain and update normalize.css – consistent rendering - http://necolas.github.io/normalize.css/

CSS

Inline CSS

CSS

Formatting of CSS

Presenter
Presentation Notes
Improve readability and comprehension Prevents lines from growing too long Each selector on one line Each property on one line

CSS

CSS Box Model

padding: 20px 80px 10px 40pxmargin: 20px 80px 10px 40px;

Presenter
Presentation Notes
Explain if these values are undeclared What is the difference?

CSS

Clearfix

Presenter
Presentation Notes
Ref- http://learnlayout.com/clearfix.html

CSS

Clearfix

CSS

Clearfix

Presenter
Presentation Notes
http://getbootstrap.com/css/#helper-classes-clearfix

CSS

Selectors

• Naming conventions

– Use a name that describes what it is rather than what it looks like

• Classes Vs ID’s

• Selectors

• .class and #id

• Grouping Selectors

Presenter
Presentation Notes
ID’s for elements if they are unique Classes if they can be reused use the id selector when you want to find a single, unique element.

CSS

• Specificity

Presenter
Presentation Notes
Ranking from Inline, ID, Class, Elements If the element has inline styling, that automatically1 wins (1,0,0,0 points) For each ID value, apply 0,1,0,0 points For each class value (or pseudo-class or attribute selector), apply 0,0,1,0 points For each element reference, apply 0,0,0,1 point �For More Info Look up CSS tricks - http://css-tricks.com/specifics-on-css-specificity/

CSS

Specificity Example

CSS

Specificity Example

Presenter
Presentation Notes
What CSS rule has a power to override them all?

CSS

Which CSS rule has a power to override them all?

CSS

!Important Tag

• When to use

• Never unless they are absolutely necessary

• Harder to maintain style sheets

Presenter
Presentation Notes
Without the use of !important, there are two reasons why the second declaration block should naturally have more weight than the first: The second block is later in the stylesheet (i.e. it’s listed second). Also, the second block has more specificity (#container followed by #example instead of just #example). But with the inclusion of!important, the first font-size rule now has more weight. In the CSS cascade,inline styles will override regular styles, 

CSS

! Important Tag

• When it can be used

– Temporarily fix an urgent problem

– Override inline styles

CSS Inheritance & Cascading

• Styles can be inherited from a parent

• Overriding styles

Presenter
Presentation Notes
http://webdesignfromscratch.com/html-css/css-inheritance-cascade/ Switch out to code pen for an example

CSS

Developing Responsive Applications

and many more… google it

Presenter
Presentation Notes
Responsive Mobile Friendly Applications http://getbootstrap.com/ Bootstrap, Boilerplate etc. Don’t start from scratch. Take code from other souces. Don’t try and reinvent the wheel.

CSS

Media Queries

• Change styles depending on a number of properties

– Size

– Resolution

– Orientation

Presenter
Presentation Notes
 enhances the overall user and browser-based experience  Can manipulate the CSS and HTML markup

Java Script

Should progressively enhance the experience

• General Guidelines

– Stored in an External .js file

– Include at the end of the document

– Comment your code and use Whitespace

– Formatting of JavaScript Code

– Validate your code

• http://www.jslint.com/

Presenter
Presentation Notes
Consistent formatting makes code more readable, and also means the code can be easily modified with find and replace commands. Usable and Interactive 

Java Script

General Guidelines

• Never End a Definition with a comma

• Use === Comparison

– The == comparison operator always converts (to matching types) before

comparison.

– The === operator forces comparison

of values and type:

Java Script

Global Variables

Presenter
Presentation Notes
Variables that exist throughout the script are called Global variables. Their values can be changed anytime in the code and even by other functions.

Java Script

Global Variables

• Global Scope Vs Local Scope

• Minimize the number of global variables

• Local variables are not available outside the function

Presenter
Presentation Notes
A variable declared inside of a function is local to that function and not available outside the function. On the other hand, global variables are those declared outside of any function or simply used without being declared. Naming collisions Accessible by 3rd party JavaScript libraries

Java Script

• Variables

– Lowercase or camelCase

• Name Logically

– popUpWindowForAd rather than myWindow

• Bad Examples

– X1, createNewMemberIfAgeOverTwentyOneAndMoonIsFull

Presenter
Presentation Notes
Variables should be easier to understand

Java Script

Avoid Mixing Technologies

Java Script

Over Reliance on JavaScript

Presenter
Presentation Notes
What Happens if JavaScript isn’t enabled – terrible user experience. Elements that are dependent on JavaScript. Use html mark-up when available http://www.slideshare.net/cheilmann/javascript-best-practices-1041724

Java Script

Debugging

– Can help refine code and loading speed

– Use developer tools

Java Script

Scripts

• Plugins – plugin.js and script.js

– Advantage

• Give the site greater functionality/ Dynamic

– Disadvantage

• Too many plugins can have a negative effect on performance

Presenter
Presentation Notes
Plugin.js –hold all your site plugins code. An extremely large plugin only used on one rarely visited page, for example, might be better off in a separate download Script.js hold your application code. The larger the project, it may be more benefical to break out the code into module/feature specific code

Accessibility

Image credit: Travelbag Ltd (https://www.flickr.com/photos/98585738@N07/10346101216

Accessibility

Reality check: Accessibility rights are growing teeth

Image credit: Travelbag Ltd https://www.flickr.com/photos/98585738@N07/10346101216

Accessibility

• Web Content Accessibility Guidelines (WCAG)

• Ireland Disability Act 2005:

Where a public body communicates in electronic form, the communication must be as far as practicable, "accessible to persons with a visual impairment to whom adaptive technology is available"

“• W3C Web Accessibility initiative – Easy Checks:

http://www.w3.org/WAI/eval/preliminary

Accessibility – Easy Checks

Page title

Image text alternatives ("alt text")<img src="pointer_to_image.png" alt=“describe the meaning of the image">

Correct markup http://validator.w3.org/

Accessibility – Easy Checks – cont.

Color contrast

Color Contrast Checker http://webaim.org/resources/contrastchecker/

Accessibility – Easy Checks – cont.

Headings

[…] etc.

Accessibility

Code checks do not guarantee that people with

disabilities will be able to use your website.

• Requirements need to be defined by the customer

• Train developers and testers to evaluate

• Build best practice into development process

• Use accessibility evaluation tools

• Involve users in evaluating

Performance

Performance

Follow Yahoo Performance Guidelines

• Put stylesheet links at the top

(HEAD)

• Put scripts at the bottom

• Minify JS and CSS files

• Automate the two above

• Optimise JavaScript execution

• Reduce number of HTTP requests

– Combine CSS files together

– Combine JS scritps

– Use CSS sprites

– Caching

• Use a Content Delivery Network

• Reduce image size (px and KB)

Presenter
Presentation Notes
Mention Pawel Kalbrun’s session on the 9th Oct

Test it - YSlow, Page Speed, Hammerhead, MSFast, …

Performance - Image size (px)

Live demo

Presenter
Presentation Notes
https://intranet.version1.com/accounts_team/Public%20Wiki/Home.aspx

Performance – Image size (KB)

Image formats – JPG vs GIF vs PNG

Performance

Image formats – a quiz!Match a file format with use scenario:

Size matters + = ?

Size matters + = ?

+ transparent background = ?

Image credit: Huffingtonpost http://www.huffingtonpost.com/2011/05/31/the-hardest-simpsons-quiz-of-all-

Presenter
Presentation Notes
Answers: 1. GIF / SVG, 2. JPG, 3. PNG / GIF

Performance – CSS spritesPerformance – CSS sprites

Image credit:Steve Jurvetson https://www.flickr.com/photos/jurvetson/10793760553

Performance – CSS sprites

Image credit: DryBowser455 http://s1037.photobucket.com/user/DryBowser455/media/NyanCatSprite.png.html, Giphy.com

=

Performance – CSS sprites

Image credit: Kasey Jean Robinson http://www.kaseyjeanrobinson.com/tech/what-is-a-css-sprite/

Performance – CSS sprites

Image credit: CSS Tricks http://css-tricks.com/css-sprites/

Performance – CSS sprites

Create manually:

• SpritePad

• ZeroSprites

• SpriteCow

• … and many more – google it!

OR automate:

• grunt-spritesmith

• node-spritesheet

• dr-grunt-svg-sprites

• ... and many others.

Performance

• Performance can be subjective

• It’s about managing customer’s expectations – agree what’s acceptable.

• Communicate performance impact during design phase or changes

Things to remember:

Cross-Browser Compatibility

Cross-Browser Compatibility

What do we support?

There are two major truths when it comes to in-browser experience:

1. Everyone wants the best experience possible (support them all!).

2. Everything added to the page slows it down.

Cross-Browser Compatibility

What do we support?

Cross-Browser Compatibility

Browsers

Devices

Resolutions

Cross-Browser Compatibility

Image credit: Gideon https://www.flickr.com/photos/malias/73169727

Cross-Browser Compatibility

Define & communicate what you support

• Time

• Effort

• Cost

Image credit: Jess

Presenter
Presentation Notes
We all know the offenders

Cross-Browser Compatibility

Testing is the key

Emulators vs virtual machines vs actual device

https://modern.ieFirefox portable appsOpera archiveChome updates itself

Devs and testers should have access to the same setup

Code Reviews

Code Reviews

• It's about mitigating risk

• Catches bugs that can affect multiple pages

• It’s ‘you time’ and it’s about your professional development

Why do it

Peer code reviews are the single biggest thing you can do to improve your code

- Jeff Atwood (Coding Horror)“

Final word

Code Reviews

The Internet is a cobweb of different technologies cobbled together with duct tape, string and chewing gum. It's not elegantly designed in any way, because it's more of a growing organism than it is a machine constructed with intent.

- Mattias Petter Johansson, Developer at Spotify

Image credit: Daniel Levy http://www.bedeliberate.com/use-giants-to-see-further/

-GOOGLE IT

Tools & Resources

Tools & Resources

• W3C CSS Validation Service

• HTML Validation firefox extension

• CSS validator

Validators

Browser Developer Tools

• Chrome Developer Tools

• IE Developer Tools (F12)

• Firefox Firebug

• Opera Dragonfly

CSS Sprite generators

Manual:

• SpritePad

• ZeroSprites

• SpriteCow

Automatic:

• grunt-spritesmith

• node-spritesheet

• dr-grunt-svg-sprites

• ... and many others.

Front-end templates & frameworks

• http://cssframeworks.org/

• Bootstrap

• HTML5 Boilerplate

• Topcoat

References

• http://isobar-idev.github.io/code-standard

• http://jstherightway.org/