Sunteți pe pagina 1din 3

Why Im Using ClojureScript | Digital Digressions by Stuart Sierra

http://stuartsierra.com/2012/06/16/why-im-using-clojurescript

Digital Digressions by Stuart Sierra


From programming to everything else

Why Im Using ClojureScript


Posted on June 16, 2012 by Stuart

Elise Huard wrote about why shes not using ClojureScript. To quote her essential point, The browser doesnt speak clojure, it speaks javascript. This is true. But the CPU doesnt speak Clojure either, or JavaScript. This argument against ClojureScript is similar to arguments made against any high-level language which compiles down to a lower-level representation. Once upon a time, I feel sure, the same argument was made against FORTRAN. A new high-level language has to overcome a period of skepticism from those who are already comfortable programming in the lower-level representation. A young compiler struggles to produce code as efficient as that hand-optimized by an expert. But compilers tend to get better over time, and some smart folks are working hard on making ClojureScript fast. ClojureScript applications can get the benefit of improvements in the compiler without changing their source code, just as Clojure applications benefit from years of JVM optimizations. To address Huards other points in order: 1. Compiled ClojureScript code is hard to read, therefore hard to debug. This has not been an issue for me. In development mode (no optimizations, with pretty-printing) ClojureScript compiles to JavaScript which is, in my opinion, fairly readable. Admittedly, I know Clojure much better than I know JavaScript. The greater challenge for me has been working with the highly-dynamic nature of JavaScript execution in the browser. For example, a function called with the wrong number of arguments will not trigger an immediate error. Perhaps ClojureScript can evolve to catch more of these errors at compile time. 2. ClojureScript forces the inclusion of the Google Closure Library. This is mitigated by the Google Closure Compilers dead-code elimination and aggressive space optimizations. You only pay, in download size, for what you use. For example, jQuery 1.7.2 is 33K, minified and gzipped. Caching doesnt always save you. Hello World in ClojureScript, optimized and gzipped, is 18K. 3. Hand-tuning performance is harder in a higher-level language. This is true, as per my comments above about high-level languages. Again, this has not been an issue for me, but you can always drop down to JavaScript for specialized optimizations. 4. Cross-browser compatibility is hard. This is, as Huard admits, unavoidable in any language. The Google Closure Libraries help with some of the basics, and ClojureScript libraries such as Domina are evolving to deal with other browser-compatibility issues. You also have the entire world of JavaScript libraries to paper over browser incompatibilities. *** Overall, I think I would agree with Elise Huard when it comes to browser programming in the small. If you just want to add some dynamic behavior to an HTML form, then ClojureScript has little advantage over straight

1 de 3

15/12/2012 14:12

Why Im Using ClojureScript | Digital Digressions by Stuart Sierra

http://stuartsierra.com/2012/06/16/why-im-using-clojurescript

JavaScript, jQuery, and whatever other libraries you favor. What ClojureScript allows you to do is tackle browser-based programming in the large. Ive found it quite rewarding to develop entire applications in ClojureScript, something I would have been reluctant to attempt in JavaScript. Its partially a matter of taste and familiarity. Clojure programmers such as myself will likely prefer ClojureScript over JavaScript. Experienced JavaScript programmers will have less to gain and more work to do, learning a new language by adopting ClojureScript. JavaScript is indeed good enough for a lot of applications, which means ClojureScript has to work even harder to prove its worth. I still believe that ClojureScript has an edge over JavaScript in the long run, but that edge will be less immediately obvious than the advantage that, say, Clojure on the JVM has over Java.
This entry was posted in Programming and tagged Clojure, ClojureScript, JavaScript. Bookmark the permalink.

4 Responses to Why Im Using ClojureScript


Axel Rauschmayer says:
June 17, 2012 at 4:55 am

Great to read a thoughtful analysis of JavaScript versus language X. What do you miss most when programming in the large in JavaScript? Note that a module system such as [1] is very helpful here. [1] http://requirejs.org/

Stuart says:
June 17, 2012 at 9:51 am

Axel- I dont miss anything in JavaScript, I just avoid it. :)

Joseph Wilk says:


June 19, 2012 at 11:23 am

jQuery 1.7.2 is 33K, minified and gzipped. Caching doesnt always save you. Hello World in ClojureScript, optimized and gzipped, is 18K Im confused by this point. Are you comparing the file size of Hello world and jQuery? If you are that does not seem like a very fair comparison, jQuery has a little more functionality than hello world. Comparing a ClojureScript file mirroring the functionality of jQuery and comparing would be a fairer comparison. Or saying: Hello World in Clojurescript is 18K. Hello World in javascript is 22 bytes.

Stuart says:
June 19, 2012 at 12:21 pm

Joseph- The point of the Hello World example is not to compare ClojureScript to JavaScript, but to compare ClojureScript with the Google Closure Libraries to JavaScript with a common library such as jQuery. I was addressing Elise Huards complaint (later retracted) that using ClojureScript forces you to include all of the Google Closure Libraries in your application. The smallest possible download size for a JavaScript program using jQuery is 32K. The Google Closure Libraries,

2 de 3

15/12/2012 14:12

Why Im Using ClojureScript | Digital Digressions by Stuart Sierra

http://stuartsierra.com/2012/06/16/why-im-using-clojurescript

uncompressed, are many megabytes in size, but anything you dont use will be eliminated from the application download. The Hello World program in ClojureScript contains a lot of code besides just Hello World, mostly because the ClojureScript compiler is emitting that code in such a way that the Google Closure Compiler has trouble optimizing it. So right now, the smallest ClojureScript program compiles to about 18K. I am hopeful that the ClojureScript compiler will improve to take better advantage of the Google Closure Compilers optimizations, thereby shrinking the size of the average compiled ClojureScript program.

Digital Digressions by Stuart Sierra


Proudly powered by WordPress.

3 de 3

15/12/2012 14:12

S-ar putea să vă placă și