7

I understand that compiling into Javascript is sometimes necessary and even in some cases results in a faster application.

However I'm wondering if compiling into Javascript, for building web applications, from a language like ruby, is considered bad practice. Specifically for the purpose of language preference and for the task of doing typical front-end functions.

closed as unclear what you're asking by gnat, Bart van Ingen Schenau, Michael Kohne, Kilian Foth, EL Yusubov Sep 3 '13 at 12:26

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    What other language would you use? Last time I checked, Javascript is the only language universally supported in browsers. – Robert Harvey Aug 26 '13 at 15:26
  • 1
    @RobertHarvey CoffeeScript and TypeScript are examples of languages that cross-compile to JS. I'm sure there are many others. – Dónal Aug 26 '13 at 15:34
  • 2
    @RobertHarvey I assumed the "language preference" choice implicit in the OP's question was "Compile language X into JavaScript" versus "Write JavaScript by hand" (rather than "Compile language X into JS" versus "Compile language X into other language Y"), but maybe I've misread (or maybe I'm misunderstanding you're saying). For example, is it bad to use fay to compile Haskell code into JS, just because you're don't want to use JS. – apsillers Aug 26 '13 at 15:53
  • 1
    Why do you think it would be a bad practice? – user16764 Aug 26 '13 at 23:00
  • 1
    @user16764 I'm not sure if I lean one way or the other. The main reason I ask is because the compiled JS code from Ruby, using opal, isn't necessarily as "clean" as I would expect. Check it out – opalrb.org/try – Ryan Rich Aug 27 '13 at 2:58
8

In his article Javascript is assembly language for the web Scott Hanselman argues quite convincingly that JavaScript is a low-level language and that it makes perfect sense where possible to leverage a cross-compiler if that will make you more productive.

The same question could be asked regarding Assembly language. And while many people railed against the concept of higher level languages, they eventually lost that argument, just like the people who complained about the performance lost when using languages like Java and C# that don't compile to machine code but instead are compiled against a virtual machine (JVM or .NET Runtime, respectively).

JavaScript is like IL for the web, the fact that we can use higher-level languages to program JS means that we can be more productive and leverage the compiler to get good enough (and in many cases better) performance makes doing so a very good practice.

When I say low level, I mean metaphorically. Just as C lets us ignore things like registers and the like. Using a cross-compiler let's us ignore things like navigating/manipulating the DOM. We're removing ourselves from the details of what Javascript does and focusing on the logic of the app. This has enabled things like a full x86 emulator running linux in the browser or Unreal engine running in the browser

To answer Robert Harvey's Question, if you look at projects like LLVM, you can practically choose whatever language you'd like (and thus use whatever tools you like) and have it generate valid Javascript.

Also, as follow up, Scott asked the creator of JavaScript and the creator of JSON their views on the topic Brendan Eich replied:

I said "JS is the x86 of the web" a couple of years ago [likely at JSConf], but I can't claim it's original. [Nick Thompson said it on Hacker News this year as well.]

The point is JS is about as low as we can go. But it also has higher-level facilities. Shaver's right, assembly without a great macro processor is not good for programmers or safety. JS is. So the analogy needs some qualification or it becomes silly.

The mix of high-level functional programming and memory safety, with low-level facilities such as typed arrays and the forthcoming ES.next extension of typed arrays, binary data, make for a more powerful programming language than assembly, and of course memory safety is the first differentiator.

  • 2
    What "ridiculous argument" about performance? And I didn't say the performance makes it a good practice, I said the increased productivity. If I'm proficient in Ruby, writing code in Ruby is more productive than writing code in JavaScript. As far as performance being good enough it's how Google+ is run if you had bothered to follow the link you would have seen that. – Michael Brown Aug 26 '13 at 15:39
  • 1
    There's nothing wrong with what you've said here. But implicit in the OP's question is the notion that there might be some other language choice, and your answer doesn't really address that possibility. – Robert Harvey Aug 26 '13 at 15:41
  • @MikeBrown Well, the article is about minifying your javascript. I don't see the article saying that running high level languages on top of just as high level language gives acceptable performance or (in many cases better?) – Esailija Aug 26 '13 at 15:43
  • 2
    @MikeBrown I get where you're coming from, but saying that JS is a low level language is a bit mind blowing. If you consider JS to be low level, how would you categorize C? BTW, JS is compiled to an IL by our browsers. It's just that every browser apparently has its own version of that IL. – Stefan Billiet Aug 26 '13 at 15:47
  • @StefanBilliet: Wouldn't it be nice if the IL were under an ECMA standard as well as Javascript? – Robert Harvey Aug 26 '13 at 16:34
2

I wouldn't quite call it a bad practice. However, the further you are removed from JS in the language you're working in (in your case Ruby apparently), the greater the chance that the compiler will create errors. E.g. TypeScript is a close superset of JS, but I imagine that Ruby doesn't share a whole lot with JS (I could be wrong).
I don't particularly like JS myself, but I found that when trying to circumvent using a language, sooner or later it catches up with you and you'll still be forced to actually use that language. Might as well embrace it from the get-go.

Not the answer you're looking for? Browse other questions tagged or ask your own question.