CoffeeScript vs Fay
When comparing CoffeeScript vs Fay, the Slant community recommends Fay for most people. In the question“What are the best solutions to "The JavaScript Problem"?” Fay is ranked 11th while CoffeeScript is ranked 21st. The most important reason people chose Fay is:
Fay produces smaller output than pure Haskell compilers such as GHCJS; It does not need to include the whole Haskell runtime, as it drops support for features such as multi-threading, giving it fewer dependencies.
Specs
Ranked in these QuestionsQuestion Ranking
Pros
Pro Compiles to readable Javascript
With CoffeeScript, there's never really a question of what is going on. If you're worried that something went wrong in the compilation process, the output is very human readable and mostly 1 to 1 with the CoffeeScript code, making debugging easier as the code that is being executed by the interpreter can be double checked.
Pro Widely used
CoffeeScript is the most popular of the compile to Javascript languages, so long term support is much less of a worry than with others.
It also means there are many plugins and tools for integrating it into many different build systems, giving it it nearly universal support.
Pro Lightweight syntax
Javascript is a very verbose language so CoffeeScript's goal is to lighten it to make it less tedious.
Various design choices are built around making CoffeeScript more terse with things like optional parenthesis in function calls, cleaner function declarations, no curly braces, and significant white space. Because Javascript can get pretty deeply nested at times, having a lightweight syntax helps with readability.
The result is a language with a minimalistic syntax with lots of syntax sugar.
Pro It's just JavaScript
The golden rule of CoffeeScript is: "It's just JavaScript". The code compiles one-to-one into the equivalent JS, and there is no interpretation at runtime. You can use any existing JavaScript library seamlessly from CoffeeScript (and vice-versa). The compiled output is readable and pretty-printed, passes through JavaScript Lint without warnings, will work in every JavaScript runtime, and tends to run as fast or faster than the equivalent handwritten JavaScript.
Pro Syntax for humans, not compilers.
CoffeeScript adds syntax that is not only more terse than javascript, smoothing over the rough edges, but also enforces a more human readable syntax to the point where a non-programmer can understand some logic.
Many programmers that are not use to coffeescripts syntax will find it foreign if they don't read the single page API, but generally it is quick to understand and although self documenting code is a myth coffeescript is definitely very close.
if hungry then eat food for food in fridge when food isnt poison and it isnt bedtime
Seems a lot more concise and comprehensible to many programmers and most others than the alternative syntax:
var food, i, len;
if (hungry) {
for (i = 0, len = fridge.length; i < len; i++) {
food = fridge[i];
if (food !== poison && it !== bedtime) {
eat(food);
}
}
}
Pro Function syntax is great for callbacks
Passing functions as callbacks is central to how Javascript is written, but the default syntax for functions is very verbose and hard to read. Various CoffeeScript syntax decisions help with ease of writing and reading functions.
The most obvious change is that the function keyword is changed from function
to ->
. Writing out the word function
is very clunky especially when you need to use multiple nested functions.
One of the more opinionated choices of CoffeeScript is the use of significant whitespace, and optional parens around function arguments. While this can be used poorly, it can also be used to great effect with nested functions. For example, a function that takes an object that defines anonymous functions will end in a mess of parens and curly braces:
asyncAction({
success: function(data){ /* handle data */ },
error: function(error){ /* handle errors */ }
});
In CoffeeScript you could rewrite this as:
asyncAction
success: (data) -> /* handle data */
fail: (error) -> /* handle errors */
Other helpful features are automatic returning of the last statement to make short anonymous functions easier so (a, b) -> a+b
would replace function (a, b) { return a+b; }
, as well as binding functions to the current context object with =>
.
Pro Splats
Because Javascript functions can take variable amounts of parameters, it is helpful to be able to use splats to extract an array of arguments in a function.
For example, if you have a function like: (a, b, rest...) ->
any amount of parameters sent to the function after a
, b
will be stored in an array in the variable rest
. You can also put splats at the start or middle of the arguments list, such as (a, middle..., b) ->
.
When calling a function you can use a splat to apply an array as arguments as well.
Pro Everything is an expression
Even for
loops and if
statements. For example, to get mapped array, you don't have to use any Array methods, just the language features:food = ( stuff for stuff in fridge when stuff.isEatable() )
Pro Familiar to Ruby programmers
CoffeeScript was created by a Ruby programmer and a number of syntax features are modelled on Ruby equivalents, so will be familiar to Ruby programmers. For example, implicit returns, i.e. the last variable of each function is implicitly returned, so "return" keyword need not be present.
Pro Extremely easy to document with literate coffeescript
Skip the documentation build, just write documentation with literate CoffeeScript.
CoffeeScript has a literate mode which let's you use markdown (used by almost everything, such as reddit, github, stack exchange, etc) with code indented how you would normally in markdown and simply enables you to run the code.
This enables you to quickly write FORMATTED, custom documentation that's easily displayed with no build step for the documentation.
Pro Source maps allow you to debug code in CoffeeScript
With source maps, you can get the proper location of where an error occurred directly in precompiled code, making it easier to debug without the tedious step of translating the compiled code back to the original code in your head.
Pro Significant whitespace
Having indention-based code blocks is particularly helpful in JavaScript because of its functional callback based nature. In JavaScript you find yourself writing functions within object and passing functions to functions. You can find statements ending with a confusing melange of braces like )}})})
. With significant whitespace, most of the needs for braces go away.
Pro Default choice for Ruby on Rails
Pro Maintainable code
Easy to read and easy to work with structures like list.
Pro Small output
Fay produces smaller output than pure Haskell compilers such as GHCJS; It does not need to include the whole Haskell runtime, as it drops support for features such as multi-threading, giving it fewer dependencies.
Pro Simple, flexible, hackable FFI
As with UHC, the FFI to Javascript works with printf-style format:
max = ffi "Math.round(%1,%2)"
This can simplify code needed to make calls to methods on objects, in contrast to e.g., Purescript's FFI, which requires that methods be wrapped in Javascript. Similarly to UHC, Fay also supports the use of %*
, for javascript functions with arbitrary numbers of parameters, such as concat
, though they must expose an explicit number of parameters to Fay.
Pro Easy to set up, with packages available on Cabal
Fay is available on Cabal, as are Fay packages, so getting up and running is as simple as typing 'cabal install'. Happstack, Snap, and Yesod packages are available on Hackage, as are bindings for JQuery and Backbone.
Pro Subset of Haskell - nothing new to learn
Since Fay is a subset of Haskell - Lazy, statically typed, and pure by default. There's no new syntax to learn, and no surprises when it comes to the semantics of your code. This extends into function names as well - Fay programmers can use familiar functions such as putStrLn
to output to the console, rather than Javascript-specific versions.
Cons
Con Terse syntax can lead to ambiguity
It can sometimes be hard to be sure of what CoffeeScript will compile down to because of the optional parentheses and significant white spacing. Over multiple lines the same statement can be written in many different ways, and it's not always clear what the intended interpretation is.
For example:foo bar and hello world
can compile to either:
foo(bar) && hello(world)
foo(bar && hello(world))
Con Initializing a variable and assigning it are essentially the same thing
Because of how variables are initialized and reassigned in CoffeeScript, it becomes very easy to accidentally overwrite a variable as the codebase increases. As complexity increases, the only way to safely create a variable is by pressing Ctrl + F and by examining the current file to ensure that there's no conflict.
Con Last expression is returned by default
While this is a pro for small functions, it requires self-discipline to check if unnecessary overheads are introduced:
eat_full = ->
for food in fridge
break if full
cook food if food.requires_cooking()
eat food
This will return array of eat
function results. Can be fixed by adding empty return
at the end.
Con No typeclasses
This can cause some overhead.