Also accept a string as an argument#11
Conversation
|
Any feedback on this one? |
|
@wilmoore, @theotherzach - would you mind taking a look at this? |
|
I would argue against this test. It forces people to create an additional check in the match method for minimal gain. |
|
OK, this bugged me for the first time right now:
This is how all the tests are setup so it's not specific to this pull or JavaScript. I find it interesting that we have a separate concept for the class and an instance of the class. An |
Yeah, it's the same in Ruby right now. I've gone back and forth on this. In the Ruby test suites I'm tempted to make fewer and fewer choices. Like, perhaps |
Probably a good idea because I'd like to refactor this, but I'd be happy to see someone else take a crack at refactoring it as well. The extra API sugar is just a nice incentive to get the refactoring going which I am OK with. |
|
As a start; we can probably drop the constructor. |
|
Probably something like this (but probably should break out the string transforms into a function): |
|
Test would look something like: |
Many real world libraries and frameworks are flexible about the arguments in their functions which makes using them convenient in different scenarios. I would also add a version that accepts a variable number of arguments, e.g.
Why objections against the constructor? The language has this feature, and while you're not required to use it, JavaScript does allow one to do classic OOP. |
They have their pros and cons (just like everything else); however, I try to only use them when they are absolutely necessary. The object graph (i.e. prototype chain) becomes much more complex when you introduce a constructor; and in a lot of cases, they aren't needed. If the intention is to always mimic the class-based oo style, then obviously using the constructor is the way to go :) |
|
A great article to read in general but also very relevant to this discussion: |
Hardly :) |
|
Merged in #13. Thanks! |
|
@wilmoore thanks for your comments and the article link, that makes sense! |
|
@dpashkevich: no problem at all 👍 |
|
This is super awkward to do in JavaScript. It also happens to be slow (relatively speaking, of course). It feels like its likely to make the developer think, "Oh, yeah. I gotta do that slice thing with arguments" or, "What? I guess I'll google this." It's cryptic, often-cargo-culted code. |
Please expound. We have no way of knowing what your definition of awkward is :)
Perhaps...but, then again, what do you mean by slow?
Just trying to get to the bottom of your definition of slow and if it is a necessary optimization or not. Keep in mind, without any parameters around requirements and no discussion of trade-offs, I'm not sure what to do with your claim. If you can provide some further detail, we can begin to have a discussion about it. |
Let's look at the code:
Additionally, the code doesn't pass the smell test. If you are an experienced programmer from another language there's very little chance you'd grok without some serious "WTF?" and googling. It's one of the many sharp edges of JavaScript.
The MDN Documentation offers this -- laid out in a bright red box so as to get your attention:
One could use a Due to these issues, I'd argue that this isn't good API design in JavaScript. You should take an array (or somethinng that responds to the array methods you require (likely |
I agree, it sucks 👍 That being said, it is an idiomatic technique and it's what we've got to work with.
I agree; however, googling is simply part of the job. Even for an experienced programmer.
Even when things are written in a big red box, we still have to apply our own justifications and check trade-offs. Still, this warning comes with no benchmarks and a short article such as that will never go into trade-offs, etc. It's a warning...yes, you should consider it, but it does not mean you should never use it. Though, I do agree that
Depends on the situation. In this case, I'd argue that convenience outweighs the anecdotal WTF argument (pun intended). Anyway, it's better to have a little fun with this stuff. Let's not be so uptight about this sort of thing unless it is (1) causing major pain without question (2) simply does not work. Again, more fun, less serious 😄 |
|
If the above doesn't leave you satisfied for some reason, here is a little more food for thought if you really want to dig into this further:
|
|
👍 |
This would be a little extra exercise in API building