/
why-i-dont-use-coffeescript.html
89 lines (83 loc) · 7.07 KB
/
why-i-dont-use-coffeescript.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<!DOCTYPE html>
<html>
<head>
<title>OscarGodson.js | Why I Don't Use CoffeeScript</title>
<link href="/css/main.css" rel="stylesheet">
<meta name="description" content="CoffeeScript in of itself, and not as a transpiled language, is admittedly pretty. The problem for me is what it's supposedly 'fixing'.">
<script type="text/javascript">var _gaq = _gaq || [];_gaq.push(['_setAccount', 'UA-4491651-1']);_gaq.push(['_trackPageview']);(function() {var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);})();</script>
</head>
<body>
<div class="wrapper">
<div class="container">
<header>
<require src="/partials/header.html">
</header>
<div class="left-col">
<article id="content">
<h1>Why I Don't Use CoffeeScript</h1>
<p>CoffeeScript in of itself, and not as a transpiled language, is admittedly pretty. The problem for me is what it's supposedly "fixing".</p>
<h2>Syntax Sugar</h2>
<p>For a Rails and Python developer JavaScript is ugly, or so I hear. You have... (ready for it?), curly brackets and white space that doesn't mean anything. But, I like my whitespace. Most JavaScript engineers I know do. I like not being restricted on <em>how</em> I write my code aesthetically. Not to mention CoffeeScript is by far less human readable than JavaScript. Programming languages, in my opinion, need to be a good balance of both human and machine readable. Simple example:</p>
<pre>cube = (x) -> square(x) * x</pre>
<pre>cube = function(x) {
return square(x) * x;
};</pre>
<p>Even if you've never programmed before the 2nd example would make sense. The first is, well, illegible. I guess if you were writing 1000s of lines of code every single day from top to bottom typing less would be important, but we're humans, not computers, so legibility is far more important than saving a few keystrokes. When talking to mostly rails people I've heard numerous times that they dislike having to type out "function". Yes. This is a reason I hear as a plus for CoffeeScript. That is pure fucking laziness in my opinion and it really makes it hard for me to take their opinions seriously after.</p>
<p>But, more than all of this what bugs me is the fact <em>it's a completely transpiled language mostly for syntax sugar</em>. What an awful reason. So, on to the next reason...</p>
<p>Related: <a href="/posts/writing-eloquent-javascript-without-coffeescript.html">http://oscargodson.com/posts/writing-eloquent-javascript-without-coffeescript.html</a></p>
<h2>Wart Removal</h2>
<p>Another thing people <em>always</em> bring up is that it fixes "warts" in JavaScript. Things like <code>for...in</code> loops and they give examples like this:</p>
<pre class="javascript">for (var key in obj) {
if (obj.hasOwnProperty(key)) { // only look at direct properties
var value = obj[key];
// do stuff...
}
}</pre>
<p>First of all... no. Just use a <code>for(i=0;i<=10;i++)</code>, but if you need to use <code>for...in</code>s a lot, write a helper function that takes care of the <code>hasOwnProperty()</code> check. You need an entirely different language for it? Or, the other day someone on Google+ said that he likes CoffeeScript because he didn't like that JavaScript made <code>var</code>s global when you forgot the <code>var</code> keyword and that it was "just wrong". <strong>Protip:</strong> Don't do that. Just don't, not write <code>var</code>. Problem solved.</p>
<p>There are many other examples I've seen and they're all like this. That's just <em>trying</em> to prove a point. I find wart removal to be an awful selling point. In other words you're saying <q>Fuck the need to learn how to write JavaScript, we'll fix it for ya.</q> You really need a transpiled language to save you some time from learning the right way to write JavaScript in the first place? Learn JavaScript's pain points and just don't</em> do them.</p>
<p>It's like that C/C++ quote goes:</p>
<blockquote>
<p>"In C++ it's harder to shoot yourself in the foot, but when you do, you blow off your whole leg."
<cite>Bjarne Stroustrup.</cite></p>
</blockquote>
<h2>Classes</h2>
<p>I hear a lot about the love of how CoffeeScript handles classes and writing classes is a pain in JavaScript. You know why? <strong>Because, JavaScript is a fucking prototypal language.</strong></p>
<p>However, for whatever reason, people have a primal urge to use "classes" in JavaScript. Luckily there's numerous small libraries to try to shim classes into JavaScript. You don't need an entire transpiled language for it. For example here's what you can do with a pretty tiny script:</p>
<pre>var Ninja = Person.extend({
init: function(){
this._super( false );
},
dance: function(){
// Call the inherited version of dance()
return this._super();
},
swingSword: function(){
return true;
}
});</pre>
<p>From: <a href="http://ejohn.org/blog/simple-javascript-inheritance/">http://ejohn.org/blog/simple-javascript-inheritance/</a></p>
<p>Let me remind you though, JavaScript is <strong>not</strong> a classical language. Here's a famous quote from Crockford from <a href="http://www.crockford.com/javascript/inheritance.html">http://www.crockford.com/javascript/inheritance.html</a>:</p>
<blockquote>
<p>
I have been writing JavaScript for 8 years now, and I have never once found need to use an uber function. The super idea is fairly important in the classical pattern, but it appears to be unnecessary in the prototypal and functional patterns. I now see my early attempts to support the classical model in JavaScript as a mistake.
<cite>Douglas Crockford</cite>
</p>
</blockquote>
<h2>Conclusion</h2>
<p>After everything, CoffeeScript is for people who hate JavaScript. JavaScript warts should be avoided by education and understanding of the core language, not by simply ignoring them and cool features like how CoffeeScript handles Classes, or even loops, can easily be fixed with a much smaller codebase with no need to compile by using a simple homebrewed library or something like underscore.js, jQuery, etc.</p>
<p><em>P.S. People who write CoffeeScript specific codebases/libraries/etc and name them like "mylib.js"... i hate you. *cough*batman.js*cough*</em></p>
</article>
</div>
<div class="right-col">
<require src="/partials/sidebar.html">
</div>
<br class="clear">
<footer>
<require src="/partials/footer.html">
</footer>
</div>
</div>
<script src="/js/libs/libs.js"></script>
<script src="/js/app.js"></script>
</body>
</html>