Wednesday, March 11, 2015

A ramble on web development from a non-millennial developer's perspective.

Having a background in desktop and embedded software development, web application development technologies frequently stymie me. I get "the good parts" of Javascript, but I often feel like Javascript makes what I've come to consider good practice in application architecture difficult. The loose typing of Javascript is an appeal to so many, but it can make it hard to maintain a very large code base.



var foo = function (payload) {
    var timestamp = payload.getTimestamp();
    //some code that does something with timestamp, etc...

There is an inherent code interface requirement for anything passed into the function foo that it must have a member function named getTimestamp, and that function must return a type suitable for the expected operations. 

There are essentially 4 ways in Javascript that a developer who wished to use function foo can discover this implicit requirement:
  • Inspect the internals of the function foo, to learn all of it's expectations of the passed in type
  • Read documentation bequeathed by the author(s) of foo
  • run a "lint" -type tool for static analysis to catch improper usage. 
  • run the code and debug the exception that gets thrown when you pass it an invalid object.

All of these options, except maybe the lint option require manual human action. All of the options, including the lint option, are quite fallible. I did not include a 5th option that is unit testing, unit testing is a good thing, but it does not even begin to help this particular problem. For starters, the unit test will not replicate the real world usage. Where was the payload object created? It might come from a call back from within a callback from within a callback using an object stored form some earlier callback. Similarly, lint tools are a good thing, but they are far from perfect as well, and more cumbersome to use than a compiler.

compare the Javascript with the equivalent C#

interface IPayload
    DateTime getTimestamp();

void Foo(IPayload payload)
    var timestamp = payload.getTimestamp();
    //do some stuff with timestamp

Now this example is trite, but the point is that as the size of your application grows, and you pull in more and more 3rd party code, the occurrence of problems surrounding the "hidden contracts" in dynamic languages like Javascript grow. In the C# version (or just about any statically typed, compiled language), you know immediately, and so does the compiler, what is expected of the payload object passed to Foo, it's contract is clear, and rigid. 

There is a lot of thing appealing about how Javascript works and dynamic languages. Due to the nature of web the web, deployment has always been more of a challenge than with desktop applications. The ability to swap out a single file has it's appeal. Java server side tried to alleviate this with the servlet system and WAR files. It's also more terse, and superficially more forgiving. (Any object could be a "payload" in the Javascript version so long as it has access to the getTimestamp prototype. 

Also, there is this appeal in the perception that a change in a third party dependency won't break your application. I think the problem is with dynamic scripting languages is that your relationship to your dependencies becomes a "gentleman's agreement", and not an enforceable contract. Say in the next version of the foo library,  the authors made to following changes to foo:

var foo = function (payload) {
    var timestamp = payload.getTimestamp();
    //some code that does something with timestamp, etc...

Maybe this was documented, no big deal right? If your code was such that you could find everywhere that payloads were created, great. But what if it's not that easy? Again, this is trite, but illustrative of a general problem, use your imagination. To fix this problem could involve a lot of debugging, text searching, inspecting logs, etc. Then when you have to integrate your code into another department's application, you have to do it again.

In C# the compiler would immediately find all cases where the 3rd party library broke you. 

Ok that was a rather winded way of saying "dynamic languages pose extra challenges for large applications". This is also true of Python. This isn't to say I don't like Javascript or Python, that can do many things that would be hard or even impossible in other languages. The asynchronous nature of Javascript lets you do a lot without multithreading and the evil, evil shared memory synchronization patterns you must use with it. At the same time, I miss the comfort of knowing my object is an apple, not not having to debug to figure out I was suddenly passed an orange. 

One of the great things about Javascript is the ecosystem, and the shear massive amount of effort and work going into it. There are more Javascript libraries and frameworks and tools than one could ever learn in a lifetime. Javascript is ubiquitous,

The second aspect to Web Development that makes me marvel at it's popularity is way in which user interfaces are created, the DOM. Creating user interfaces in html is for the self-taught an execise in cooking "div soup". Go to any major modern website; Facebook, Twitter, CNN, and open the page in the dev tools in your browser off choice. Invariably, it's built with nested div upon div upon div's. The css is typically where the magic happens that makes those div actually look and behave like the site you're visiting. But it's a dark art to getting it right and authoring, that comes with experience, not necessarily the good kind. It takes years of learning the arcane quirks like "oh you need to wrap that in a div with float:left" or "oh Firefox need's explicit widths on those elements". Compared to most of the Desktop UI frameworks I've worked with, traditional web UI development feels .... irrational.

There is hope though, I think things are getting better. HTML 5 has enabled the tools needed to make vast improvements. A lot of HTML 5 is not implemented yet, and even more is still experimental or bleeding edge. Web Components offers a path to maybe one day doing away with the div soup and making a more satisfying meal. I have been experimenting with Google Polymer lately, and it's still very new, but has me excited that I might soon be able to build a browser app with markup that makes sense for a UI.

Though I never used Silverlight much, I've used it's big sibling, WPF, extensively. It had its warts for sure, but creating a UI was so much more intuitive than the div soups/css hacks of HTML we see today. It makes me sad that Silverlight, met an untimely end. I feel it's demise was more due to a shift to mobile devices and away from browser plugins, than its technical merits. There is a project that I ran across in my feeds recently, Fayde, which is a re-imagination of Silverlight in pure Javascript. I was able to get a basic UI rendering in minutes, despite never using it before. It is super-neat-o.

For those crufty programmers such as  myself, with a slight allergy to new-fangled dynamic languages, there are a lot off tools to let you build Javascript applications in the language of your choice. Tools like Google Web Toolkit(Java), Emscripten(C/C++), Typescript, Dart, JSIL(C#/VB) all let you shield yourself a little bit from the idiosyncrasies of Javascript and the very loosely-typed nature of it.  It used to be that "strongly typed" was generally considered a virtue in a programming language, it seems like the rise of browser based development has flipped the consensus. I predict it will come full circle once really large "SPA" web or nodejs application developers start feeling the pain of maintaining multi-million line, aged, legacy applications that were built on gentleman's agreements.


 [Note, I struggled to find the right title for this post, no offense to millennial engineers is meant by it. I think if you learned how to program before Google was a thing, you cut your teeth under a different set of common wisdoms than today. Some are now obsolete but others got lost to the noise of the new shiny. The merit of strong explicit contracts seems like one of those lost wisdoms these days. I'm always keeping up with the new shiny, while trying not to loose touch with my roots. Millenials are crazy-smart and talented. Now get off my lawn!]