Over a million developers have joined DZone.

10 Tips and Tricks on How to Improve Your Productivity with Coffeescript

DZone's Guide to

10 Tips and Tricks on How to Improve Your Productivity with Coffeescript

· Performance Zone ·
Free Resource

SignalFx is the only real-time cloud monitoring platform for infrastructure, microservices, and applications. The platform collects metrics and traces across every component in your cloud environment, replacing traditional point tools with a single integrated solution that works across the stack.

Post to Twitter

CoffeeScript for those that might not know is a language that compiles into JavaScript. Today I’m going to go over ten things that will help you when working with CoffeeScript.

Tip 1: Splats
Splats in CoffeeScript allow you to have functions that take a variable numbers of arguments. Here is an example:

mySplat = (args...) ->
    console.log "#{args.join()}"



Tip 2: Working with Booleans
For booleans you can use on and yes and they will work just like true, whereas off and no are treated like false. Here is an example of what you can do:

lightSwitch = false
if lightSwitch is off
    console.log 'Turn on the lights!'
    console.log 'Turn off the lights!'


Turn on the lights!

Tip 3: The Existential Operator
The Existential Operator checks for the existence of a variable. Here is an example:

myValue1 = 'Hello World'
if myValue1? then console.log myValue1
# This next variable doesn't exist
if myValue2? then console.log myValue2


Hello World

This also means you can do things like this:

class MyClass
    myFunction1: (val) ->
        console.log "You passed #{val} to myFunction1"
myClass = new MyClass()
# This next function doesn't exist

Since myFunction2 doesn’t exist in MyClass you will only see the following (no errors raised, etc.):

You passed 123 to myFunction1

Tip 4: Using ‘in’ to test if a value is contained in an array
Using the keyword in you can quickly check if a value is inside of an array. Here is an example:

foods = ['apple', 'orange', 'potatoe', 'strawberries']
if 'potatoe' in foods then console.log 'Found potatoe'
if 'carrot' in foods then console.log 'Found carrot'


Found potatoe

Tip 5: Iterating an Object
I’ll create a simple object and show how easy it is to iterate through it getting the keys and values.

dragon =
    level: 1
    alignment: 'neutral'
    age: 'Youngling'
    attack: 'Fire'
    damage: '1d4'
for key, val of dragon
    console.log "#{key}: #{val}"


level: 1
alignment: neutral
age: Youngling
attack: Fire
damage: 1d4

Tip 6: Classes
Class are dead simple. I have an existing article which I will refer you to, however here is a simple example of creating a class and using constructors and inheritance:

class Spaceship
    constructor: (@speed = 1, @spaceShipType = 'spaceship') ->
        console.log "New #{@spaceShipType} created with a speed of #{@speed}"
    move: () ->
        console.log "The #{@spaceShipType} is moving now at a speed of #{@speed}"
class FlyingSaucer extends Spaceship
    constructor: ->
        super 5, 'Flying Saucer'
    useDeathRay: ->
        console.log "The flying saucer is using a death ray!"
spcshp = new Spaceship()
flySaucr = new FlyingSaucer()

Tip 7: A simple shortcut for ‘this’
There is a simple shortcut for this and it is the @ symbol.

class MyClass
    constructor: (@greeting) ->
        console.log "You set the greeting as: #{@greeting}"
    greet: ->
        console.log "You said: #{@greeting}"
myClass = new MyClass('Bonjour')

You can see @greeting is available to the class functions.


You set the greeting as: Bonjour
You said: Bonjour

Tip 8: Default Values
Using the above class I can actually set a default greeting within the constructor (or any other function). Here is the modified example taken from above:

class MyClass
    constructor: (@greeting = 'Hola') ->
        console.log "You set the greeting as: #{@greeting}"
    greet: ->
        console.log "You said: #{@greeting}"
myClass = new MyClass()


You set the greeting as: Hola
You said: Hola

Tip 9: Block Strings
Very easy to use and you don’t have to escape quotes or apostrophes as it is done for you.

markup = """
    <form action="/" method="post">
        <input type="submit" />
console.log markup


<form action="/" method="post">
    <input type="submit" />

Tip 10: String Interpolation
I’ve been using this through several of my examples already. Anything in a double quote will get parsed so you can embed a variable like this:

myValue = 'Hello World'
console.log "You said: #{myValue}"

You can also do things like this though:

val1 = 5;
val2 = 7;
console.log "Your answer for 5 * 7 is: #{val1 * val2}"


Your answer for 5 * 7 is: 35

You could do this as well:

console.log "Your answer for 5 * 7 is: #{5 * 7}"

SignalFx is built on a massively scalable streaming architecture that applies advanced predictive analytics for real-time problem detection. With its NoSample™ distributed tracing capabilities, SignalFx reliably monitors all transactions across microservices, accurately identifying all anomalies. And through data-science-powered directed troubleshooting SignalFx guides the operator to find the root cause of issues in seconds.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}