Wrestling Javascript with Love

By which I mean server-side web development, and client-side web development if you imagine that those other languages were available in the browser with the same stupid DOM API that browsers expose.

I think English just lost.

And that’s my point, really. No language is going to be clean or pretty if it has to live in this environment, dealing with HTML entities and DOM trees and all kinds of other nasty browser features. Even if you used functional lisp to do the job, somehow it would wind up looking horrible in the actual implementation. So it’s really not Javascript’s fault that its more interesting applications tend to be distressingly ugly.

The earliest Javascript implementation was as I recall mainly intended for tiny applications like validating browser form inputs to avoid server round trips. It’s really a credit to the original design that it’s usable at all for the enormously complex applications you see today, but you can also understand why everyone keeps trying to come out with alternatives, from Visual Basic on down to Google Dart.

I don’t know why people say that the DOM Api is bad. Its because in most implementations the calls are very expensive? Its because are very low level? (you modify nodes, but rarelly have ‘batch’ operations where you modify a range of nodes).

?:-)

Slight digression from the topic, but I found it interesting and useful.

I found myself having to dust off my JS knowledge for a project involving Google Apps recently. I was just getting used to the weak typing and starting to rev up into things when I came across a problem I would normally quickly solve with Linq. I thought, “man, wouldn’t it be awesome if someone implemented something like Linq in JS?”

As it turns out, someone has.

It’s a pretty good implementation too. While I still don’t prefer using JS over C# (too spoiled by all the coddling I get from VS) it goes a long way toward making it a language that I will be much more likely to want to use.

You should check out Script#

You write C# and it compiles into javascript. The last two large web apps we did would have been chaos without it.

Another cool thing about JS is the passion of the developers, people using Javascript are inventing things all the time. Cool stuff :D

Heres something:
NowJS

It seems a reinventation of Java remoting, done right.

It’s worth noting that a lot of the Linq methods are just standard functional programming functions renamed, so you can get a lot (maybe all?) of that functionality with something like Underscore, which is more common in the Javascript world, and more idiomatically JS-ish. Where() is filter(), Select() is map(), Aggregate() is reduce(), etc.

And there’s also the reactive extensions (Rx) for JavaScript – basically LINQ to Events. Want some SERIOUS JavaScript nerdery? Check out these blog posts about it.

It’s still possible to miss out on SelectMany, though, which is the most important one, as this article explains when talking about why LINQ is a nice pattern. But you’re basically right – this is all fairly standard relational / functional terminology.

Your article is paywall-blocked, but it seems like you could use flatten in conjunction with map to get the same output?

I think it takes nothing away from Linq to say that it takes the abstruse concepts of functional programming, and turns them into something that’s familiar to line-of-business programmers, so that people who would be terrified of Lisp (or even functional Javascript) can use functional programming.

JQuery allow some of this “Query in a chain” style. The key for this is that functions returns a object of his own type (or a array of objects). So you can attach functions one after another, with perhaps a .find( ) function in the middle with a selector.

I am trying to teach me to use underscore, and write my code in a more functional way, but Is a long term project, since I have learned to program in procedural, and wen I relax is how I tend to write.

The reason I am tryiing to learn underscore, and the functional way to write code, is because I think is a superior level of abstraction. If you write a for ( var t=0;t<maxT;t++) {… , there are a lot of things that can go wrong, while in a .each( function(t){… theres only one. Is like with the for you have to deal with details of implementation that are meaningless, making stuff unnecesary more complex. What I want is to be able to write code with less bugs, that is easier to read, smarther, more powerfull, easier to modify, and all that I think will be feasible with a more functional based programming.

If you really want to tackle functional programming, why not spend some time with a language like Haskell or a Lisp/Scheme-like language? I learned everything I need to know about FP (and then some and more) from spending time with Haskell.

No doubt because he’s doing web programming.

Yea, thats the main reason. The other one is practical, I want to learn things that I can inmediatelly use to conquer the world and slave the human rac… ooops, I accidentally leaked my plan.

Javascript + Underscore looks like would let me do some basic steps in functional programming land, withouth abandoning everything else I already know and love.

I want to become a better programmer in the languages I actually use.

If you found JavaScript that painful, did you ever investigate CoffeeScript?

Javascript is fine, I have writte a lot of code in it, some of it horrible, some of it honest to god.

CoffeScript may add things I like, but from the examples I can see the syntax is not for me. I am a C guy, I like to use “{”, “}”, and I don’t feel good in a language where whitespace is specially important.

Also, everything you use will break, and wen something is very popular, like JQuery or Javascript, you have blog posts, comments, lots of Q&A in sites like StackOverflow. If you choose a small language with few followers, you lost all that, so is like you become blind and are alone with your problems. Thats not good at all, is something to avoid.

Javascript has enough syntax sugar to write in any language you may want, I have see people write C code in Javascript ( WebGL people ), and I have seen people pretend Javascript is Python or Ruby and not have a single “;” in the code ( the bootstrap.js code ). Java programmers can pretend Javascript is Java, and it mostly work, if limit thenselves to a small list of tricks that work. So theres not real reason to write in anything else than Javascript.

Also what if CoffeScript has bugs, or is alpha, and you get not just your bugs, but the bugs of the CoffeScript compiler. Until proven otherwise, all software is buggy as hell… I don’t want to bet my life in alpha software, wen I have seen suposedly mature code enter production and fail to meet expectations because the code is a horrible infestation from hell (osCommerce).

Not sure if you considered it, but you could probably make the program in Java, and just have the browser launch it, rather than actually try and stuff it into the browser. Java is a hell of a lot nicer to use than JavaScript.

It’s a better language, but in the browser it’s also more punishing to users.

I guess?
I mean, what’s so punishing about it? It just pops out of the browser and runs as an application on their machine. It ends up getting the same type of universality, because pretty much everyone has a Java VM on their machine.

Whoa there, lets not say things we can’t take back!

I’ve been playing with CoffeeScript; I like it a lot and I’m generally not a fan of cross-compilation these sorts of cross-compilation tools. I loath GWT, for instance. The great thing about CoffeScript is that its semantics are almost identical to JavaScript. While tools like GWT and ClojureScript are attempting to shoe-horn completely different semantics and abstractions into JS, CoffeeScript just gives you different syntax.

Another added benefit is that CoffeeScript has a few JS best practices baked in. It defaults to the narrowest variable scope and strict equality comparison, for instance. These may seem like small things, but even the most experienced JS programmer can get bitten in the ass by them every once and a while. CS also adds a few handy shortcuts around the verbosity of JS’s prototype base object system.