CoffeeScript – The Awesome way to write JavaScript


Do you struggle with Braces and semicolon in writing code of JavaScript. There is a solution of this problem and that is – CoffeeScript.  In this post I’ll explore  CoffeeScript – a minimalistic language that compiles to JavaScript.

CoffeeScript – The Awesome way to write JavaScript

CoffeeScript

CoffeeScript

CoffeeScript is a little language that compiles into JavaScript. Underneath all those awkward braces and semicolons, JavaScript has always had a gorgeous object model at its heart. CoffeeScript is an attempt to expose the good parts of JavaScript in a simple way.

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.

The language adds syntactic sugar inspired by Ruby,Python and Haskell. NO braces and semicolons. Since March 16, 2011, CoffeeScript has been on GitHub‘s list of most-watched projects, and as of 29 August 2012 is the eleventh most popular language on GitHub. CoffeeScript compiles predictably to JavaScript and programs can be written with less code, typically 1/3 fewer lines, with no effect on runtime performance

Syntax of CoffeeScript – Easy to use – Easy to Read

Here I am showing some example.

1. Assignment

str = "CoffeeScript"

2. Function

square = (x) -> x * x

3. Condition

string = "Passed" if  condition

4. Multiply numbers with 2

[1..10].map (i) -> i*2 

5. Object

math =
  root:   Math.sqrt
  square: square
  cube:   (x) -> x * square x

6. Loop

eat food for food in ['toast', 'cheese', 'wine']

CoffeScript vs JavaScript Code

CoffeScript vs JavaScript

Installation

The CoffeeScript compiler is itself written in CoffeeScript, using the Jison parser generator. The command-line version of coffee is available as a Node.js utility. The core compiler however, does not depend on Node, and can be run in any JavaScript environment, or in the browser.

To install, first make sure you have a working copy of the latest stable version of Node.js, and npm(the Node Package Manager). You can then install CoffeeScript with npm:

npm install -g coffee-script

(Leave off the -g if you don’t wish to install globally.)

If you’d prefer to install the latest master version of CoffeeScript, you can clone the CoffeeScriptsource repository from GitHub, or download the source directly. To install the lastest master CoffeeScript compiler with npm:

npm install -g http://github.com/jashkenas/coffee-script/tarball/master

Or, if you want to install to /usr/local, and don’t want to use npm to manage it, open the coffee-script directory and run:

sudo bin/cake install

For More Information and learning
1.http://coffeescript.org
2.http://jashkenas.github.com/coffee-script/documentation/docs/grammar.html

What makes C++ so good?


Answer by Harsh Kothari:

1: Stronger Type Checking – the use of classes, inheritance & automatic type conversions mostly eliminates the need for the abominable void* of C.

2: Type safe linkage – you can’t accidentally call a routine from another
module with the wrong type and/or number of arguments – even if your header files get out of date.

3: A complex data type is provided. It includes all the standard arithmetic operations, implemented as operators, not function calls.

4: User-defined operators and function overloading are supported. When you design a data type you can specify which operators & functions are provided.

5: You can use class libraries to provide robust new data types which can be made exceptionally easy to use.

For example, the Rogue Wave ‘math.h++’ class library implements general multi-dimensional arrays which can be manipulated with high-level operations
and an intuitive syntax:

DComplexArray a(10,10); // Construct a 10×10 complex array
cin >> a; // read it in from standard input
DComplexArray b = inverse(a); // Calculate the inverse
cout << b; // write out the inverse
cout << variance(b.diagonal()); // write out the variance of the diagonal
elements of b

6: You can define automatic type conversions to convert between data types.
For example, the Rogue Wave library provides a conversion from a double array
to a complex array.

DoubleVec a(10, 0.0, 1.0); // Construct a double vector in initialised to
{0,1,2,3,4…
DComplexVec z = a; // Construct a complex vector initialised to
{(0,0),(1,0),(2,0),…
cout << a; // write them out
cout << z;
cout << cos(z)*exp(DComplex(0,1)*a);

7: Provides inline functions which combine the efficiency of using macros
with the safety of using functions – simply prepend the word ‘inline’ in
front of the function – if the compiler can inline it, it will.

inline Double
SumOfPositiveElements
(const DoubleVec&amp; v)
{
Double theSum = 0;
for (int i = 0; i < v.length(); i++) {
if (v[i] > 0) {
theSum += v[i];
}
}
return theSum;
}

8: C++ Compiles most ANSI C code directly and can call compiled C code
directly, so you don’t even have to learn anything new at all!

9: You don’t have to put all of your declarations at the top of each block
in C++.
This means
that you can organise your code into logically related ‘paragraphs’ complete
with their necessary declarations. This makes code much more maintainable –
you can easily move sections of code around, taking the necessary
declarations along at the same time. If you use the const modifier you can
also ensure that variables whose value should not change after it is first
calculated do not do so.

Double x,y; // Declare two variables
cin >> x >> y; // read in their values
const Double sqrtX = sqrt(x); // Calculate the square roots
const Double sqrtY = sqrt(y);
cout << sqrt(sqrtX+sqrtY);
sqrtX = 42; // Will give an error…

10: Classes provide extensible types, promoting code reuse. This can
result in major savings in the amount of code written. I saw a recent
article which stated that the new Taligent operating system, which is written
in C++, consists of 250,000 lines of code, whereas WindowsNT, written in C,
was said to consist of 4,000,000 lines of code.

View Answer on Quora