c eescrip - hectorcorrea.com · same code in coffeescript pricewithtax = (price, tax) -> price +...

34
A decaf introduction to CoeeScript Hector Correa [email protected] Monday, June 18, 12

Upload: others

Post on 17-Oct-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: C eeScrip - hectorcorrea.com · Same Code in CoffeeScript priceWithTax = (price, tax) -> price + (price * (tax/100)) price = 100 tax = 12 total = priceWithTax(price, tax) alert "Total

A decaf introduction to

CoffeeScript

Hector [email protected]

Monday, June 18, 12

Page 2: C eeScrip - hectorcorrea.com · Same Code in CoffeeScript priceWithTax = (price, tax) -> price + (price * (tax/100)) price = 100 tax = 12 total = priceWithTax(price, tax) alert "Total

Agenda

• What is CoffeeScript

• Why CoffeeScript

• Code Samples (client and server side)

• Q&A

Monday, June 18, 12

Page 3: C eeScrip - hectorcorrea.com · Same Code in CoffeeScript priceWithTax = (price, tax) -> price + (price * (tax/100)) price = 100 tax = 12 total = priceWithTax(price, tax) alert "Total

What’s not in the agenda

• CoffeeScript Installation

• Specific configurations

• Node.js

• Advanced Topics

Monday, June 18, 12

Page 4: C eeScrip - hectorcorrea.com · Same Code in CoffeeScript priceWithTax = (price, tax) -> price + (price * (tax/100)) price = 100 tax = 12 total = priceWithTax(price, tax) alert "Total

What is CoffeeScript?

Monday, June 18, 12

Page 5: C eeScrip - hectorcorrea.com · Same Code in CoffeeScript priceWithTax = (price, tax) -> price + (price * (tax/100)) price = 100 tax = 12 total = priceWithTax(price, tax) alert "Total

“CoffeeScript is a little language that compiles into JavaScript”

http://coffeescript.org

Monday, June 18, 12

Page 6: C eeScrip - hectorcorrea.com · Same Code in CoffeeScript priceWithTax = (price, tax) -> price + (price * (tax/100)) price = 100 tax = 12 total = priceWithTax(price, tax) alert "Total

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 toJavaScript

file: hello.js--------------alert('Hello World!');

> coffee -c hello.coffee

Monday, June 18, 12

Page 7: C eeScrip - hectorcorrea.com · Same Code in CoffeeScript priceWithTax = (price, tax) -> price + (price * (tax/100)) price = 100 tax = 12 total = priceWithTax(price, tax) alert "Total

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

http://coffeescript.org

Monday, June 18, 12

Page 8: C eeScrip - hectorcorrea.com · Same Code in CoffeeScript priceWithTax = (price, tax) -> price + (price * (tax/100)) price = 100 tax = 12 total = priceWithTax(price, tax) alert "Total

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

Page 9: C eeScrip - hectorcorrea.com · Same Code in CoffeeScript priceWithTax = (price, tax) -> price + (price * (tax/100)) price = 100 tax = 12 total = priceWithTax(price, tax) alert "Total

Same Code in CoffeeScript

priceWithTax = (price, tax) -> price + (price * (tax/100)) price = 100tax = 12total = priceWithTax(price, tax)

alert "Total = #{total}"

Monday, June 18, 12

Page 10: C eeScrip - hectorcorrea.com · Same Code in CoffeeScript priceWithTax = (price, tax) -> price + (price * (tax/100)) price = 100 tax = 12 total = priceWithTax(price, tax) alert "Total

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

Page 11: C eeScrip - hectorcorrea.com · Same Code in CoffeeScript priceWithTax = (price, tax) -> price + (price * (tax/100)) price = 100 tax = 12 total = priceWithTax(price, tax) alert "Total

http://coffeescript.org

The golden rule of CoffeeScript is:

“It’s just JavaScript”

Monday, June 18, 12

Page 12: C eeScrip - hectorcorrea.com · Same Code in CoffeeScript priceWithTax = (price, tax) -> price + (price * (tax/100)) price = 100 tax = 12 total = priceWithTax(price, tax) alert "Total

Why CoffeeScript?

Monday, June 18, 12

Page 13: C eeScrip - hectorcorrea.com · Same Code in CoffeeScript priceWithTax = (price, tax) -> price + (price * (tax/100)) price = 100 tax = 12 total = priceWithTax(price, tax) alert "Total

JavaScript development will continue to grow...

Monday, June 18, 12

Page 14: C eeScrip - hectorcorrea.com · Same Code in CoffeeScript priceWithTax = (price, tax) -> price + (price * (tax/100)) price = 100 tax = 12 total = priceWithTax(price, tax) alert "Total

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

Page 15: C eeScrip - hectorcorrea.com · Same Code in CoffeeScript priceWithTax = (price, tax) -> price + (price * (tax/100)) price = 100 tax = 12 total = priceWithTax(price, tax) alert "Total

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

Page 16: C eeScrip - hectorcorrea.com · Same Code in CoffeeScript priceWithTax = (price, tax) -> price + (price * (tax/100)) price = 100 tax = 12 total = priceWithTax(price, tax) alert "Total

