Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

the right time to use coffeescript

So basically I have a number of concerns holding me back from coffeescript:

  1. I'm not really an expert in js yet, even though I'm using it for around 3 years now I still feel like I'm missing something important about it. Since it's mostly a supportive technology for me I never find time to go in depths of js ( which, I admit, might be a wrong attitude ).

  2. My js knowledge will get even worse if I'll start Using coffeescript

  3. I'm not sure if I can actually trust coffescript, meaning the js code it compiles to

  4. At times I don't understand the js code coffeescript compiles to and even worse - why it compiles like that.

I'd like to know your thoughts on the above points. The crucial one is: How using coffeescript affected your knowledge of js? And how important you think it is to fully understand js before switching to coffeescript?

like image 765
Vlad Khomich Avatar asked Apr 24 '12 10:04

Vlad Khomich


4 Answers

You should understand what problems Coffeescript is supposed to solve. And for that, you should have a basic knowledge of javascripts' "bad parts". I suggest reading Douglas Crawford about that (there's a book, but also a lot of resorces on internet. Just google "javascript bad parts"). Basically, the idea is that "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." (taken from coffeescript's site).

There's a tool to assist programmers to avoid javascript pitfalls called jslint. This tool analizes your code and gives warnings about common mistakes, such as global variables, semicolon insertions, namespace pollution, etc...

Coffeescript translates to javascript. But the javascript it generates is a cannocical subset, highly compliant with jslint. What's more, it generates javascript code valid on all browsers. So it is not just a nice syntactic sugar layer, it really helps generate solid code.

like image 99
Luis Gonzalez Avatar answered Oct 01 '22 17:10

Luis Gonzalez


I'd like to address your concerns.

1) If you've been using JS for three years, you probably have a pretty solid understanding of JS. If you haven't gained a solid understanding yet, it may be time to supplement your knowledge with one of the good JS books.

2) Coffee-script probably wont make your knowledge of JS worse. The way you design Coffee-script applications is the same way that you would design a JS application (for the most part), so the design skills you gain will transfer over. Program design, in my opinion, is the most important aspect of programming.

3) Why don't you trust the JS? Why do you trust any of the other compilers/interpreters/other tools you use? I doubt Coffee-script is bug free, but many people use it for many purposes. This means that a large set of behavior has been tested, often in production, so your use case has probably already been tried and tested.

4) Of course the JS generated by Coffee-script will look foreign to you, since the rules for generating it don't have human readability as a priority. Reading it, however, will increase your knowledge of JS as you see how peculiarly written programs run. This brings us back to point 1.

like image 30
Max Avatar answered Oct 01 '22 18:10

Max


I think that the crucial thing to remember here is that Coffeescript IS javascript. Every Coffeescript statement or magic operator has a distinct concrete representation in Javascript. For instance (x) -> x * x in Coffeescript will translate directly to function (x) { return x * x; }.

You can't really write Coffeescript without being aware of the Javascript it will generate. For one thing, the generated Javascript is what you will have to debug. If anything, I believe that writing Coffeescript could possibly improve your understanding of Javascript, because it forces you to make decisions that are unique to Javascript. For instance, when in Coffeescript, you decide to use => instead of -> in reality you are making a decision about whether or not you want to bind this - a very real Javascript problem.

When (or if) to start using Coffeescript? I think the answer to this is more or less up to you. Try it out, and if you feel that it is easier to get your tasks done using Coffeescript, then stick to it. If you find it difficult to write the code in a different language from the one that runs (and thus the one you have to debug), then go back to Javascript.

like image 1
AHM Avatar answered Oct 01 '22 18:10

AHM


So here's what I think about the topic:

  1. JS is not a supportive technology (support for what?). It is a language mostly used on front-end and there is a new trend of using it on back-end. Since browser do not support CoffeeScript natively than unless you use it as a back-end then I don't think there is a point in using CoffeeScript. Although learning new language is always a good idea.

  2. Not at all. Actually using CoffeeScript is like using different language. Learning one cannot make you dumber in the other. Unless you stop learning the other one.

  3. There is no evidence that CoffeeScript compiles to buggy or slow code. Actually I am using CoffeeScript for some time and I didn't observe any performance hit.

  4. Actually you don't need to understand why it compiles like this. If you are using CoffeeScript on back-end then you don't even have to look at the code it compiles into (you only need the source code). As for using it to make browser scripts then yes - it may be a bit difficult to work with it (debug). That's why I always advice to write normal JavaScript for browsers and use CoffeeScript on back-end.

Now as for the last question: I don't think that CoffeeScript affected my JS knowledge at all. I treat them as separate languages. Also you don't need to know JS in order to switch to CoffeeScript (although you should) unless you want to use CoffeeScript on front-end.

Also mastering JavaScript is always a good idea, no matter what. :)

like image 1
freakish Avatar answered Oct 01 '22 17:10

freakish