Consider removing soop
Hi,
First of, congratulations on a great library. I'm following both the development on this and bitcoinjs and they both look promising.
One thing that struck me when digging thru the source code is the use of soop on every class. I do think that it has some drawbacks and I don't really see the benefit.
The biggest drawback I believe is code complexity. Soop is not a known standard or even an implementation of an agreed upon way on how to do classes in javascript. As someone new to this code it took me some extra time to figure it all out, and it still feels awkward to use.
Inheritance is done by setting the nonstandard attribute parent on a class. The prototype then gets its __proto__ modified later when module.exports = require('soop')(Address) is called. Modifying __proto__ is deprecated (MDN proto) and is discourage even by the creator of javascript (Comment 6121). (More reading: proto alternatives, MDN Inheritance Revisited) Also, the standard js way to do this is also a one liner: Address.prototype = Object.create(parent.prototype).
Calling functions on parent is done with the super(receiver, method, args). The first thing that struck me was, why do I have to specify a receiver? In our codebase it's only called with this and I can't imagine why you would want something else there. It also feels odd to specify the method as a string and the arguments as an array. I think it feels more natural to do: parent.isValid.call(this, arg1, arg2)
One of soop's main feature is the ability to override variables used in the code. This is not something that is currently used, and I don't think that it's something that will benefit this project. I feel that the nature of this library makes testing very easy without mocking parts of the library, and I fell that this stance is supported by the fact that we have really good tests now without using it.
I hope that I'm not coming across as a douce that want to change the entire project, but I really feel like this could lower the barrier for new pull requests and also clean up the code base, making it easier to read.
I would be happy to do all the legwork on this and submit a pull request, but I wanted to know what you think about this before I started.
Agreed.
After working with bitcore and soop for a couple of months I must say I agree. Soop is interesting conceptually but it has proven to be a hassle more often than not (Especially regarding browserifying projects using soop). I agree with removing it from bitcore. ping @gasteve
Huge thumbs up.
+1
regarding super(), you need both the receive and the parent in order to know where method lookup starts …otherwise you would end up in an endless loop… but the more important reason for soop is to control bindings to the external world (how a body of code it wired up to it's environment)…this draws inspiration from a language by Gilad Bracha called newspeak…but it is essential for an environment to employ a capability based approach to security. I actually wish "imports" was standardized in nodejs much like "exports" is standardized (it could be with very modest improvements to the module system). One of the things soop allows you to do is to reload code, but wired up to its environment in a different manner (different bindings for console, or Math, or whatever you wish). There are a lot of cases where this comes in handy when building memory efficient code while still maximizing code reuse. Instead of just caching fully instantiated code with require(), it could also cache the compiled code and allow new instances of that code with different environmental bindings to be materialized.
Soop does not let you reload code for the browser, it has to be hacked. For the few cases where this would be handy (which never occurs in bitcore), we have to write all code in a non-idiomatic way, which is confusing to beginners, and an annoying extra hurdle even for experienced bitcore developers. It is especially annoying when soop doesn't load your file correctly (which almost always happens in the browser) and you have to manually override in the build script.
This the same conversation we had about classtool many months ago. The interface of soop is an improvement to classtool, but does not solve the problem. It is non-idiomatic, and does not provide sufficient benefits to be worth the hassle.
Proxyquire solves the same problem soop does (allowing you to override bindings in node), but does not require that programmers write their code in a non-idiomatic way. If you want to override bindings in bitcore, you can use proxyquire. It also has a version for browserify, so browser support is natural and does not require hacks.
Proxyquire: https://github.com/thlorenz/proxyquire Proxyquireify: https://github.com/thlorenz/proxyquireify Example of a hack necessary to use soop in bitcore: https://github.com/bitpay/bitcore/blob/master/browser/build.js#L14 Example of the hacks in Copay necessary to use soop: https://github.com/bitpay/copay/blob/master/util/build.js#L53
working on this