The wind of change
Say hi to CoffeeScript
Objectivity ECMA 6 ready
class example extended by superClass#property@property constructor:->#call function@method()#function method:(parameter)->alert(parameter)
CoffeeScript let you optionally omit most of the braces in your code,making the code less enigmatic and almost brace-less.From my experience,applying all the brace-removal features makes more mess in code than not using them at all,but there is always good balance-point that works smoothly.
With CoffeeScript you can forget about the concatenation of the strings that can be cleanly placed into the strings.
Managing the scope
Due to fact that
Type coercion in comparison operators
Inexperienced programmes often forget about the difference between
===operators.The effect is quirky code,that unexpectedly stop working,because the interpreter implicitly tries to convert the values before comparing of values.Example below shows it precisely:
#true true==1#true'2'==2#false true===1#false'2'===2
CoffeeScript prevent coercion errors happen by forcing usage of
===in comparison operations.It lowers the code efficiency a bit,because
==is faster than
===in execution,but by forcing that,prevents substantial errors in the code.
To make writing
thisfaster,really smart alias
@was added.It stands out of the code and it’s very easily cached by eye.For me it’s meaningless syntactic sugar,but yet very helpful.
The most gorgeous thing I love in CoffeeScript is definitely fat-arrow operator that allows to decide on handling scope of
thishandler when changing scope in code.
class clickClass $(elem).on('click',@clicked)#This===clickClass clicked:(e)->alert(@)#This===clicked element clicked:(e)=>alert(@)
Looping over arrays and hashes is very crucial in every language.The more we can enhance the experience,the better language is.CoffeeScript’s juice is made form that particular feature,allowing programmer to nicely and very efficiently iterate over the value+index of an array,or the key+value of a hash,hiding beneath nasty code.
results=(book.name for book in bookstore when book.rating>3)[0...10]
Learn more about CoffeeScript comprehension frompost on OpenMind.
Criticism of CofffeScript
CoffeeScript is like putting lipstick on a pig,but it is damn good lipstick.
If you feel that you want to go deeper into CoffeeScript see the links below:
- CoffeeScript Homepage–greatest source of perfectly documented code filled with rich examples
- CoffeeScript GitHub repository–download source from here and discuss issues about the code
- How to make good Coffee–best CoffeeScript coding practices
- The Little Book on CoffeeScript–sensational book explaining basics of CoffeScript syntax
- CoffeeScript Cookbook–set of useful examples and ready to use patterns in CS