Javascript and the brain: Why Javascript is the future of programming languages
I discover myself all the time comparing programming languages to brain functions.
While I have never designed a programming language myself, I have used many of them, and think, that I know their strengths and weaknesses quite well by now.
But more importantly, I have studied brain functions and neural sciences to some extend over the years (especially starting with reading von Neumann’s and Turings books)
It is not a case that von Neumann, was also one of the founders of the computer paradigm. After all he just invented computers after the human prototype he was studying.
Over the decades however programming languages diverged from his principles and I always found myself reaching a dead end after some while with modern programming languages like Ruby, Java, .Net. etc. Strangely at first contact with Javascript over 15 years ago, I suddenly had this “feels right” feeling, but couldn’t describe why. Today, after some more years and some more experience with computer languages, I finally understand why Javascript not only feels right to me, but objectively is the right language for building future applications.
Let me explain why:
Preamble: I must explain some basics of how basic brain functionality in order to explain my conclusions:
- The brain is a neural network
- Neural networks are connections of neurons
- Neurons are cells
So what’s the brain for?
The brain is a world simulator. It’s a big approximator. It takes input from it’s senors (the eyes, etc.) and produces output (which in someway should reproduce what’s happening or will happen in the real world)
How does the brain do that? It does it by a process called “learning”, which was studied quite extensively by von Neumann, reaching also very surprising conclusions about the learning process itsself. All that neural networks do, is they modify their connection weightings all time, until the output produced by the network is coherent to what the sensor input says. It’s some sort of “Calibration” that takes place during learning. When you learn things right, chances are you will make the right conclusions, before things happen, and you probably will survive. Yes, you see that car rushing at you with 100 km/h. Well your brain should be able to precalculate, that if it continous on its trail like that it will just hit you in the next minute. Therefore it’s better to jump in the bush before. Your brain might even be able to precalculate, that the driver might be drunk or that he will see you in the next 20 seconds and therefore tell you to wait a moment to not make too fast or early conclusions. Your brain is fantastic!
Ok. Probably you knew this already.
What does this have to do with programming languages?
Well. If you think about the above, there is a powerful conclusion you can make, which is:
The brain is a (sophisticated) computer! It’s a big processor. But most importantly and this is what computer languages forgot over the years. It’s a parallel processor! That’s what most programming languages are not. They just took some of von Neumann’s paradigms, but forgot about the most important ones. Parallelism and self modification. Aehm, self modification? Yes, the brain modifies it’s functions by learning. Neural networks function, in that electronic impulses coming from the human sensors, run through it parallely. It’s evented non-blocking IO. Yes! I hope now you understand the hype about node.js. Node is an evented non-blocking IO framework written in Javascript.
Ok so let’s see why Javascript fits in perfectly in imitating the brain.
a) No types: types are stupid. The brain has no notion of a type. A neuron is a neuron. A type is an artifical creation of computer scientists, in order to distinguish, what an object can do. It turns out, that object are actually all the same. They are objects in OO programming languages. The type comes over their metadata and the objects they are connected with in memory. In fact one of the big “revolution” in OOP languages was to move away from basic types like int, float, etc. to a generic object oriented model. However still some languages use types to distinguish such basic objects. It’s not necessary. The compiler can find out by itself over heuristics. That’s what Javascripts tracing JIT does! And as we see in the last years with Tracemonkey, Spidermonkey and Google V8 or Crankshaft it starts doing it really well and fast!
b) Dynamic: Javascript is a dynamic language. This means that code can modify code during runtime. This was something like the devil for programming language designers over the last decades. They thought it was evil to do that and would lead to code, that wasn’t clear anymore and so they tried to prevent such use in their languages. Well it turns out that they were wrong. Self modifying code will be the future, and while we are not there yet, I think it will be the 4th and last revolution in software design in this century.
c) Functions in Javascript are first class citizens: Well to explain this I have again to make reference to brain functions and “brain functions”.
What are equivalent to functions in the brain? Well the brain has no distinction between functions and memory. Everything is taking up memory in the neural network. Functions and data. But Functions are like a set of neurons, with a specific scope. That’s data (signals) runs through them to produce some output data (other signals). In fact the way this works in Javascript is completely the same. Functions are just again objects in memory and thus can group functionality. Not only! They will also map to process memory perfectly.
The objects that run through such functions are electric impulse codificated objects coming directly from our sensors.
In computers sensors quantize these information to 0-1 bit streams.
So a binary diff function somewhat operates however on the same memory and therefore is a first class citizen. That’s why javascript is the future of languages, cause it treats functions as first class citizens, whereas other languages don’t.
d) Javascript scope and closures: Javascripts scope keeps your working environment and variable in place, also after you come back from some asynchronous function. This is important and is like the brain is connected to neurons it uses more often in a more “near” or “local scope” fashion.
e) IPC communication: well todo communication we need to marshal data in a protocol. The way this is done nowadays is mainly http. And parsing text on both sides. Javascript is a perfect fit for this with it’s human readable JSON object notation.
f) Push and not pull, and Javascripts event model: The brain works by emitting electrical signals to connected neurons. The brain doesn’t “call” functions it always “emits” events. In computer science it is well known by now that passing objects to functions leads to tight coupling, which makes code less maintainable over time. Javascript although it is not perfect has a quite simple notation for emitting events. Whilst other languages can do that too over patterns like inheritance, delegates and interfaces or notification centers. Javascript’s implementation is more powerful cause it uses simple function pointers, which are more leightweight, than passing around objects.
g) Inheritance, Annotations and Mixins: The brain has no notion of inhertitance. It’s an interconnected graph so there is no hierarchy. While other languages try to create object hierarchies through inheritance, Javascript doesn’t. Instead you can modify or extend every existing object or just “connect” to it over a pointer. That’s why Javascript immitates better the connection between neurons, than any other language I know.
This article was really fun writing. I think creating the brain analogy was a good idea. Comparing programming language features to brain functions, somewhat works quite well, so this means that there is a strong correlation between the two.
Have another brain analogy. Post me a line.