/ coffeescript

Stuff you might didn't know about CoffeeScript

CoffeeScript is a language that is falling out in popularity these days. Of course, with the arrival of ES6 doesn't make a lot of sense, does it? Wow, that wasn't a good way to start a post. Moving on... Personal list of CoffeeScript cool stuff.

coffeescript-nooice.png

Chained Comparisons

I worked a lot with Python and unfortunately didn't know back then that Python also haves this.

Makes checking if a value is between a range easier, and works great!

a = 10
b = 5 < a < 30

Compiles to:

var a, b;

a = 10;
b = (5 < a && a < 30);

Fact: Chained comparison is a feature of BCPL, since the late 1960s.source.

Scopes

For function definitions: A function defined with -> compiles to a regular JavaScript function. But, like in ES6, a function defined by a => (fat arrow) preserves the context (binding this).

For values: While ES6 added the let assignment for locally scoped variables. Every variable in CoffeeScript will compile to var. There's a discussion in the CoffeeScript's repository that intends to add support for it. [The same happens with]((https://github.com/jashkenas/coffeescript/issues/3571) const.

Generators

Generators in CoffeeScript are like in ES6 but removing the distinction between regular functions. If there's a yield in the function body, it will compile to a function generator.

hello = () ->
  yield 'Hello!'

Compiles to:

var hello;

hello = function*() {
  yield 'Hello!';
}

Destructuring Assignment

The same as ES6 Destructuring Assignment.


gimmeAnArray = (stuff) ->
  [stuff, "BOWIE", 61]

[stuff, myText, myNumber] = gimmeAnArray "Stuff"

console.log stuff    # -> "Stuff"
console.log myText   # -> "BOWIE"
console.log myNumber # -> 61

Splats...

Works like ES6 Spread operator.

Switch

From their documentation: "Switch statements can also be used without a control expression, turning them into a cleaner alternative to if/else chains."

And they give you this example:

score = 76
grade = switch
  when score < 60 then 'F'
  when score < 70 then 'D'
  when score < 80 then 'C'
  when score < 90 then 'B'
  else 'A'
# grade == 'C'

I like to use it as an if/else chain alternative with a control expression. Like this:

msg = switch order.status
  when 'approved' then 'Cart has been Approved'
  when 'rejected' then 'Cart has been Rejected'
  else 'Cart has been Reviewed'

Instead of this:

templateName = 'Cart has been Reviewed'

if order.status is 'rejected'
  templateName = 'Cart has been Rejected'

if order.status is 'approved'
  templateName = 'Cart has been Approved'

Array Comprehensions

Like Python's array comprehension. CoffeeScript suggests in their documentation that this is how you will write most fors.

# Eat lunch.
eat food for food in ['toast', 'cheese', 'wine']

# Fine five course dining.
courses = ['greens', 'caviar', 'truffles', 'roast', 'cake']
menu i + 1, dish for dish, i in courses

# Health conscious meal.
foods = ['broccoli', 'spinach', 'chocolate']
eat food for food in foods when food isnt 'chocolate'

(Example from CoffeeScript documentation).

Is this on ECMAScript? It could be part of ES7 but in MDN worlds: Non-standard. Do not use! The array comprehension is non-standard, and it's unlikely to be added to ECMAScript.

coffeescript-wat.png

CoffeeScript will build code for ES5 but using a transpiler like Babel will also allow you to write ES6 (or ES7) code and compile to ES5. So, what's better? Well...

JavaScript is better.

Even that great projects like Atom or Hubot are using CoffeeScript. If I had to choose between CoffeeScript or ES6 I'd definitely go for plain JavaScript.

Basically, same features with less nonsense. Plus scoping is clean and flexible with standard declarations (see let, var, const).

But, if you are using CoffeeScript, I'm highly encourage using CoffeeLint. Keep it consistent, mate.

Happy new year!

happy-new-year