Follow-up: Why JavaScript and PHP rule the web

The Reddit discussion of my earlier post “Why JavaScript and PHP rule the web” had good comments and made me wonder a bit more about the subject.

There are two comments of mine that I think are worth enough to put up on the blog, even if it is for being bashed by the internet, as it adds more substance to my opinion.

“pinnr” reminded me that both languages has lot of flaws, and I agree. But why no substitute won so far? That’s my comment for it:

That’s a good point. PHP and JS are full of “gotchas”. It makes me wonder, why no other languages have replaced them already?

Maybe if a new language can be as permissive and flexible as JS and PHP are, use an easy to understand flow (both are basically an infinite interactive loop) and adds more “sanity” to it, then it could win. The problem could be that new languages tries to be saner and stricter at the same time, and the web doesn’t like the stricter part. Does it make sense?

From “aslate” comments, I believe some people thinks that WordPress plugin system is just an example of a well defined API, and has no relation to PHP itself, as it could be made in any other language. I do believe that almost any high level language can be used to build a plugin system, but not as flexible as PHP would (or other languages that uses the “Assumption Inversion Principle”). That’s my answer to it:

Types, imports and modules enforce too much for the web. That’s my point of view.

By using only assumptions in your code, you don’t care how those assumptions are met, you just believe they will be somehow. It is like Dependency Injection, but at a more granular level.

Writing a single piece of plugin code that works on multiple versions of WordPress, leveraging new functionality when available is as simple as checking if a function exists (inspecting the environment) and adapting.

Imagine that JS used modules from the very start. A lot of libraries would have require('jquery') to only ever use a minor subset of it. This is where “assumptions” shine, you only pay for the exact subset of assumptions you make, and your code believes they would be fulfilled somehow.

jQuery has alternatives, underscore.js has alternatives, Backbone.js has alternatives. If each module selected one that better fits its need and you end up using those modules, you would end up with a bunch of duplicated JS functionality in your final product.

That’s the main point: when you require/import/etc something. You are locking your code with a preset selection that you made for whatever reason, and you are forbidding others to change that. There are plenty of reasons that other developers would want to change that selections (e.g. add a cache layer, only use a subset for space constraint reasons, completely mock it out for testing, use a proxy pattern to delegate things for a remote service, …).

Coming up with an API that fits every need is completely impossible. IMHO it is better that each code use assumptions about its dependencies, the same way a JS or a PHP function use assumptions about its arguments.

So, what I’ve called as Assumption Inversion Principle may have a 1:1 relation with duck typing of variables, but taken to the module level.

I like how a single function in JavaScript can be used in any type of object, as long as it has some properties that are assumptions of the function itself. Don’t you?

If you are an advocate of duck typing, why not advocate a “duck typing of dependencies”? What is the fundamental differences that I am missing here?

Why JavaScript and PHP rule the web

JavaScript and PHP are excellent languages. It took some time for me to really understand their power, and I hope to clarify my point of view in this post.

I believe that those languages shine and survive for being permissive and by adopting an Assumption Inversion Principle (AIP) — I’ve made up that name, you may suggest a better one in the comments. 🙂

What is AIP?

Raw JavaScript does not have any module definition structure, no statements as “import” or “include”. Symbols available for a piece of code written in JavaScript are pure assumptions. Those assumptions must be satisfied by other pieces of code, and not by itself. It is incredibly easy to change behaviours by loading new script files before the one to be changed by changing the link of its assumptions. This “change by adding” relates directly with the “O” in the SOLID principles.

Old PHP code goes the same way, your piece of code has some assumptions that some symbols are available and you simply use it. It is not the responsibility of your piece of code to link those symbols, you normally end up putting all of your script loading in something like a bootstrap system.

Import statements are bad, because they hardwire the symbols in your piece of code. Then if you need to change something, you actually need to get into the file and change it. In my opinion, “change by changing” is more prone to errors than “change by adding”. Also, “change by changing” does not scale as well as “change by adding”.

JavaScript and PHP code can also inspect its environment and adapt. So it’s possible to keep adding files that keep changing stuff depending on what has already changed or what is currently available for the script. This is pure gold in software development.

Think about WordPress plugins, you don’t need touch a single line of the core code, you just drop a new file and you can get a whole new experience out of your web site. Some plugins add features, other plugins only change behaviours. So, it either “add by adding” or “change by adding.” It’s a win-win situation, core code is hardly changed to add a feature, so you won’t get any bug from features you don’t care. For the features that you care, you get the plugins, drop them in and live with the bugs, if you happen to find a scary security bug, just disable the plugin.

I know a lot of people who criticize this way of developing and would prefer that WordPress be more object-oriented than it currently is. For what? Then we would need a plethora of new abstractions to achieve the same stuff. As code is a liability, the less code we need to achieve something, the better.

I love Haskell and its type system, it’s phenomenal. But it suffers from the same problem as other languages like Java and C# suffer. Modules are hard-wired with their dependencies and code can’t inspect its environment. I’m not saying that those languages are bad and should not be used. They surely has their own good use cases.

For a fast and distributed development effort as the web is, we simply can’t afford anything that is unforgiving or strict. The code needs to be open for changes from the outside (change by adding).

So, for the web, I don’t like new PHP code that uses modules and neither the usage of ES6. They may feel faster for developers starting out new products, but my bet is that in the long run they end up killing productivity and/or quality.

Think about it for the current project you are working on, how much could you change of it by only adding new files? What could you deliver by adding a new file: a whole new story, a feature, a bug fix or nothing?

All this permissive environment with all these code based on assumptions can turn into a real mess. That’s the reason those languages are hated by some developers, then those developers try to fix it by creating new stricter languages for the web, the end of the story is always the same: JavaScript/PHP triumphs over all. Why? Is it because of AIP?

JavaScript and PHP has some hidden properties that newcomers tend to ignore. Those hidden properties make them a perfect fit for the web. What do you think?

Am I just an old-school guy talking bullshit? Please, be kind. 🙂