News for hackers. Stuff that matters.
Re:The JavaScript Problem by jurassic on Saturday April 26, 2014 @10:29PM (#7653662)
The people who hate javascript the most are the ones who wish it was something else. I used to be one of those people. Once I decided to accept it as it is and read a few top books on the language to learn the "javascript way", my life got a lot better. It's really not that bad. I actually like it a lot, but there are still a lot of ignorant people who will look at you as if you're not l33t enough to know that foo language is better if you tell them you have a favorable view of js.

Of the things on his list, the only ones that really resonate with me at all are verbose function syntax and silent coercion. Typing function () {} gets old fast, but then I set up snippets and it ceased being a problem. That really just leaves the type coercion problem, which you adapt to pretty quickly if you're using the language in earnest.

What it boils down to is choosing to be a pragmatist who gets down to work with what's available instead of a naval gazer who swears everything will be awesome as soon as they have their preferred tool|language available. There are tons of compiles-to-js alternatives, but I don't see a huge difference in the productivity of people working in these not-JS alternatives. Where are the killer apps that were developed through a niche JS transpiler? If you want to build a web app in Haskell instead of the web's true lingua franca, all you're accomplishing is making it harder to hire and harder to collaborate.

Re:The JavaScript Problem by kenjackson on Saturday April 26, 2014 @10:40PM (#7653681)
If you want to build a web app in Haskell instead of the web's true lingua franca, all you're accomplishing is making it harder to hire and harder to collaborate.

We should be making the web multi-lingual. It's kind of absurd that we've built these great abstractions over HW and few people ever think about writing machine language. But for the web, an abstraction over several abstractions, we're largely stuck with a single language and seemingly little real effort to build a Multilanguage abstraction.

If you told me 20 years ago about what the web would become, I'd be amazed. If you went on to say that all programming would be in a single language -- I'd think you were crazy, yet here we are.

The web should have added 1st class support for plug-ins, rather than the direction we went to effectively abolish plugins from browsers.

Re:The JavaScript Problem by jmspring on Saturday April 26, 2014 @10:52PM (#7653711)
Browsers should not be multi-lingual -- more bloat.

The "compile X to javascript" movement has things half right. Unfortunately the "compile to" is Javascript. I wish the language the browsers implemented was something more modern, strong typing, etc. But I am not sure that will happen.

Re:The JavaScript Problem by AnthonyMouse on Sunday April 27, 2014 @01:09AM (#7654065)
The issue is that the "compile to" thing shouldn't be another programming language at all. It should be something like Java bytecode. Except not actually Java bytecode, because Java bytecode was made specifically for Java. You want something designed to be an intermediary representation between code in any arbitrary language and native instructions for any arbitrary architecture.

In theory you could use Javascript as that thing, but it wouldn't be very efficient. On the other hand, what you could do is create a compiler that compiles to both bytecode and Javascript, and then give the Javascript to older browsers that don't support bytecode yet.

Re:The JavaScript Problem by acqq on Sunday April 27, 2014 @02:37AM (#7654194)
If you think about it, bytecode you wish just doesn't exist, whereas JavaScript does and it works:

http://mozakai.blogspot.co.at/2013/05/the-elusive-universal-...

Re:The JavaScript Problem by _random_ on Sunday April 27, 2014 @07:48AM (#7654756)
The point is that we have something that sucks, not that we don't have anything at all.
Re:The JavaScript Problem by flohofwoe on Sunday April 27, 2014 @05:13AM (#7654461)
As an example, Portable Native Client is "proper byte code" (simplified LLVM IR), but client-side compilation takes multiple times longer then AOT-compiling the same code from asm.js (the NaCl team is working hard on improving this though). The download size is also almost the same (if compressed). Performance isn't that much better either, so for every practical purpose "real" byte code isn't automatically better then sending (compiled and compressed) "source code" over the wire.

I'm even getting worse startup times for a "cold start" for the same code compiled to native OSX then for starting the same demo as emscripten version, (presumably because the native version first needs to load a lot of DLLs).

Re:The JavaScript Problem by teacup50 on Sunday April 27, 2014 @02:45PM (#7656214)
On the other hand, because PNaCL is a sane IR, its composeability and implementability vastly exceeds that of asm.js, which requires not only a full JavaScript stack, but additionally, a complex JavaScript JIT system capable of executing it with additional asm.js-specific optimizations.

I can take PNaCL today and deploy it on alternative stacks, irrespective of the web stack. I can inter-operate cleanly with alternative libraries and environments, I can adopt efficient host platform ABIs, I can run it with a sandbox or without. I could even use it as a generic cross-platform sandboxed in-kernel driver layer.

Moreover, should NaCL/PNaCL be successful, there exists the possibility for optimization on the silicon-level via the introduction of new instruction sets that optimize for in-process sandboxing of untrusted code, much in the same way that trap-and-emulate VMs led to much higher performing silicon-implementations of the same ideas.

Issues of performance can be solved given a well-designed system that applies only as much complexity as is needed, at the level that it is needed. asm.js is a hack inserted at an inappropriate layer of the technology stack; NaCL/PNaCL is a coherent compositionally-sane design that opens the door to later optimizations and significant improvements of the underlying technologies on which it rests.

Re:The JavaScript Problem by baddox on Sunday April 27, 2014 @02:11AM (#7654162)
If there exists a tool that compiles your language of choice to JavaScript, and the stuff you write in your language of choice works on modern browsers and is stable and performant, why do you care that it's JavaScript being compiled to and not byte code?
Re:The JavaScript Problem by peterashford on Monday April 28, 2014 @08:54PM (#7664186)
Because JS will always be compromised by being JS shoe horned into a role it wasn't designed for and not byte code optimised for the purpose at hand.

Shoe horning technologies into roles they weren't intended for could describe the entire scope of why the Web sucks (e.g.: HTML mixing presentation with semantics).

Re:The JavaScript Problem by jmspring on Sunday April 27, 2014 @03:55AM (#7654329)
I agree something w/ "byte code" would be that thing. Said "byte code" would deal with types and such.

Now-a-days that "thing" is "javascript" and it has implementation nuances that depend on browser/runtime environment.

Re:The JavaScript Problem by kenjackson on Saturday April 26, 2014 @11:47PM (#7653866)
JavaScript and HTML are a lot more bloat than machine language code written to the wire.

Writing in a language appropriate to the task is almost always the right tradeoff.

Re:The JavaScript Problem by bzbarsky on Sunday April 27, 2014 @01:37AM (#7654097)
> JavaScript and HTML are a lot more bloat than machine language code written to the wire.

http://mozakai.blogspot.com/2011/11/code-size-when-compiling... suggests otherwise in a head-to-head comparison: C code compiled to both a platform-native binary blob and JavaScript, then both gzipped (standard practice for any large JS file on the web).

And if you're talking platform-dependent machine code, that's likely _worse_ on the wire in practice, because unlike a platform-independent representation you can't do edge caching or other sorts of intermediate caches. But maybe you meant a virtual machine (i.e. what people ask for when they want a common bytecode)?

Re:The JavaScript Problem by kenjackson on Sunday April 27, 2014 @01:45AM (#7654117)
That's not how you measure bloat. There's a huge runtime associated with that JS code, and you ignored performance.
Re:The JavaScript Problem by bzbarsky on Sunday April 27, 2014 @10:33AM (#7655194)
Ah, you mean client memory bloat, not transfer bloat (i.e. pageload time). That part was not very clear at all.

You still need a nontrivial runtime (a la PNaCl) if you're going to provide the sort of sandboxing guarantees for the code people want for code that runs without explicit user opt-in. Or is that a non-goal in your case? In that case, I'd like to understand the problem you're trying to solve, since it sounds different from the one JavaScript in web browsers is solving.

I agree that things like asm.js have a performance hit compared to just running an unsafe binary blob. So does PNaCl (though the hit there is different from the asm.js hit: it has somewhat faster steady-state, but worse startup performance). Again, if you're not talking about something that has the same safety guarantees as JS and PNaCl you're comparing apples to oranges.

Re:The JavaScript Problem by _delirium on Sunday April 27, 2014 @12:53AM (#7654025)
I probably wouldn't invent JS if I were tasked with coming up with a bytecode for the web. But how bad is it, on the overall scale of things? I mean, x86 is not a great ISA, and yet has prospered, despite some real nonsense corner cases and an anemic register set. Is JS bad as a bytecode the way that x86 is bad as an ISA, or bad at a different level?
Re:The JavaScript Problem by AnthonyMouse on Sunday April 27, 2014 @01:28AM (#7654087)
There is a degree to which you can make anything do anything. Using JavaScript as bytecode is actually really easy if you don't care about performance whatsoever. And if you get together a group of smart people who try very hard for a long time, I'm sure you could even get it to have reasonable performance. Especially if the people writing the interpreter are talking to the people writing the compiler.

But why would you actually do that as a long-term solution? JavaScript isn't an ISA. It's not hard-coded into anything. You don't have to throw away all the existing JavaScript code to start supporting something else in addition to it. All you would have to do is write code to support whatever bytecode you actually want to use and contribute it to the major browsers.

Re:The JavaScript Problem by BugBrother on Sunday April 27, 2014 @06:10AM (#7654559)
I came into this discussion many hours late, but I have to ask: Why are people discussing as if asm.js didn't exist?! It is fast. (It even support integers!)

To me, asm.js looks like a bad kludge -- and a great road ahead, just extend it a little bit over the next decade with the stuff compiler writers will clamor for.

Am I missing something?

Re:The JavaScript Problem by AnthonyMouse on Monday April 28, 2014 @02:05AM (#7658437)
What you're missing is that there is no need to do that. Using JavaScript as an intermediary representation for other languages on a permanent basis is kind of ridiculous. Improving browser support for JavaScript-as-IR provides no benefit over adding support for a "real" IR and introduces superfluous complexity which is the enemy of both performance and security.

Compiling other languages to JavaScript is a tolerable transition mechanism while browsers that don't support something better are still popular, but trying to improve the ability of browsers to interpret compiled-to-JavaScript code from other languages is farcical -- if you're going to update the browser then update it to support something better than JavaScript, not to improve the performance of the horrible legacy transition kludge.

Re:The JavaScript Problem by BugBrother on Monday April 28, 2014 @01:25PM (#7661450)
I don't disagree with anything you wrote, except the first sentence.

But I'm cynical about the possibility to get a new bytecode standard out, without e.g. MS sabotag... extending it. Half the value of asm.js should be that if there is a working system already in place, the motivations for such shenanigans lessens.

Re:The JavaScript Problem by AnthonyMouse on Tuesday April 29, 2014 @12:24AM (#7664951)
EEE wouldn't work here. That only works when you have overwhelming market share. Microsoft has basically no browser market share on mobile and even on the desktop they no longer have so much that developers can afford to ignore everything else.

The way to handle Microsoft in a situation like this is to ignore them. Support the same IR on Firefox, Chrome and Safari and then Microsoft can play along or go home. At that point the worst they could do is implement an extended version that allows sites made for all browsers to work with IE but then encourage developers to make sites using Windows-specific features that won't work on other platforms. That's effectively what they tried with Silverlight and you see how far that got them.

Re:The JavaScript Problem by bratsche on Sunday April 27, 2014 @12:57AM (#7654035)
Why do you care what the target of the compile is if you're happy with the language you're writing in? That seems like being upset that some code you wrote in a language you like might be running on some processor you don't like.
Re:The JavaScript Problem by Twirrim on Sunday April 27, 2014 @01:43AM (#7654106)
Because it requires some really difficult conversion work under the surface. For example, JS has no concept of integers. To shoe-horn other languages in involves a lot of extra work, which by nature is going to be less efficient. Worse, the javascript engine's JIT can't take advantage of some of the semantics of the language being converted, e.g. if the JIT knew for sure that the number was an int, it could take advantage of that in various ways, or make a smarter decision based on the known performance characteristics of the hardware it's running on.
Re:The JavaScript Problem by SiVal on Sunday April 27, 2014 @04:49AM (#7654416)
because it's inevitably a leaky abstraction
Re:The JavaScript Problem by stplsd on Sunday April 27, 2014 @01:19PM (#7655900)
Jesus, the next time I will read phrase 'leaky abstraction' I am going to throw up.
Re:The JavaScript Problem by tim333 on Sunday April 27, 2014 @04:01AM (#7654340)
I guess Google's Dart is trying to go that way. Not sure it's going to get there though.
Re:The JavaScript Problem by cbhl on Sunday April 27, 2014 @12:41AM (#7653994)
The web was multi-lingual, it's why script tags have a "type" parameter.

I personally think that the death of plugins came about as the result of the iPhone, because x86 binary plugins won't run on an ARM-based phone.

Re:The JavaScript Problem by chipotle_coyote on Sunday April 27, 2014 @03:28AM (#7654270)
Uh, scripts aren't plugins, right? In theory there could be other scripting languages for use in web browsers, which is what the "type" attribute would have been used to indicate. It's just that nothing else ever really took off. Other than Microsoft's VBScript, the only browsers I can think of that supported other scripting languages were early proof-of-concept attempts like Grail.

I think you're a bit confused about browser plugins in general here. They're OS-specific and CPU-specific. I can run Linux, Windows and OS X on a MacBook, but I need OS-specific versions of both the browser of my choice for each one and the browser plugins of my choice for each one. If the iPhone has helped contribute to the decline of plugins, it's because iOS doesn't support them at all, full stop -- if it did, though, they'd have needed to be compiled in iOS-specific versions even if the iPhone used x86 chips.

Re:The JavaScript Problem by cbhl on Monday April 28, 2014 @01:18AM (#7658304)
I was responding to two statements in one comment; I apologize for the confusing non-sequitur.

I do agree that plugins are OS and CPU specific, but I think the popularity and explicit lack of support in iOS and Mobile Safari (which, to my understanding, are stripped down ports of OS X and Safari) really caused web developers and their clients to move off of Flash (and other plugins). For a number of years, Adobe shipped a version of Flash for Android, and I'm under the impression that they were always willing to port it to other platforms/devices if paid enough money (since the Linux binaries had an explicit restriction from inclusion in devices; you could only use them with general-purpose PCs). Java ME ran on phones for years, too, although I don't recall anyone shipping a mobile browser with Java SE plugin support. If someone (a client or web developer) wanted their website to work on iPhone, they had to remove the plugins, and I think many people just started designing around that constraint.

Re:The JavaScript Problem by rdtsc on Saturday April 26, 2014 @11:13PM (#7653761)
I think it comes down to two things.

* Acknowledging that it has many many warts. Weak types, modularity, every number is a double, etc. some of those warts are there. Explaining why they are there historically doesn't make them go away either. "Oh but it was only written in 1 month so.." -- "Yeah it is still broken".

Many things can be chucked to "not broken but different". But, some are just broken, no sane language released after 1990's should not be this broken unless it was on purpose (brainfuck).

* Realizing that in practice, currently, there are not too many alternatives and just learning to use it. Like you say just getting down to being pragmatic. One can talk about functional purity but that doesn't usually bring food to the table. A finished site does.

But you see how the two are different. Many confuse them. For example, saying the language is fine and great just because they learned it and are using it. Yeah, at some level we want to feel good about the tools we use, so it is harder psychologically to say "this stupid thing I have to use every day". It goes the other way, just because the language is broken and there is some pure language, re-writing the site into might be very risky.

I personally am looking at Dart with excitement but at the same time won't be urging anyone yet at work to switch to it yet. Kind of a chicken and egg problem, I understand, we just can't afford to take that risk yet. But with time who knows.

Re:The JavaScript Problem by Swizec on Saturday April 26, 2014 @10:41PM (#7653685)
Javascript's real problem is unfamiliar semantics hidden behind familiar syntax. It throws people off and they hate the language because they think it's weird. Really, they just haven't learned how to actually use it.
Re:The JavaScript Problem by gutnor on Saturday April 26, 2014 @11:09PM (#7653750)
Javascript is a tiny language, but it pack a serious amount of gotcha.

If you learn the javascript way, you just realise that the core language provide little to no tooling around it. Like if you really want to fully utilise the prototype based inheritance, or the functional aspect of the language, you will basically have to write a whole set of utilities and extension on your own.

Of course, javascript run in the browser, is small and super simple, so it packs a lot of fun. To me it oddly reminds me toying with assembly in the old DOS 16 bit days and I like it despite its flaws ( and well those flaws are being addressed in latest iterations ) But the flaws are real, and if you need to use it on a big project with a regular team, and not complete freedom of choosing the browser you support, javascript will make you cry more often than smile.

Re:The JavaScript Problem by nilliams on Sunday April 27, 2014 @08:47AM (#7654859)
> Like if you really want to fully utilise the prototype based inheritance, or the functional aspect of the language, you will basically have to write a whole set of utilities and extension on your own.

Hmm, this is definitely something of an exaggeration. With old-school cross-browser JS you just need something like Backbone's `extend` implementation (jashkenas posted a shorter example a while back [1]) for inheritance and underscore.js [2] for your functional aspects (assuming that's the sort of thing you mean).

If your criticism is that those things had to be written, then this was solved with ES5 which gives you `Object.create` [3] for your inheritance needs and `map()`, `filter()` et al for your functional stuff.

I certainly don't see why you'd ever need 'to write a whole set of utilities and extensions of your own', that reads like pretty hand-wavey and unfair criticism.

> if you need to use it on a big project with a regular team, and not complete freedom of choosing the browser you support, javascript will make you cry more often than smile.

I think you are depicting a pretty broadly-shit scenario that I don't think you can say any other language ecosystem deals with well. Trying to support many different clients is a nightmare across e.g. mobile and the desktop using any other language/ecosystem too, with same 'regular team'. And I don't agree there's anything particular about JS that will make you 'cry more often that smile' in that scenario, but that's obviously subjective.

  [1] https://news.ycombinator.com/item?id=7244023
  [2] http://underscorejs.org/
  [3] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create
Re:The JavaScript Problem by flywheel on Sunday April 27, 2014 @12:09AM (#7653930)
Considering the language was created in about a week, Javascript is nothing short of a computing miracle.

It has enabled anyone with a web browser to write software that can run on billions of computers.

And it is extremely easy to learn and use, with no boilerplate whatsoever.

People who require the boilerplate to feel comfortable probably shouldn't use Javascript, and should have their heads examined.

Re:The JavaScript Problem by baddox on Sunday April 27, 2014 @02:13AM (#7654166)
Doesn't that argument get weaker as JavaScript gets more widely used? It's already one of the most prominent languages in the world, and is probably increasingly the first programming language encountered and learned by beginners.
Re:The JavaScript Problem by mgsloan1 on Sunday April 27, 2014 @10:12PM (#7657897)
One problem is that these users do not necessarily understand the semantics. I helped a friend of mine, a reasonably competent programmer, debug his javascript once. He had no idea that you needed to use "var" in order to make a variable local! He'd written quite a lot of javascript without triggering any funny behavior until this bug.

So, instead, imho, the argument gets stronger. Why is the first programming language encountered by beginners these days full of traps? How cruel! What a poor impression of programming they must get.

Re:The JavaScript Problem by teacup50 on Saturday April 26, 2014 @10:50PM (#7653705)
> Where are the killer apps that were developed through a niche JS transpiler? If you want to build a web app in Haskell instead of the web's true lingua franca, all you're accomplishing is making it harder to hire and harder to collaborate.

You're right about one thing: JavaScript is awful, but trying to avoid JavaScript while targeting a web browser is worse.

I simply don't write software for the web. HTML/CSS/JS is too much of a train wreck of bad design for me to find any pleasure whatsoever in the task, and I'm content leaving the job anyone that can stomach it.

However, if we followed your argument to its conclusion, we'd never escape the atrophied staleness of a browser technology core that somehow has never moved past 1995-era Netscape.

Re:The JavaScript Problem by camus2 on Sunday April 27, 2014 @01:56AM (#7654136)
> The people who hate javascript the most are the ones who wish it was something else.

Ask yourself,how can a single language can fit every use cases,for every developpers? We cant agree on what we should use on the server,but we are basically forced to use 1 language in the browser,of course some people are going to hate it.

I shouldnt have to adapt javascript,javascript should adapt my coding style.But aint gona happen.

Weak typing and Type coercion are imho the biggest flaw of javascript,I can live with the rest,but not that.

Proxies will help a bit though.Devs will definetly abuse them.

Re:The JavaScript Problem by Peaker on Sunday April 27, 2014 @01:53AM (#7654132)
IMO the biggest problem, even worse than type coercions, is the error silencing regarding wrong argument counts.

This can easily hide so many bugs, it's amazing it is acceptable to anyone.

Re:The JavaScript Problem by briantakita on Sunday April 27, 2014 @11:57PM (#7658159)
In practice, this issue is rarely (like 1-2 times a year) encountered. And then functional tests catch the error. It would not be acceptable if it was a constant thorn.
Re:The JavaScript Problem by Kiro on Sunday April 27, 2014 @04:51AM (#7654421)
Do you have an example?
Re:The JavaScript Problem by Peaker on Sunday April 27, 2014 @05:56AM (#7654535)
Call a function "foo" that expects 3 arguments with 5 or 2 arguments, instead.

Javascript will gladly accept that and not yield any error, not just at compile-time, but even at run-time!

Unless the function explicitly checks that the argument list is correct, it will simply hide the bug.

When changing function signatures in any language, it is the programmers' collective responsibility to fix all callers (some of which in the same working tree, some hidden in others' branches, etc). In a static-language, it is very easy, you get compile-time errors at some point. In dynamically typed languages, it is slightly harder -- you get an error (ideally under a test suite) and fix it there. In Javascript you get cryptic bugs. No compile-time error. No run-time error. Happy debugging!

Re:The JavaScript Problem by LunaSea on Sunday April 27, 2014 @07:54AM (#7654772)
How is that an issue ?
Re:The JavaScript Problem by Peaker on Sunday April 27, 2014 @12:29PM (#7655688)
How is a common change (updating a function signature) easily causing cryptic bugs rather than a visible compile-time or run-time error an issue?

Bugs are bad?

Re:The JavaScript Problem by implicit on Sunday April 27, 2014 @11:09AM (#7655350)
Guess the result of this fragment:

    ["1", "2", "3"].map(parseInt)
Re:The JavaScript Problem by Kiro on Monday April 28, 2014 @12:49PM (#7661126)
Ok, that's crazy.

Here's the explanation for anyone interested:

http://stackoverflow.com/questions/14528397/strange-behavior...

Re:The JavaScript Problem by Aardwolf on Sunday April 27, 2014 @06:00AM (#7654542)
> verbose function syntax

I didn't agree with that one myself. I mean, ok, now we have C++11 and can just type [](){} for inline lambda functions, but before C++11 you needed a huge amount of boilerplate to pass a function to something. JavaScript's way to just type "function something" inline was so non-verbose and convenient compared to that.

So what is it being compared to instead here? Functional programming languages?