Recreating Python's Slice Syntax in JavaScript Using ES6 Proxies
intoli.com
intoli.com
Actual syntactic suger can make a program more readable, easier to debug and even more efficient. Those kind of hacks are neither.
It's not more readable because your expressions are limited to the original grammar, so you have to resort to weird compromises. It's harder to debug because you add layers of abstraction (like the proxies here) that have no purpose at all except changing how your code looks. And it's less efficient because those layers of abstraction will still stay with the program when it's being executed even though you only needed them while you were writing the code.
So, please, syntactic sugar is awesome and more thought should be given to letting users add their own sugars - but can we do it via metaprogramming and preprocessors and not by hacks like this?
(Disclaimer: This pattern annoys me to no end in Java, so it's gotten a bit of a pet peeve. No fault to the author of this post who has written an excellent article)
Just throwing the baby with the water is never a solution. As usual there is a balance to be found.
I'm more hoping that user-defined syntactic sugars and DSLs could be a focus point in future language design - so you actually get compile time tools and debugging support for them and don't have to resort to runtime or modeling hacks to implement them.
You can see the source for the javascript client that does the monitoring with proxies here: https://github.com/westoncb/JS-Watcher/blob/master/watcher.j... —it just works for arrays at the moment, but the basic idea is there. (I've written another much more complete client that does monitoring in Java; this JS one is more experimental.)
The reason for this is to give me logging for this part of the application to aid in extracting it out to a separate service. That is, to see what from this part of the application is actually being used/called.
Edit: I meant to say that I wanted to share this because previously I hadn't yet found an interesting use for a Proxy.
Also, I thought of another use case: in the get method you could return a percentage of the time a different object, i.e. an A/B test.
It's called @adrianhelvik/mock
I wonder if it makes more sense to write a babel plugin to support the python syntax. Since everyone using es6 features are transpiling anyway.
Also note that Babel's parser isn't pluginizable (normal Babel plugins just run during the transform step), so the typical way to add syntax is to fork the parser and run everything against the forked parser:
https://babeljs.io/docs/en/next/babel-parser.html#will-the-b...
There's an active Babel feature request to implement exactly that proposal: https://github.com/babel/proposals/issues/41. As such, it'd definitely make a lot of sense to implement this!
The result is that all property names passed to the interceptor get converting to a string.
That means if for example you were intending to proxy an array, your performance is destroyed.
First a numeric index gets converted to a string, then when to pass that on to your target array it has to be parsed back into a number.
In a trivial case (literally just forwarding the access) it might be possible to fake the stringing of the property name, but if you actually end up doing real work it becomes increasingly hard to avoid the property name escaping and so having to be reified.
I mentioned in the article that Remote Browser is an interesting project to look at to see some more advanced proxy usage. One of the things that it does is to define a CallableProxy class [1] which facilitates attaching a call handler to objects that otherwise wouldn't support them. The code itself isn't complicated at all, but it takes a bit of thought to make the pieces fit together just right.
- [1] - https://github.com/intoli/remote-browser/blob/9ffdd1b5b0a9b4...
I'm thinking to replace that global object with recursive proxies so that all updates can be forwarded to ngrx store
And slowly replace all reads with ngrx selectors
Tongue in cheek but still, webpack, babel, react, jquery, coffeescript, lowdash and typescript are all attempts to make the pain of using the js ecosystem go away by patching in things that has existed for decades elsewhere at the price of huge infrastructure and organisational cost.
That such a terrible tech remains with a monopoly on the most popular and awesome platform in the world still astonishes me to this day. And humbles me in a way.
But what infuriates me is that a lot of js devs will actually claim that it's great. The stockholm syndrom is strong indeed.
[1] - https://github.com/intoli/slice#for-people-who-know-python-a...
Personally I’ve come to appreciate it (and even prefer it).
Anyway, it says to use two spaces. It got super popular and here we are now.
CoffeeScript was a big catalyst, and I think Rails was as well, but it probably would have happened anyway. Of the languages I use regularly, the only ones for which 2 spaces is not accepted as normal are Python (which uses 4, probably because it was set in stone decades ago, before people even knew how to write code) and Go (whose auto-format uses tab characters, probably because Rob Pike is a stubborn prick).
Really, 4 spaces is an awful lot, unless your eyesight is failing.
That is, if a given language is really verbose and requires a lot of code to do simple things, the functions, if-statements, loops, etc will probably be a lot longer (like in C or Go) and so you need wider tabs to help visually align things.
But if a language encourages a style where functions are just a couple of lines long, if statements and loops are rare (or at least comparatively short), having excessively wide indentation can hinder readability.