When comparing CoffeeScript vs Erlang, the Slant community recommends Erlang for most people. In the question“What is the best programming language to learn first?” Erlang is ranked 44th while CoffeeScript is ranked 65th. The most important reason people chose Erlang is:
Erlang has strong roots with the telecom industry in which concurrent processes are normal. It's designed to be concurrent, to be used for distributed computing and to be scalable.
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 Built from the ground up with concurrency and distributed computing in mind
Erlang has strong roots with the telecom industry in which concurrent processes are normal. It's designed to be concurrent, to be used for distributed computing and to be scalable.
Pro Fault-tolerant
Fault tolerance means that a system has the property to continue operating even though one or more components have failed.
For Erlang systems, this means that the system is kept running even if for example a user has to drop a phone call rather than forcing everyone else to do so.
In order to achieve this, Erlang's VM gives you:
- Knowledge of when a process died and why that happened
- The ability to force processes to die together if they depend on each other and if one of them has a fault.
- A logger that logs every uncaught exception
- Nodes that can be monitored so that you find out when they go down
- The ability to restart failed processes (or groups of them)
Pro Upgrade code without stopping the system
In a real-time system it may not be possible to stop the system in order to implement code upgrades. For these cases Erlang gives you dynamic code upgrade support for free when using OTP. The mechanism is very easy to understand and works as follows:
- Start the app
- Edit the code
- Recompile
That's all that is needed, the app updates with the new code while it's still running and tests are run automatically.
Pro Great for writing distributed applications
Erlang is made to be parallel and distributed. Because it's very easy to write code that uses multiple processor cores, it's also very easy to write applications that span multiple servers.
Pro Battle proven
Erlang has been used in production for more than 20 years now. During that time it has proven itself over and over again that works great in both small startups and large-scale enterprise systems.
Erlang has been used extensively by Ericsson themselves. For example, the AXD301 ATM, which is one of Ericsson's flagships is probably the largest Erlang project ever with more than 1.1 million lines of Erlang code.
Pro Light processes
Erlang's processes have very little overhead (about 500 bytes per process). This means that a huge amount of processes can be created, even on older machines.
Pro Consistency across platforms
Erlang's processes run in a complete independent way from the OS (they aren't managed by the OS scheduler neither). This means that programs written in Erlang will run exactly the same way regardless of the operating system or platform.
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 Eccentric syntax
Erlang's syntax may feel very strange to 99% of programmers who have never used it. This is because it does not share any similarities or common syntax definitions that are found in all the other languages that are used today.
Con Useful in only one niche
Erlang is not really a general purpose language. It has a very special and well-defined niche where it towers above everything else. It's specialized in scalability and in distributed applications. Which is not necessarily a bad thing per se, but it still lacks and falls behind other languages when it needs to do things outside it's niche.