Javascript Rants (GHD012)

Javascript Rants (GHD012)

"A bunch of open windows" I’ve been working on this Javascript project, and with the final trial are coming up on Tuesday we’ve been steadily adding additional features as the researchers think of them. One innocuous request was to add event logging of some kind, redirecting some of our existing monitoring with timestamps, and so I started it today thinking it wouldn’t take too long. I ended up having to clean-up and rewrite quite a bit of code to actually do useful logging that would be helpful to someone who is trying to understand what meaningful interactions happened at what time. I just finished it up at around 345AM, and then remembered OH CRAP I didn’t do any goals-related work today!

I’m wiped, so I am doing the minimum under the goal Sharing Something I Know. It’s worth a 10 point result, and so I am going to yammer on a bit about Javascript and what I think about it, if you were perhaps thinking it might be good to know. Then I am going to go to sleep.

What should you know about Javascript?

Well, I could go into the history of the language and build a careful narrative about its place in the world of computing, but instead I will impart a few facts about it:

It’s in your browser and it’s everywhere! And it’s increasingly in your web server, doing things! And it’s becoming one of the major languages of this decade for general-purpose programming. If you know Javascript these days, it’s a great platform for getting a job as it is hugely in demand. Though it started out as a kind of toy language that was slow and crappy, these days there are a lot of surprisingly powerful things you can do with it. The modern HTML5+Javascript platform provides hardware-accelerated 3D graphics, capable sound reproduction, streaming video conferencing support, and networking, so you can do quite a lot of fun things with it.

Because Javascript is in the browser, you can share what you do with everyone very easily. And since browsers have standardized to a great degree so the same Javascript runs the same way on each one—this was not the case even 5 years ago—you have the world’s biggest computing platform to show off your stuff. Not only that, but scores of programmers have contributed tools to overcome the tedious shortcomings of Javascript development that more mature languages solved decades ago. For example, Javascript doesn’t natively have any kind of ‘type checking’, which in layman’s terms is like riding a motorcycle without a helmet or not having seat belts on a roller coaster; it’s your own damn fault if you’re not CAREFUL and you spelled something wrong and it blew up. However, many tools have arisen around Javascript to make it more convenient and less screwy.

For older programmers like me, Javascript is also hard to understand at first because its syntax (that is, grammatical conventions) looks like languages familiar to long-time programmers such as C, C++ and even Java. However, it is totally not like any of those languages in fundamental ways; old programmers like me are thrown by its surface familiarity and maddening differences. If you are going to learn Javascript, you need to LET GO your expectations that it works like a competent suite of C/C++ style development environment tools. Javascript is crap in this department. I thought it was not worth learning for years because it seemed like a toy-like heap of steaming garbage. That is, until I read Douglas Crockford’s Javascript: The Good Parts and started to understand its chameleonic nature. The short story is: yeah, Javascript has some terrible things in it, but there are also some very powerful ideas in it. If you can let go of your desire for strongly-typed compiled procedural language (all of which Javascript is not), and if you can wrap your head around the idea of prototype-based object oriented functional programming (which is totally not like what you’re used to in C/C++), then it’s actually pretty neat.

  • Everything is an object, including functions. Objects have a variety of properties, which are also objects. Objects objects objects.
  • You can attach a property to any object without having to declare it somewhere.
  • To create a “class object”, you make an object you like, and then use it as the parent of a descendant object. You make chains of these kinds of objects to define a class hierarchy; you don’t have the ‘class’ syntax approach in languages like C++ or Java. Though that has changed; the latest version of Javascript, ES6, has support for class declarations in a more ‘traditional’-seeming style.
  • Scope is weird, because Javascript does not have block scope like you’re used to. It instead has functional scope, and functions are used by Javascript programmers to create namespaces. One cool thing though is that Javascript has a concept called “closures” which allows one to write code that refers to any variable reachable within the function scope even in event handlers that normally would lose access to those variables when they are invoked. This is SUPER COOL, but it will seem very weird at first because…
  • Functions are used like wrappers inside of wrappers, and since they are also objects they are often used in an object-like way. Javascript programmers use a lot of anonymous function declarations as function parameters themselves, and sometimes they get used to provide some partitioning of the namespace. So when you see stuff like (function($){ $('#content').hide()})(jQuery); and go WTF IS THAT, it’s an example of using an anonymous function to create an protected namespace where you can use any variables you want inside it in a completely self-contained way. It does this by defining then executing the function (note the () that invokes the function rather than declares it, and the () wrapping it is a convention so you notice it’s not a function declaration; it’s also common to see + used). Yeah, it seems ridiculous at first because any other language would just have block scope or a namespace {} style declaration…

In some ways, Javascript is like an object-oriented construction set from some alternate reality, if your current reality is C++, C#, Java or even Actionscript. Once you get used to its ways, you can start to construct some very expressive object-oriented code structures. This is also one of its liabilities, because there is so much flexibility that every programmer implements their object-oriented modules in a different way. In a language like C++ or C#, you learn the syntax for declaring classes and that’s it. In Javascript, you learn how to think like the author thought good object oriented design should be like because it doesn’t look like your code.

Rant Over!

I guess that’s enough rambling for now. I think there is probably a way to introduce Javascript to experienced old programmers like myself; it took a long time for me to come around to the Javascript way of thinking and be productive programming in it. Despite its ubiquity and ease of access, there are still a lot of hair programming concepts that people need to learn…or do they? I HAVE NO IDEA.

Points Scored and Next Steps

10 Shared something I know!
3 Tried something new I wasn’t sure would work! (this article)
2 Posted words about the work to website!

That’s 15 points! Not bad for a lost day. It’s 5AM now and I’m going to crash.

About this Article Series

For my 2016 Groundhog Day Resolutions, I'm challenging myself to make something goal-related every day from February 2nd through December 12. All the related posts (and more!) are gathered on the Challenge Page.