Skip to content

Instantly share code, notes, and snippets.

@benjie
Last active November 4, 2016 15:43
Show Gist options
  • Save benjie/57a2e893d07fee32b1bd to your computer and use it in GitHub Desktop.
Save benjie/57a2e893d07fee32b1bd to your computer and use it in GitHub Desktop.
Reasons CoffeeScript is still relevant for me

I'm often asked why I still use CoffeeScript rather than ES6/7. Well, in truth I use both, but I do still prefer CoffeeScript for the following reasons:

  • Correctly trimmed multiline strings (ES6 includes the indentation because it doesn't have significant whitespace)
  • Existential operator (a?.b; b?(); a?.b?() and of course a ? b)
    • a ? b is more appropriate than a || b if "" or 0 are values of a you wish to persist
    • (a && a.b && typeof a.b === 'function') ? a.b() : undefined doesn't quite have the same ring to it as a?.b?()
  • Significant whitespace
    • (a personal preference, but since you already lay your code out sensibly why do you need the additional visual distraction of braces?)
  • Convenient shorthand for common things (-> (regular non-bound function), @ (this.), :: (.prototype.))
  • in to check if something in a list
    • a in b is a lot clearer and harder to mess up than b.indexOf(a) >= 0
  • Chained comparisons a < b < c (minor)
  • Ranges [a..b] e.g. in loops for a in [1..10]
  • Shorthand slicing a[b..c] / a[..] (minor)
  • Block regexps (a very good way to allow documenting of complex regexps)
  • Safe switch statements (never forget a break again; minor since this can be linted against)

For more you might be interested in my Long Live CoffeeScript and Long Live ES6 post.

There's a lot to like about ES6/7 though; here's some things that I miss from Babel when writing CoffeeScript:

  • let / const; and to be honest var (CoffeeScript should support an initial assignment operator such as a := b)
  • The ternary operator a ? b : c (CoffeeScript doesn't have this because of the existential operator and implicit object syntax; however I think that could be solved by switching to a ?? b and giving the ternary operator higher precedence than implicit objects?)
  • spreading objects {...a, b: 'c'} (CoffeeScript has spread for arrays, but not for objects; easy to work around with Object.assign({}, a, b: 'c') but I like sugar)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment