Programming, Scripts

CoffeeScript : A boon for Visual Studio developers

Scripting Libraries were always my favourite. For a long time I had been trying to seek various scripting libraries to unleash their power of existence and what they can provide to our programming world. One of the library I really like is CoffeeScript. As the name suggest CoffeeScript means a cup of coffee that unleash the taste of sensuality in scripting world. We all like coffee isn’t it.

Well to define CoffeeScript, it is a language that compiles into a JavaScript. How does CoffeeScript makes it different in our scripting world. Well it expose the good parts of JavaScript in a simple manner and that’s how it makes a difference. It is not a replacement of JavaScript but it is a JavaScript generator. Our development environment will utilize CoffeeScript to generate browser-ready JavaScript which means you can also use other JavaScript libraries with CoffeeScript. You can learn a lot about CoffeeScript from http://coffeescript.org/.

You can install CoffeeScript using Node Package Manager and write CoffeeScript in various editor which supports this library but for Visual Studio developers “Welcome to CoffeeScript World”. Now CoffeeScript can be added to Visual Studio 2010, 2012 and 2013 as an extension for editing by downloading the Visual Studio tool Mindscape Web Workbench or installing the Web Essentials which will provide a template for CoffeeScript in your Visual Studio version.

Once you install CoffeeScript in your Visual Studio, you can add a CoffeeScript file with *.coffee extension into the script folder and start editing in any HTML/MVC/JavaScript application.

image

image

While you open the *.coffee file you will able to see that the editor is divided into two sections. Left section is the CoffeeScript editor and the right section is the window where dynamic JavaScript is getting created. Whenever you write your script in CoffeeScript editor and save it, the script gets compiled dynamically and JavaScript gets generated which you can see in the right section.

image

Having said that let’s try to seek what can be done with CoffeeScript. Let’s try to identify some of the similarities of CoffeeScript with C# and also some points to identify the basic difference between CoffeeScript vs jQuery  or CoffeeScript vs JavaScript.

What kind of similarities we can see between CoffeeScript and C#.

  • C# use Lambda expressions for declaring an anonymous delegate. CoffeeScript also use the same syntax by implementing jQuery grep() function to decide whether to return an element. For example:

image

  • I can also write a function in CoffeeScript without using any return keyword, any parenthesis for calling function or any curly braces. Only lambda expression is enough to generate the JavaScript. For example:

image

  • Just like C#, CoffeeScript supports default values for function arguments. For example:

image

  • As in C# we are using comparison operators, in CoffeeScript comparison operators are pretty simple to use. They are used just like layman terms. CoffeeScript  will always use === and !== operators for generating JavaScript code using == and != operators. In CoffeeScript is is an alias of JavaScript === and isnt is an alias of JavaScript !==.  Also it use built-in alias like yes/on for true and no/off for false which are pretty easy to understand. For example:

image

  • Just like C#, it use existential operator “?” which acts similar to C# operator “??”. For example:

image

  • C# use Tuple which is a data structure that has fixed number of elements and it is useful when we want to return more than a single value. The same concept applies in CoffeeScript which is using destructuring assignment syntax. For example:

image

  • C# use switch/case statement. CoffeeScript use the same approach as switch/when/case construct which automatically inserts break in each case block. For example:

image

image

  • Since JavaScript is a prototype-based language, it doesn’t have native class and inheritance concept although there are lot of JavaScript libraries that help us to create classes. Well just like C#, CoffeeScript offers first-class support for defining classes and also handle the inheritance feature of Object Oriented Programming. For example:

image

image

Well I have covered few basics of the similarities of CoffeeScript and C# and how CoffeeScript is different from JavaScript. You need to do a lot more exercise on this language and trust me its worth working on this scripting language.

Before I conclude I would also like to say that since CoffeeScript generates JavaScript so its maximum possible speed equals to the speed of JavaScript. So in terms of performance CoffeeScript cannot be optimized at low level like JavaScript. Still we don’t have any questions on CoffeeScript because it makes life easier.

And last point that I would like to highlight is CoffeeScript is packed with features, so combining CoffeeScript with jQuery can really make the world clean and beautiful.

Hope you will learn more on CoffeeScript and implement it in your projects.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s