When comparing CoffeeScript vs Xtend, the Slant community recommends CoffeeScript for most people. In the question“What is the best programming language to learn first?” CoffeeScript is ranked 65th while Xtend is ranked 68th. The most important reason people chose CoffeeScript is:
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.
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 Ruby-like syntactic conveniences
Lambdas are written like Smalltalk's blocks. If it's the last argument, it can go after the parentheses like Ruby's blocks. Parentheses on method calls are optional.
Pro Type inference
It uses Java's static type system, but you don't have to declare the type of everything all the time, since the Xtend compiler can usually figure it out. This also dramatically cuts down on Java's infamous verbosity.
Pro Code runs just as fast as Java
Because Xtend relies heavily on JDK and Android classes, it runs just as fast as native Java code.
Pro Easy to switch back to Java
Xtend is a low-risk option. Because it compiles to human-readable Java, if you decide you don't like it for your project, you can just switch back to Java without losing your work.
Pro Extend even library classes with new methods
This is where it gets its name. You can open classes and add new methods, kind of like Ruby. (Of course this has to be compiled to Java, so really it lives in a kind of helper class.)
Pro Succint
Uses functional features ,which are very concise and idiomatic. Plus it has annotations, which cuts down on the Java boilerplate.
Pro Better defaults than Java
Methods are public if you don't specify, and fields are private. Locals declared with val
in Xtend are final
in Java. This dramatically cuts down on Java's infamous verbosity.
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 Difficult to configure in Android Studio
Con Slower compilation
Unlike most JVM languages, Xtend compiles to Java rather than directly to JVM bytecode. So you have to compile everything four times for Android: from Xtend to Java, from Java to JVM bytecode (.class files), from .class to .dex bytecode, and then AOT compilation from .dex to native ARM upon installation. This can really slow down development and testing vs a more interactive language like Clojure.