Saturday, March 19, 2016

Is it defensible to write industrial software in JavaScript?

I've been working for some time on an industrial instrumentation system. The software is predominantly written in JavaScript. That is such an obscure choice - why not in a Microsoft language, or in a more typical PLC framework?

The "firmware" that runs on the sensor boards has always been written in C. This is very traditional for programming microcontrollers. Some other options are emerging, including embedded JavaScript, but none seems to come close to C yet.

The first version of the software that runs on the server was written in C#. This was my first real project in C#, but I think that anyone with Java experience can be proficient at C#. C# seemed a responsible choice - something that anyone should be able to pick up after me. The web client code was necessarily in JavaScript. I suppose Flash, Silverlight, Java applets (remember them?) or even CoffeeScript would also have been possibilities, but it's not much of a simplification to say that JavaScript is the only option for browser-side programming.

There's respectable advice from extremely intelligent people suggesting that big re-writes are rarely the best approach.

But I did a rewrite anyway. I switched from C#.Net to NodeJS. There were a few reasons.

  • The code was becoming harder to maintain. I hadn't been refactoring enough. I could claim it was "technical debt" but I think it was just a mess.
  • I was drawn to the emerging NodeJs bandwagon/fad.
  • I felt myself becoming more proficient at JavaScript, and enjoying the lightness and simplicity of being able to do everything from a simple text editor and browser, without the "heavy" IDE required for C#.
  • It seemed like JavaScript would become a serious language. There was so much happening in the JavaScript community: so many nice open source packages, test frameworks, and clever people offering advice.

As the luminaries above would have predicted, the rewrite didn't solve all my problems. The new code is certainly not a model of programming virtue. But there is no question that it is better now than it had been. Most importantly, there are unit tests. It would be ridiculous to say that C# doesn't allow or even encourage unit tests. But somehow the JavaScript test frameworks (Mocha and Jasmine) feel simpler, lighter, and easier to use. Continuous integration servers like Jenkins can run these tests easily, without relying on that enormous TFS monolith.

The event-driven aspects of JavaScript took some time to get used to, and it took even longer to reach what I feel is a level of proficiency. Again, it's entirely feasible to be event-driven in C#, but just more natural in JavaScript. My software is even now broken into separate self contained services. Clearly I can't leave that without calling them "microservices". (If only I can get Docker into the picture, I'll be up with all of the people who speak at conferences these days!)

I've just seen the results of the 2016 stackoverflow.com survey. It's interesting, gratifying, and perhaps a relief to see that JavaScript is again the most popular language. I doubt that this would be true of the subset of people developing industrial instrumentation systems, but I feel that IT/OT convergence is happening whether we like it or not, and I'm not afraid to be helping it along. In looking at language popularity, it's also useful to look at some other surveys:
TIOBE has JavaScript at number 8 (behind Java, C, C++, C#, Python, PHP, and Visual Basic).

An interesting aside: the Stackoverflow survey found Visual Basic to be the most hated language!
There are similar results from Stephen Cass in IEEE Spectrum  and PYPL. Two github analyses put JavaScript at the top. Clearly all these surveys are estimates, and the concept of popularity is vague anyway. But the message is: I don't think JavaScript was too obscure a choice for a modern software system.

I mentioned the openness and momentum in the JavaScript world, which was one of the points of attraction. People have recently been speaking of JavaScript Fatigue - difficulty in trying to keep up with all the alleged best practices that are emerging so quickly.  I have experienced this. But I don't believe it will be fatal, or even serious. It may encourage a degree of extra caution before adopting the Latest New Thing.

For anyone overwhelmed by the apparent complexity of the JavaScript world, there are some good recommendations to get you started.  I'm a bit behind, since I haven't made it into React yet. Maybe one day. It won't take a rewrite!

I now have the opportunity to bring some new people into my project. It feels a bit overdue. But it makes me realise how many giants' shoulders I'm standing on. There are so many things for a newcomer to learn, that it's hard to know where to start. Maybe that's why I had to write this blog.