Reconsidering Javascript

I’ve said before that I don’t like Javascript. I should be more precise.

Some things bother me about javascript itself. First, I find it hard to debug. When PHP dumps and error or exception, you know it. No question (assuming you have your error reporting turned up). It requires extra work to determine when Javascript has dumped out with an error.

Second, Javascript is used to defeat the web’s page-at-once paradigm. You know how this works. The server presents a page of data to your browser, and you push a button or click on a link. The server processes your selection and presents a new page. There’s no such thing as per-field operations on the web. At least, not as it was designed.

Javascript short-circuits this by triggering on web page events. Click on a field and get a drop-down calendar. Fill in a field and see a whole form appear below it. Do something else, and your browser sends a data request to the server’s database.

I was raised on C programming. In C, you can do any number of things on a screen and obtain any number of responses in real time. I like that type of application. But the web was made to react more like those old RPG programs where you fill in all your data, and then hit the “Send” key (different from the Enter key).

I don’t mind web applications being more responsive, but it presents two problems. First, the web gets a lot more busy and noisy when Javascript applications are constantly sending data requests to servers. Second, having to attached Javascript event handlers to all the various events which might happen on a page is hairy programming at best. Talk about hard to debug….

The third major problem with Javascript is that it has anonymous functions, or lambdas. If you’re not familiar with anonymous functions, here’s an example:

’’' function counter() { let count = 0; return function() { return ++count; }; } ’''

Notice in the middle of the “named” function, the function with no name. I’m not sure who thought of this idea, but I can’t find a good reason for it. In all the 45 years I’ve been writing code, I’ve never found a need for it. You could rewrite the above as:

’’' function counter() { let count = 0; return ++count } ’''

I wouldn’t mind this so much if I could find a reasonable application for it. But I can’t. Much worse is the fact that everyone and their brother has jumped on to this bandwagon. It makes the code much harder to decipher.

A twist on this makes it even worse. I can’t seem to find an example of this online. Nor can I find out what this technique is called. But it looks something like this:

’’' function alfa (function () {a + b}); ’''

Notice that in the area between the parentheses (which normally describes the variables passed on the stack to the function), we’ve defined an anonymous function. What? I’ve seen a lot of this recently in code, but I can’t for the life of me figure out what the point is. And to me, it just makes the code harder to read. In particular, I’ve seen a lot of this in Javascript code.

Now, to be fair, PHP has introduced anonymous functions. I’ve looked over the example code in the docs, and I still don’t see the point.

But my biggest complaint about Javascript isn’t about the language itself. It’s what people do with it.

When i pull up a typical web page’s source code, you can’t imagine how much Javascript I see. Hundreds of calls to Javascript routines to do any number of unnecessary things. Page load times go up. Complexity increases exponentially. Moreover, there are some applications, like Facebook, which have so much embedded Javascript that it sends my CPU usage sky high. I can’t keep Facebook open for long, just because it pins my CPU. Firefox sucking memory is one thing, but this is too much.

For decades, I’ve written pure PHP code for web applications. In only one case did I use AJAX (Javascript) to fetch customer details and job numbers. Otherwise my working code makes no use of client-side scripting. Forms get processed, databases get updated, and everyone is happy. No Javascript.

At this point, I’m pretty much done with all the applications I need to write. At this point, it’s maintenance. I may add some features or fix some bugs, but for the immediate future, I’m going to be working on some construction and art projects.

Now, as a programmer, I don’t collect fads and languages. I’m happy with C, PHP and Bash (although I am intrigued by Google’s Go language). They get the job done.

Still, the code I write is no frills. I’ve got some processes which require 6 separate page loads to complete. Javascript could possibly cut those page loads down to a single page. Javascript has some quirks (e.g. prototypes versus classes), but it’s not far different from C. After all, javascript inherits from C.

There are libraries which shortcut using Javascript, like JQuery, which is apparently the most popular of these. I downloaded JQuery. Uncompressed, it’s 290K. Compressed, it’s 72K. So if I want to load a page using this library, I have to drag a minimum of 72K worth of code, most of which I won’t need.

I’m one of those guys who likes economy of code. It should be readable, easy to understand, and not take up too much memory. And as I’ve always maintained, web applications are one page, not a whole accounting program that must remain in memory all day.

I have a long ways to go to master Javascript. So you’ll have to wait for more information. But I’m optimistic. If used judiciously, I think Javascript could be a boon to taking some of the load off servers and making pages simpler to navigate.