C o ff eeScrip t Hector Correa hector@hectorcorrea.com Monday, - - PowerPoint PPT Presentation

c o ff eescrip t
SMART_READER_LITE
LIVE PREVIEW

C o ff eeScrip t Hector Correa hector@hectorcorrea.com Monday, - - PowerPoint PPT Presentation

A decaf introduction to C o ff eeScrip t Hector Correa hector@hectorcorrea.com Monday, June 18, 12 Agenda What is CoffeeScript Why CoffeeScript Code Samples (client and server side) Q&A Monday, June 18, 12 Whats not in


slide-1
SLIDE 1

A decaf introduction to

CoffeeScript

Hector Correa hector@hectorcorrea.com

Monday, June 18, 12

slide-2
SLIDE 2

Agenda

  • What is CoffeeScript
  • Why CoffeeScript
  • Code Samples (client and server side)
  • Q&A

Monday, June 18, 12

slide-3
SLIDE 3

What’s not in the agenda

  • CoffeeScript Installation
  • Specific configurations
  • Node.js
  • Advanced Topics

Monday, June 18, 12

slide-4
SLIDE 4

What is CoffeeScript?

Monday, June 18, 12

slide-5
SLIDE 5

“CoffeeScript is a little language that compiles into JavaScript”

http://coffeescript.org

Monday, June 18, 12

slide-6
SLIDE 6

Type CoffeeScript Code

file: hello.coffee

  • alert 'Hello World!'

Reference JavaScript in HTML

file: index.html

  • <script

type="text/javascript" src="hello.js"> </script>

Compile to JavaScript

file: hello.js

  • alert('Hello World!');

> coffee -c hello.coffee

Monday, June 18, 12

slide-7
SLIDE 7

“CoffeeScript is an attempt to expose the good parts of JavaScript in a simple way.”

http://coffeescript.org

Monday, June 18, 12

slide-8
SLIDE 8

function priceWithTax(price, tax) { return price + (price * (tax / 100)); } price = 100; tax = 6.5; total = priceWithTax(price, tax); alert("Total = " + total);

var not used What happens if another priceWithTax function already exists?

What’s wrong with this code?

Monday, June 18, 12

slide-9
SLIDE 9

Same Code in CoffeeScript

priceWithTax = (price, tax) -> price + (price * (tax/100)) price = 100 tax = 12 total = priceWithTax(price, tax) alert "Total = #{total}"

Monday, June 18, 12

slide-10
SLIDE 10

JavaScript generated by CoffeeScript

All variables are declared (including the function) Local scope has been defined (module pattern)

(function() { var price, priceWithTax, tax, total; priceWithTax = function(price, tax) { return price + (price * (tax / 100)); }; price = 100; tax = 12; total = priceWithTax(price, tax); alert("Total = " + total); }).call(this);

Monday, June 18, 12

slide-11
SLIDE 11

http://coffeescript.org

The golden rule of CoffeeScript is: “It’s just JavaScript”

Monday, June 18, 12

slide-12
SLIDE 12

Why CoffeeScript?

Monday, June 18, 12

slide-13
SLIDE 13

JavaScript development will continue to grow...

Monday, June 18, 12

slide-14
SLIDE 14

JavaScript Growth

“tremendous growth in JavaScript size – up 44% over the course of the year”

http://www.stevesouders.com/blog/2012/02/01/http-archive-2011-recap/

Monday, June 18, 12

slide-15
SLIDE 15

CoffeeScript is a great way to manage this growth

  • Write less code
  • Clean and concise syntax
  • Easier to write good code
  • Produces correct JavaScript (“the

good parts”)

Monday, June 18, 12

slide-16
SLIDE 16

Show me the code

Monday, June 18, 12

slide-17
SLIDE 17
  • Syntactic sugar
  • Classes
  • Other features

Language Features

Monday, June 18, 12

slide-18
SLIDE 18
  • Functions (declaration, callbacks,

default arguments)

  • Significant white space, string

interpolation, equality operator

  • Existential and soak operators
  • Bare objects
  • Bound functions =>

Syntactic Sugar

Monday, June 18, 12

slide-19
SLIDE 19
  • Classes
  • Default arguments
  • Bound methods
  • Inheritance

Classes

Monday, June 18, 12

slide-20
SLIDE 20
  • Destructuring assignment
  • Conditional iteration (unless, while, loop)
  • Loops, arrays, ranges, and

comprehensions

  • Everything is an expression

Other Features

Monday, June 18, 12

slide-21
SLIDE 21

CoffeeScript

  • n the Server Side

Monday, June 18, 12

slide-22
SLIDE 22

node.js

  • A server-side JavaScript interpreter
  • it uses the

V8 JavaScript interpreter that Google wrote for Chrome...

  • ...and repurposes it for use on the

server

http://www.ibm.com/developerworks/opensource/library/os-nodejs/

Monday, June 18, 12

slide-23
SLIDE 23

http://nodejs.org

“Node.js uses an event-driven, non- blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.”

Monday, June 18, 12

slide-24
SLIDE 24

Type CoffeeScript Code

file: hello.coffee

  • console.log 'Hello World!'

Hello World!

Compile to JavaScript and execute on the server (via node.js)

> coffee hello.coffee

Monday, June 18, 12

slide-25
SLIDE 25

Gotchas

  • Significant space will trip you initially
  • Debugging (source maps coming)
  • It’s not a framework, it does not give

structure to your project

Monday, June 18, 12

slide-26
SLIDE 26

Who is using CoffeeScript?

Monday, June 18, 12

slide-27
SLIDE 27

“Trello started out as a pure JavaScript [after experimenting with CoffeeScript] we converted the rest of the code over and started coding CoffeeScript exclusively.”

Brett Kiefer (January, 2012)

http://blog.fogcreek.com/the-trello-tech-stack/

Monday, June 18, 12

slide-28
SLIDE 28

“[Basecamp Next] has just over 5,000 lines of CoffeeScript — almost as much as Ruby! This CoffeeScript compiles to about twice the lines of JavaScript”

David Heinemeier Hansson (January/2012)

http://37signals.com/svn/posts/3094-code-statistics-for-basecamp-next

Monday, June 18, 12

slide-29
SLIDE 29

Summary

Monday, June 18, 12

slide-30
SLIDE 30

“CoffeeScript isn’t just about prettier

  • code. [...]

It’s about being more confident that you got it right the first time, while knowing that you could change it all in just a few keystrokes.”

Trevor Burnham Author of CoffeeScript Accelerated JavaScript Development http://pragprog.com/magazines/2011-05/a-coffeescript-intervention

Monday, June 18, 12

slide-31
SLIDE 31

CoffeeScript allows you to write less, cleaner, and better JavaScript code

tl;dr

Monday, June 18, 12

slide-32
SLIDE 32

More Information

  • Visit http://coffeescript.org
  • Book: CoffeeScript Accelerated JavaScript

Development by Trevor Burnham (The Pragmatic Programmers)

  • Code: https://github.com/hectorcorrea/

intro-to-coffeescript

Monday, June 18, 12

slide-33
SLIDE 33

Questions?

Monday, June 18, 12

slide-34
SLIDE 34

Thanks

hector@hectorcorrea.com

Monday, June 18, 12