CoffeeScript vs Lua
When comparing CoffeeScript vs Lua, the Slant community recommends Lua for most people. In the question“What is the best programming language to learn first?” Lua is ranked 16th while CoffeeScript is ranked 65th. The most important reason people chose Lua is:
One of the best features of Lua is its very well designed C API. This is very useful if you have an existing C library you need to integrate with Lua or quickly get a Lua script running on the C side of the game. Finally Lua plays so nice with C that if you need to optimise for speed you can re-write it in C a lot easier than other languages.
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 Very easy to integrate with C and C++
One of the best features of Lua is its very well designed C API. This is very useful if you have an existing C library you need to integrate with Lua or quickly get a Lua script running on the C side of the game.
Finally Lua plays so nice with C that if you need to optimise for speed you can re-write it in C a lot easier than other languages.
Pro Great documentation
The official Lua documentation is very helpful and thorough. There are also a large number of online resources or books with lots of helpful information for beginners and advanced users alike.
Pro Portable
Lua can be built on any platform with a ANSI C compiler.
Other than that, Lua is extremely small. For example, the tarball for Lua 5.2.1 is only 245K compressed and 960K uncompressed (including documentation).
When built on Linux, the Lua interpreter built with the standard libraries takes 182K and the Lua library takes 243K.
The small size and the ability to build with a C compiler make Lua an extremely portable language that can run on a lot of different systems and computers.
Pro Simple
Easy to learn.
Pro Fast
Lua's performance compares very well to other languages, If performance needs to be further improved you can:
- Implement critical parts in C
- Use the LuaJIT compiler. The LuaJIT compiler is a drop in replacement for the stock compiler and provides significant performance improvements. From the overview page:
LuaJIT speeds can rival code written in C.
Pro Embeddable
Many different game engines (e.g. Elder Scrolls series, ToME) use Lua for scripting, and it's runtime is designed for embedded use.
Pro Clean and simple syntax suitable for beginners
The Lua syntax is modeled from Modula, a language known for being a fantastic introduction to programming.
The Lua syntax also has the following key characteristics:
- Semicolon as a statement separator is optional (mostly used to resolve ambiguous cases as in a = f; (g).x(a)).
- Syntactic sugar for function calls (f'string', f"string", f[[string]], and f{table}) and method calls (obj:m()).
Pro Helpful community
Due to the growing popularity, Lua has a rather large and helpful community surrounding it.
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 Easy to make mistakes when declaring variables
When writing a function, if a programmer forgets to declare a variable, that variable will be declared at global scope. The code will seem to run fine at first, but if another function uses a variable with the same name, but fails to declare it, it will create subtle, incredibly difficult to find bugs.
Con Some concepts may not be applied to other "mainstream" programming languages
Lua features a prototye-based inheritance model. While this is also used by Javascript, it's not used by many other mainstream languages, and so some of the concepts learned while learning Lua won't be very applicable to other languages.
Another thing that makes Lua different from other programming languages, is the fact that Arrays start at 1 instead of 0. While helpful for beginners, it can complicate logic and make it very confusing when switching languages.
Con Batteries not included
Lua is so small mainly due to many of the components not being included in the core package. A lot of people need the functionality provided by the Lua module management system LuaRocks and libraries such as Penlight.