scratch/output/Scratch/en/blog/2011-01-03-Why-I-sadly-won-t-use-coffeescript/index.html
Yann Esposito (Yogsototh) 1a6066ea2b regeneration
2012-02-20 15:41:09 +01:00

312 lines
No EOL
15 KiB
HTML

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="fr" xml:lang="fr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="keywords" content="Coffeescript">
<link rel="shortcut icon" type="image/x-icon" href="/Scratch/img/favicon.ico" />
<link rel="stylesheet" type="text/css" href="/Scratch/assets/css/main.css" />
<link rel="stylesheet" type="text/css" href="/Scratch/css/twilight.css" />
<link rel="stylesheet" type="text/css" href="/Scratch/css/idc.css" />
<link rel="alternate" type="application/rss+xml" title="RSS" href="http://feeds.feedburner.com/yannespositocomen"/>
<link rel="alternate" lang="fr" xml:lang="fr" title="Pourquoi je n'utiliserai pas CoffeeScript (malheureusement)" type="text/html" hreflang="fr" href="/Scratch/fr/blog/2011-01-03-Why-I-sadly-won-t-use-coffeescript/" />
<link rel="alternate" lang="en" xml:lang="en" title="Why I won't use CoffeeScript (sadly)" type="text/html" hreflang="en" href="/Scratch/en/blog/2011-01-03-Why-I-sadly-won-t-use-coffeescript/" />
<script type="text/javascript" src="/Scratch/js/jquery-1.3.1.min.js"></script>
<script type="text/javascript" src="/Scratch/js/jquery.cookie.js"></script>
<script type="text/javascript" src="/Scratch/js/index.js"></script>
<!--[if lt IE 9]>
<script src="http://ie7-js.googlecode.com/svn/version/2.1(beta4)/IE9.js"></script>
<![endif]-->
<title>Why I won't use CoffeeScript (sadly)</title>
</head>
<body lang="en" class="article">
<script type="text/javascript">// <![CDATA[
document.write('<div id="blackpage"><img src="/Scratch/img/loading.gif" alt="loading..."/></div>');
// ]]>
</script>
<div id="content">
<div id="choix">
<div class="return"><a href="#entete">&darr; Menu &darr;</a></div>
<div id="choixlang">
<a href="/Scratch/fr/blog/2011-01-03-Why-I-sadly-won-t-use-coffeescript/" onclick="setLanguage('fr')">en Français</a>
</div>
<div class="flush"></div>
</div>
<div id="titre">
<h1>
Why I won't use CoffeeScript (sadly)
</h1>
</div>
<div class="flush"></div>
<div class="flush"></div>
<div id="afterheader">
<div class="corps">
<p><img alt="Title image" src="/Scratch/img/blog/2011-01-03-Why-I-sadly-won-t-use-coffeescript/main.png" /></p>
<div class="encadre">
<p><em>Update</em>: I might change my mind now. Why?
I just discovered a <a href="https://github.com/rstacruz/js2coffee">js2coffee converter</a>. Furthermore Denis Knauf told me about a <code>CoffeeScript.eval</code> function.
And as Denis said: &ldquo;it is time to use Coffeescript as a javascript with Ruby-like syntax not a Ruby-like programming language&rdquo;.</p>
</div>
<div class="intro">
<p><span class="sc"><abbr title="Too long; didn't read">tl;dr</abbr>: </span> I would have loved to program client side using a Ruby-like syntax. But in the end, CoffeScript raised more disavantages than advantages.</p>
</div>
<p>Recently I read <a href="http://news.ycombinator.com/item?id=2053956">this entry</a> on HackerNews.
The most upvoted comment praised (within other) <a href="http://coffeescript.org">CoffeeScript</a>.
Recently I used <em>a lot</em> of javascript. After trying
<a href="http://sproutcore.com">Sproutcore</a>,
<a href="http://cappuccino.org">Cappuccino</a>, looking at
<a href="documentcloud.github.com/backbone/">backbone.js</a> <em class="pala">&amp;</em>
<a href="javascriptmvc.com">javascriptMVC</a>,
I&rsquo;ve finally decided to make my own minimal javascript MVC framework.<sup id="fnref:1"><a href="#fn:1" rel="footnote">1</a></sup></p>
<p>I had to fight the horrible syntax of javascript. It was like experiencing a back-in-time travel: </p>
<ul>
<li>Verbose Java-like syntax, </li>
<li>Strange and insanely Verbose Object Oriented Programming,</li>
<li>No easy way to refer to current instance of a class (<code>this</code> doesn&rsquo;t work really well),</li>
<li>etc&hellip; </li>
</ul>
<p>It was so annoying at a point, I had thinked about creating my <em>own</em> CoffeeScript.</p>
<p>I&rsquo;d finished a first draft of my MVC javascript framework.
Just after I learned about the existence of CoffeeScript, I immediately created a new git branch to try it.</p>
<p>Here is my experience:</p>
<ol>
<li>I had to install <code>node.js</code> and use <code>npm</code> just to use CoffeeScript. It wasn&rsquo;t a big deal but it wasn&rsquo;t as straightfoward as I expected either.</li>
<li>Existing javascript file are not coffee compatible.
I <em>had</em> to translate them by hand.
There were no script to help me in this process.
Thanks to <a href="http://vim.org">vim</a>, it wasn&rsquo;t too hard to translate 90% of the javascript using some regexp.
The <code>--watch</code> option of coffee was also really helpful to help in the translation.
But I had to write my own shell script in order to follow an entire directory tree.</li>
<li>An unexpected event. I made some meta-programming in javascript using <code>eval</code>. But in order to work, the string in the eval must be written in pure javascript not in coffee. It was like writing in two different languages. Really not so good.</li>
</ol>
<h2 id="conclusion">Conclusion</h2>
<p>Advantages:</p>
<ul>
<li>Readability: clearly it resolved most of javascript syntax problems</li>
<li>Verbosity: I gained 14% line, 22% words, 14% characters</li>
</ul>
<p>Disadvantages:</p>
<ul>
<li>Added another compilation step to see how my code behave on the website.</li>
<li>I had to launch some script to generate on change every of my javascript file</li>
<li>I have to learn another Ruby-like language,</li>
<li>meta-programming become a poor experience,</li>
<li>I must convince people working with me to:
<ul>
<li>install <code>node.js</code>, <code>npm</code> and CoffeeScript,</li>
<li>remember to launch a script at each code session,</li>
<li>learn and use another ruby-like language</li>
</ul>
</li>
</ul>
<p>The last two point were definitively really problematic for me.</p>
<p>But even if I&rsquo;ll have to work alone, I certainly won&rsquo;t use CoffeeScript either.
CoffeeScript is a third party and any of their update can break my code.
I experienced this kind of situation many times, and it is very annoying.
Far more than coding with a bad syntax.</p>
<h2 id="digression">Digression</h2>
<p>I am sad.
I wanted so much to program on Web Client with a Ruby-like syntax.
But in the end I think it is not for me.
I have to use the <em>horrible</em> javascript syntax for now.
At least I would have preferred a complete <code>ruby2js</code> script for example<sup id="fnref:2"><a href="#fn:2" rel="footnote">2</a></sup>.
But I believe it would be a really hard task just to simulate the access of current class for example.</p>
<p>Typically <code>@x</code> translate into <code>this.x</code>. But the following code will not do what I should expect. Call the foo function of the current class.</p>
<pre class="twilight">
<span class="Keyword">-</span><span class="Keyword">&gt;</span>
<span class="Keyword">class</span> <span class="Entity">MyClass</span>
foo: <span class="Keyword">-</span><span class="Keyword">&gt;</span>
<span class="Entity">alert</span>(<span class="String"><span class="String">'</span>ok<span class="String">'</span></span>)
bar: <span class="Keyword">-</span><span class="Keyword">&gt;</span>
$(<span class="String"><span class="String">'</span>#content<span class="String">'</span></span>).<span class="Entity">load</span>( <span class="String"><span class="String">'</span>/content.html<span class="String">'</span></span>, ( <span class="Keyword">-</span><span class="Keyword">&gt;</span> <span class="Variable"><span class="Variable">@</span>foo</span>(x) ) )
<span class="Comment"> <span class="Comment">#</span> That won't call MyClass.foo</span>
</pre>
<p>The only way to handle this is to make the following code:</p>
<pre class="twilight">
<span class="Keyword">-</span><span class="Keyword">&gt;</span>
<span class="Keyword">class</span> <span class="Entity">MyClass</span>
foo: <span class="Keyword">-</span><span class="Keyword">&gt;</span>
<span class="Entity">alert</span>(<span class="String"><span class="String">'</span>ok<span class="String">'</span></span>)
bar: <span class="Keyword">-</span><span class="Keyword">&gt;</span>
<span class="Variable">self</span><span class="Keyword">=</span>this
$(<span class="String"><span class="String">'</span>#content<span class="String">'</span></span>).<span class="Entity">load</span>( <span class="String"><span class="String">'</span>/content.html<span class="String">'</span></span>, ( <span class="Keyword">-</span><span class="Keyword">&gt;</span> <span class="Variable">self</span>.<span class="Entity">foo</span>(x) ) )
</pre>
<p>Knowing this, <code>@</code> notation lose most of its interrest for me.</p>
<hr/><div class="footnotes">
<ol>
<li id="fn:1">
<p>I know it may not be the best nor productive decision, but I&rsquo;d like to start from scratch and understand how things works under the hood.<a href="#fnref:1" rel="reference">&#8617;</a></p>
</li>
<li id="fn:2">
<p>I know there is <code>rb2js</code>, but it doesn&rsquo;t handle the problem I talk about.<a href="#fnref:2" rel="reference">&#8617;</a></p>
</li>
</ol>
</div>
</div>
<div id="choixrss">
<a id="rss" href="http://feeds.feedburner.com/yannespositocomen">
Subscribe
</a>
</div>
<script type="text/javascript">
$(document).ready(function(){
$('#comment').hide();
$('#clickcomment').click(showComments);
});
function showComments() {
$('#comment').show();
$('#clickcomment').fadeOut();
}
document.write('<div id="clickcomment">Comments</div>');
</script>
<div class="flush"></div>
<div class="corps" id="comment">
<h2 class="first">comments</h2>
<noscript>
You must enable javascript to comment.
</noscript>
<script type="text/javascript">
var idcomments_acct = 'a307f0044511ff1b5cfca573fc0a52e7';
var idcomments_post_id = '/Scratch/en/blog/2011-01-03-Why-I-sadly-won-t-use-coffeescript/';
var idcomments_post_url = 'http://yannesposito.com/Scratch/en/blog/2011-01-03-Why-I-sadly-won-t-use-coffeescript/';
</script>
<span id="IDCommentsPostTitle" style="display:none"></span>
<script type='text/javascript' src='/Scratch/js/genericCommentWrapperV2.js'></script>
</div>
<div id="entete" class="corps_spaced">
<div id="liens">
<ul><li><a href="/Scratch/en/">Home</a></li>
<li><a href="/Scratch/en/blog/">Blog</a></li>
<li><a href="/Scratch/en/softwares/">Softwares</a></li>
<li><a href="/Scratch/en/about/">About</a></li></ul>
</div>
<div class="flush"></div>
<hr/>
<div id="next_before_articles">
<div id="previous_articles">
previous entries
<div class="previous_article">
<a href="/Scratch/en/blog/2011-01-03-Happy-New-Year/"><span class="nicer">«</span>&nbsp;Happy New Year</a>
</div>
<div class="previous_article">
<a href="/Scratch/en/blog/2010-10-26-LaTeX-like-macro-and-markdown/"><span class="nicer">«</span>&nbsp;LaTeX like macro for markdown</a>
</div>
<div class="previous_article">
<a href="/Scratch/en/blog/2010-10-14-Fun-with-wav/"><span class="nicer">«</span>&nbsp;Fun with wav</a>
</div>
</div>
<div id="next_articles">
next entries
<div class="next_article">
<a href="/Scratch/en/blog/2011-04-20-Now-hosted-on-github/">Now hosted on github&nbsp;<span class="nicer">»</span></a>
</div>
<div class="next_article">
<a href="/Scratch/en/blog/Password-Management/">40 character's passwords&nbsp;<span class="nicer">»</span></a>
</div>
<div class="next_article">
<a href="/Scratch/en/blog/Haskell-Mandelbrot/">ASCII Haskell Mandelbrot&nbsp;<span class="nicer">»</span></a>
</div>
</div>
<div class="flush"></div>
</div>
</div>
<div id="bottom">
<div>
<a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/">Copyright ©, Yann Esposito</a>
</div>
<div id="lastmod">
Created: 01/03/2011
Modified: 10/26/2011
</div>
<div>
Entirely done with
<a href="http://www.vim.org">Vim</a>
and
<a href="http://nanoc.stoneship.org">nanoc</a>
</div>
<div>
<a href="/Scratch/en/validation/">Validation</a>
<a href="http://validator.w3.org/check?uri=referer"> [xhtml] </a>
.
<a href="http://jigsaw.w3.org/css-validator/check/referer?profile=css3"> [css] </a>
.
<a href="http://validator.w3.org/feed/check.cgi?url=http%3A//yannesposito.com/Scratch/en/blog/feed/feed.xml">[rss]</a>
</div>
</div>
<div class="clear"></div>
</div>
</body>
</html>