Wednesday, October 26, 2016

JavaScript for Pythonistas

When serving somewhat as Guido's sidekick, for a Shuttleworth Foundation pow wow, I was fortunate to meet with Alan Kay, of Smalltalk fame.

Alan had been having back problems or something that kept him bed ridden for long periods and he'd used the time to tackle JavaScript.  He was pleasantly surprised.  He had some honest admiration for the language, as he did for Python.

He'd implemented turtle graphics as a way of building muscle knowledge.

I've learned more since then, about how LiveScript from Netscape was coming straight from Scheme, or in any case a functional programming background.  That the keyword "function" serves as lambda is clear from the start.

An object is like an instance __dict__ in Python, with methods even at the instance level, where classes never store them.

In JavaScript, a first order of business is to populate some prototype with shared methods, while continuing to isolate instance state.

If you're a Pythonista, you take for granted that the __dict__ of your type holds the methods, be these instance, class or static, while each instance has its __dict__ as well, for state, perhaps immutable state if any changes must go to some __new__ version.

That's more immutability than most OO programmers want.  Keep being the same self, just change your driver's license and bank account numbers from time to time.

That being said, immutability is a source of sanity in code.  I would never scale a vector, make it three times longer, without returning a new vector.  If your code wants to recycle the name, and keep v always pointing to "the same" object in that sense, fine.

Naming conventions signal continuity of identity if that's the best way to think about it.  The underlying object is free to return a new vector instance upon scaling or rotation, keeping vectors more like integers and strings, both immutable object types in JavaScript and Python.

The Pythonista community gives the JavaScript natives that useful second pair of eyes on OO, without requiring as much real estate as Java or C++, both of which are career paths in themselves.

Python is no toy language, just it rewards even a little study more quickly, so the JavaScript user stays busy and productive, while the Python implementation of OO concepts, clarifies the advantages such strategies might bring.

Scientists and those in need of analytic powers more generally appreciate Python for similar reasons.  "Python fits your brain" is the slogan.  For many, that's true.  The language works as expected, once you get the hang of it.  Eric Raymond (The Cathedral and the Bazaar) had found that true for him as well.

Since people in many walks of life are eager to get on with it, whatever "it" is, and not detour too deeply into some side track in computer science, it's gratifying to meet a language that meets them more than half way.

JavaScript programmers will grow into ES2015 / ES6 and ES7 more gracefully perhaps, as they more than double their overall design pattern savvy, in learning from the Pythonista perspective.