Show me the code

Monday, June 18, 12

Page 17: C eeScrip - hectorcorrea.com · Same Code in CoffeeScript priceWithTax = (price, tax) -> price + (price * (tax/100)) price = 100 tax = 12 total = priceWithTax(price, tax) alert "Total

• Syntactic sugar

• Classes

• Other features

Language Features

Monday, June 18, 12

Page 18: C eeScrip - hectorcorrea.com · Same Code in CoffeeScript priceWithTax = (price, tax) -> price + (price * (tax/100)) price = 100 tax = 12 total = priceWithTax(price, tax) alert "Total

• 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

Page 19: C eeScrip - hectorcorrea.com · Same Code in CoffeeScript priceWithTax = (price, tax) -> price + (price * (tax/100)) price = 100 tax = 12 total = priceWithTax(price, tax) alert "Total

• Classes

• Default arguments

• Bound methods

• Inheritance

Classes

Monday, June 18, 12

Page 20: C eeScrip - hectorcorrea.com · Same Code in CoffeeScript priceWithTax = (price, tax) -> price + (price * (tax/100)) price = 100 tax = 12 total = priceWithTax(price, tax) alert "Total

• Destructuring assignment

• Conditional iteration (unless, while, loop)

• Loops, arrays, ranges, and comprehensions

• Everything is an expression

Other Features

Monday, June 18, 12

Page 21: C eeScrip - hectorcorrea.com · Same Code in CoffeeScript priceWithTax = (price, tax) -> price + (price * (tax/100)) price = 100 tax = 12 total = priceWithTax(price, tax) alert "Total

CoffeeScript on the Server Side

Monday, June 18, 12

Page 22: C eeScrip - hectorcorrea.com · Same Code in CoffeeScript priceWithTax = (price, tax) -> price + (price * (tax/100)) price = 100 tax = 12 total = priceWithTax(price, tax) alert "Total

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

Page 23: C eeScrip - hectorcorrea.com · Same Code in CoffeeScript priceWithTax = (price, tax) -> price + (price * (tax/100)) price = 100 tax = 12 total = priceWithTax(price, tax) alert "Total

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

Page 24: C eeScrip - hectorcorrea.com · Same Code in CoffeeScript priceWithTax = (price, tax) -> price + (price * (tax/100)) price = 100 tax = 12 total = priceWithTax(price, tax) alert "Total

Type CoffeeScript Code

file: hello.coffee------------------console.log 'Hello World!'

Hello World!

Compile toJavaScript

and execute on the

server (via node.js)

> coffee hello.coffee

Monday, June 18, 12

Page 25: C eeScrip - hectorcorrea.com · Same Code in CoffeeScript priceWithTax = (price, tax) -> price + (price * (tax/100)) price = 100 tax = 12 total = priceWithTax(price, tax) alert "Total

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

Page 26: C eeScrip - hectorcorrea.com · Same Code in CoffeeScript priceWithTax = (price, tax) -> price + (price * (tax/100)) price = 100 tax = 12 total = priceWithTax(price, tax) alert "Total

Who is using CoffeeScript?

Monday, June 18, 12

Page 27: C eeScrip - hectorcorrea.com · Same Code in CoffeeScript priceWithTax = (price, tax) -> price + (price * (tax/100)) price = 100 tax = 12 total = priceWithTax(price, tax) alert "Total

“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

Page 28: C eeScrip - hectorcorrea.com · Same Code in CoffeeScript priceWithTax = (price, tax) -> price + (price * (tax/100)) price = 100 tax = 12 total = priceWithTax(price, tax) alert "Total

“[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

Page 29: C eeScrip - hectorcorrea.com · Same Code in CoffeeScript priceWithTax = (price, tax) -> price + (price * (tax/100)) price = 100 tax = 12 total = priceWithTax(price, tax) alert "Total

Summary

Monday, June 18, 12

Page 30: C eeScrip - hectorcorrea.com · Same Code in CoffeeScript priceWithTax = (price, tax) -> price + (price * (tax/100)) price = 100 tax = 12 total = priceWithTax(price, tax) alert "Total

“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 BurnhamAuthor of CoffeeScript Accelerated JavaScript Developmenthttp://pragprog.com/magazines/2011-05/a-coffeescript-intervention

Monday, June 18, 12

Page 31: C eeScrip - hectorcorrea.com · Same Code in CoffeeScript priceWithTax = (price, tax) -> price + (price * (tax/100)) price = 100 tax = 12 total = priceWithTax(price, tax) alert "Total

CoffeeScript allows you to write less, cleaner, and better

JavaScript code

tl;dr

Monday, June 18, 12

Page 32: C eeScrip - hectorcorrea.com · Same Code in CoffeeScript priceWithTax = (price, tax) -> price + (price * (tax/100)) price = 100 tax = 12 total = priceWithTax(price, tax) alert "Total

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

Page 33: C eeScrip - hectorcorrea.com · Same Code in CoffeeScript priceWithTax = (price, tax) -> price + (price * (tax/100)) price = 100 tax = 12 total = priceWithTax(price, tax) alert "Total

Questions?

Monday, June 18, 12