Skip to content

Instantly share code, notes, and snippets.

@balupton
Created May 18, 2011 13:44
Show Gist options
  • Save balupton/978585 to your computer and use it in GitHub Desktop.
Save balupton/978585 to your computer and use it in GitHub Desktop.
JavaScript VS CoffeeScript

CoffeeScript VS JavaScript

The code examples are actually taken directly from an early version of DocPad before and after tha javascript rewrite. We now handle these cases a lot better in terms of callback hell by utilising task groups with bal-util, however the issue of the readability and error proneness of the verbose syntax in javascript is key these code examples.

There is also a larger issue that I see at the core of JavaScript, which is detailed in the following email response:

Question: Would ES6's implementation of easier classes in JavaScript, make you consider moving back to JavaScript from CoffeeScript?

Answer: Well there are lot more issues in JavaScript than its current complexity with writing code.

Sure one advantage of CoffeeScript, is that it is easier to write, understand, and learn than JavaScript thanks to its syntactical sugar. For instance, doing object orientated code with JavaScript you still need to do horrible things to emulate a super call. Cycling through arrays and objects in JavaScript natively is also a pain in the ass, unless of course you use underscore or the like or have access to .forEach which just adds to the complexity of JavaScript - CoffeeScript does not have this issue.

This is even exemplified by the simple task of writing JavaScript, as I find that I'll spend most of my time thinking "how on earth am I going to accomplish this correctly, what is the best way out of all my vast possible options, is this actually going to do what I want it to do every time, did I forget any syntax that will bite me on the ass now or later" - whereas with CoffeeScript I never really have to ask those questions as the complexity barrier is next to none, I can just focus always on what to do next.

Now, I love JavaScript, I've used it professionally for many years and I respect both languages deeply. However, I find all this complexity with JavaScript, while powerful and robust, just gets in the way. It's like being offered a billion dollars then having every salesman in the world call you up, it's not going to make you feel that your billion dollars is going to the best use. Whereas CoffeeScript you get that billion dollars, and get a best practice guide to investment without the overwhelm, so you can focus on drinking cocktails in the bahamas.

This is also a major thing with newcomers to DocPad. Unlike libraries like History.js, where you can provide a neat little API that people can use and they never have to delve into the source. Things like DocPad require understanding of the architecture if you're wanting to accomplish amazing things. If I were to write it in JavaScript I highly doubt it would even be used today by anyone, as the source would be too difficult to read, didn't use blah's favourite library, or didn't do something this way or the other, or coding style was too different and intolerable to comprehend versus one of many coding styles out there. So when the option of using CoffeeScript and getting rid of all that overhead appeared itself, it was an easy choice.

So I'm not sure how making inheritance or syntax easier in JavaScript, helps solve the many issues that the JavaScript ecosystem is having as a whole.

# Generate the Static Website
generate: (next) ->
# Prepare
docpad = @
# Check
return if @generating
@generating = true
# Continue
path.exists @options.srcPath, (exists) =>
# Check
throw new Error 'Cannot generate website as the src dir was not found' unless exists
# Continue
util.rmdir @options.outPath, ->
docpad.generateClean ->
docpad.generateParse ->
docpad.generateRelations ->
docpad.generateRender ->
docpad.generateWrite ->
console.log 'Website Generated'
docpad.generating = false
next?()
// Generate the Static Website
generate: function(next){
// Prepare
var docpad = this;
// Check
if ( this.generating ) return;
this.generating = true;
// Continue
path.exists(this.options.srcPath,function(exists){
// Check
if ( !exists ) throw new Error('Cannot generate website as the src dir was not found');
// Continue
util.rmdir(docpad.options.outPath,function(){
docpad.generateClean(function(){
docpad.generateParse(function(){
docpad.generateRelations(function(){
docpad.generateRender(function(){
docpad.generateWrite(function(){
console.log('Website Generated');
docpad.generating = false;
if ( typeof next === 'function' ) next();
});
});
});
});
});
});
});
},
@pokonski
Copy link

In this example, they are both scary :D

@balupton
Copy link
Author

;-) how I un-scary them? @pokonski

@pokonski
Copy link

Just kidding, it's just the stack level of those functions is pretty deep ;)

@padolsey
Copy link

LOL your problem is not JavaScript; it's insisting on doing async operations without promises, thunks or continuables.......

@balupton
Copy link
Author

@padolsey ... not sure, but it seems you may have skipped over the README.md file... it elaborates a lot more on why promises, thunks etc - while they are an initial and trivial issue - aren't the sole issue exposed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment