HubSpot Engineering

Latest posts

Writing lately

CoffeeScript Tricks

In this post

At HubSpot, I'm fortunate to work with some CoffeeScript experts. I've picked up a few tricks, most of which aren't mentioned in the docs.

#Try Without Catch

One of my first confusions with CoffeeScript was why it always required you to assign the 'catched' error to something, you couldn't have a catch and just drop the error object on the floor. Turns out you can do one better:

try  thisMightThrowAnError() butThisDoesntCare()

#Destructuring Arguments

It's common in CoffeeScript, as in Javascript, to pass objects as options to methods. This get's even easier when you use destructing assignment. This is a great way to get the clean binding of positional arguments while maintaining the flexibility of an object.

myMethod = ({size, weight}) ->  @cost = size * weight

You can couple that with the 'this' shorthand:

class Ball  constructor: ({@size}) ->

It gets even better when you use the object shorthand notation:

size = 55new Ball {size}

The shorthand style is particularly nice when it comes time for you to define module.exports:

module.exports = {start, stop}

You can even use the shorthand with object properties:

return {@id}

#Static Methods

In Javascript, functions are objects, and classes are just functions, meaning you can attach static methods to your classes:

CoffeeScript
class MyModel MyModel.get = _.memoize ->  new MyModel

It turns out that CoffeeScript has a syntax for just this situation:

class MyModel  @get: _.memoize ->    new MyModel

#Anonymous Functions

One of the classic bugs folks encounter in Javascript is forgetting that only functions have scope, so anything assigned in a for loop, for example, may have changed when it comes time for callbacks to fire. The do keyword can be used to call a function immediately, passing it it's arguments from the outer scope.

for dog in dogs  do (dog) ->    setTimeout ->      console.log "Dog:", dog

You can use the default argument value to create variables which will only live in the inner scope.

do (x=3) -> letsRock(x)

You can also use this trick to call functions as you create them:

do check = ->  if waitingOn is 0   done() setTimeout check, 50

#Reverse Iteration

Since CoffeeScript 1.5.0 it's been possible to iterate through a list in reverse:

for car in cars by -1  start(car)

#Splats

Most CoffeeScript developers know about splats, but not all know that they can appear anywhere in the argument sequence.

(name, children..., cb) ->

For one of our favorite CoffeeScript tricks, take a look at Mixen.

Newsletter

For Agents

View Markdown

Share

More posts

Moving Mountains: How We Migrated from Enzyme to React Testing Library

How we migrated 76,000 tests from Enzyme to React Testing Library, modernizing our stack and allowing teams to ship changes with more confidence.

A Tech Guide to Web Accessibility: Why It Matters and How to Improve It

Web accessibility is a crucial aspect of building modern websites and applications, which helps ensure that people with disabilities have access to the same information and services as people without disabilities.

Immutability and Its (Potential) Impact on Performance

We recently encountered a performance bug that challenged the use of immutability in our frontend applications.