For the love of… Javascript!!? (or nostalgic musings of a polyglot)

Javascript.

I wouldn’t have thought that a language that pretty much threw out all of the rules of good language design would come to occupy a place in my heart, standing with the likes of my romances with C++. Let me take you on a journey…

I started my career like many other computer programmers of my time in the C/C++ era. I mastered my pointers. I mastered pointers to pointers. I mastered pointers to arrays. I mastered pointers to arrays of pointers. How many stars is that, ***?

But then things changed.

Direct access to memory was now looked down upon. In came the garbage collectors and took away the pointers. Only embers remained, NullPointerException — then your program died. Ouch!

Programming languages were focused on making things safe. The clever stuff that only few programmers cared to understand was removed so that large teams of programmers could work away, drones building software? The clever stuff was clever, but hard to debug. Companies got worried. What would happen when their star programmer resigned? Who would debug the *** they left behind?

I think it started with Java. It must have…

You have to declare all the possible exceptions your method can throw. And the person using your method will have to handle them all, or re-declare them. The compiler was watching all and getting harder to please. Try, what a pain in the, NullPointerException. Catch, should it have been a NullReferenceException, I wonder? Did they get the naming right finally?

I understood the reasons but I missed the clever stuff you could do in C++, if you knew how to do it safely and properly.

Meanwhile Microsoft developed C# and incorporated some of the best features of popular languages like C++, Java and Python. C# is a fantastic language to work with. Thank you Microsoft.

Microsoft had its pulse on what programmers loved. They brought back the good stuff like function pointers, err no, delegates, ahem. Lambda functions, high-five Python. And string, after all “the world without string is chaos”1Lars from Mousehunt (1997). Any C++ programmer worth their salt has attempted writing their own String type that they wanted to be better than any other out there, but landed up being full of bugs, except std::string of course. And C#s string works with Unicode, no fuss. C# gave me hope. Await.

They saw the potential of this nascent language that did not shoehorn you into types or classes of objects.

Software engineering is maturing as a discipline. Was it ruby? on rails? The tools got better. I mean Objective C, Apple, seriously!? But with XCode by your side completing the code, even that was manageable, stay strong. Not just code completion, there were helpful tips from the compilers. Take C#, not many complaints about how it is unhappy with your code, just gentle reminders in the right direction, like that variable you perhaps forgot to initialise. But there was more awesomeness in the works, like refactoring by renaming functions and extracting parameters in Visual Studio. And dependency management. A golden NuGet.

Unit-testing got big too. Clever mocking libraries, test runners, coverage analysers. The tooling around programming just got so much better.

Thinking was changing, again. You didn’t need a large team of programmers to churn out code. More didn’t mean faster. The waterfall had dried up and you had to be Agile. The marketing department went to town. They sprinkled it liberally over the corporate website. Do you Agile to survive or do you survive to Agile? What’s your mantra? Got a coach yet?

Languages still remain primarily safe. No, you aren’t allowed direct access to memory like in the old C++ days. Those were bad practises. Still cool though, being able to de-reference pointers to *** in your head, I think. But yes, the safety offered by modern programming languages is reassuring. Delegates, lambdas, async. It is well thought out and cool in its own way, I guess.

The familiar curly bracket? A false sense of security. Your vars are still in scope.

Then there is Javascript. The vagabond. Return; and please keep your brackets on the same line. I had tried my hand at it, in the browser of course. Small snippets of Javascript, getting larger and unwieldy very quickly. I always ran back to the safe confines of “proper” languages. I appreciated the compilers and loathed the Javascript interpreter. I encapsulated myself away from the horror. Error, [Object]. IE7. Alert, no debugger. Go figure.

But I know some others persisted. They saw the potential of this nascent language that did not shoehorn you into types or classes of objects. They came up with JQuery, JSLint, promises and polyfills. It was something about it being fast, asynchronous, single-threaded they said. Something nimble about it I didn’t seem to get then. I felt it was unsafe and safety was paramount wasn’t it? But the ones that persisted had promises to keep, and miles to go. And they kept going, building then breaking pyramids of doom.

Sweat, tears, Angular. Whoa! You couldn’t turn away from Angular. And NodeJS built on top of V8. And a web server in a couple of lines of code. You just had to see it to believe it. The Web, now a platform for apps with Javascript coursing through its heart. Open-source was coming back, but not like in the GNU era, with the whole copy-right, left, what? debate. It was simple. It was modular. It was asynchronous. Because it was linted and unit-tested, it kept its promises2JavaScript Promises: an Introduction. REST assured. Have a Mocha, or Chai, if you prefer. Careful don’t release Zalgo3Designing APIs for Asynchrony.

Yes, it is difficult moving away from the safety of classes, types and a watchful compiler, into the seemingly lawless land of vars and functions. From sequential steps to asynchronous events. The familiar curly bracket? a false sense of security. Your vars are still in scope and this has changed under your nose but not that. You need to be strict. It requires a different way of thinking. But then it clicks. It is unsafe, a lot of it is unsafe. The challenge of writing good safe Javascript is perhaps equal in skill to writing good safe code in the romances of C/C++ where pointers lay bare your memory. But remember Software Engineering has matured since. The tools are so much better, and free! Linters and test frameworks. Is your code covered? The community and the collaboration is great. The examples have gotten better.

A language need not be safe but your approach to working with it can be. Javascript requires discipline of method and a rigour in approach. And yet it demands a new way of thinking. It can be as much or as little as you want it to be. It comes with freedom but it demands skill.

Javascript has brought me closure. Here be dragons.

To Javascript, with love. KISS. ({})

References