DEV Community

JavaScript Joel
JavaScript Joel

Posted on

A pragmatic solution to the flatten proposal problem #smooshgate #JavaScript

The Problem

A new TC39 proposal is in conflict with an older library, mootools.

The proposal would add flatten to ECMAScript, but would be incompatible with mootools, which has already added a flatten to Array.prototype. Adding Array.prototype.flatten would then break any existing sites that are still using mootools.

This is apparently not the first time mootools has been incompatible with TC39 proposals.

The TC39 has the difficult job of not only approving new language proposals, but also making sure those proposals are backwards compatible and do not break the web.

Not the solution

Another proposal has recommended renaming flatten to smoosh. I'm not sure if this is a tongue in cheek recommendation, but it has sparked a large debate now dubbed #smooshgate.

Break the web?

This would be a workaround as flatten is a better name than smoosh. And to rename this method for an 8+ year old library seems insane.

Adding fuel to this fire, there appears to be yet another proposal to prevent the use of smoosh by using an incompatible version of smoosh in production sites today, thus forcing the TC39 to not break compatibility with smoosh.

Backwards compatibility is important. I definitely don't want to go through old legacy sites that have been running just fine for years and update them simply because new features have been added to JavaScript. So, clobbering old libraries is not a good solution.

Though, researching every ancient library ever created for the web to ensure compatibility will also slow down progress and coming up with silly names also seems, well, silly.

A solution

I propose we learn some lessons from Ethereum and how they have already solved this exact problem in Solidity.

Solidity allows you choose which version of the Ethereum Virtual Machine (EVM) you want to run by adding a pragma tag to the top of the code file.

// run this code with EVM 0.4.0
pragma solidity ^0.4.0;

contract MyContract {
Enter fullscreen mode Exit fullscreen mode

It is clear to me that this simple solution is also the best long term solution for ECMAScript. This proposal will allow legacy sites to remain unbroken when new features come out. It will also allow the TC39 to progress than language faster and use the best proposals without fear of breaking the legacy web.

Sample Code:

'pragma ecmascript ^2018.0.0';

var arr1 = [1, 2, [3, 4]];
// [1, 2, 3, 4]
Enter fullscreen mode Exit fullscreen mode

We have already dome something similar with use strict. This would simply be the next evolution of use strict.

What is your opinion on this whole flatten, smoosh debacle? I would love to hear your thoughts in a comment below.


Top comments (0)