4 ms·
No. Stop over-engineering. YAGNI! Most JS codebases are light on polymorphism. I think I've used one UI framework that had any kind of inheritance at all. I'v
by davedx 12y ago
No.
Stop over-engineering. YAGNI!
Most JS codebases are light on polymorphism. I think I've used one UI framework that had any kind of inheritance at all. I've never (ever) had a bug caused by "forgetting to type new".
Use strict. Lint your code. Use a decent build toolchain. Stop over-engineering your code unless you have a damn good justification for it.
- couchand 12y agoAvoiding the use of 'new' isn't overengineering, it's idiomatic JS.
- davedx 12y agoIt's dogmatic not idiomatic.
- couchand 12y agoJavaScript code rarely uses inheritance because it's completely out of place. Most non-trivial JS apps I've worked on make heavy use of duck-typing polymorphism. It works great because you don't need to deal with arcane class hierarchies: just make sure the object has what it needs to get its own job done. Using a constructor in the first place in a language that really doesn't need them is what's dogmatic.
- BinaryIdiot 12y ago> It's dogmatic not idiomatic. That's not accurate; there are actual, technical reasons for avoiding new hence why some advise against using it.
- phpnode 12y agoThere are equally valid technical reasons for using `new`, so it's dogma
- BinaryIdiot 12y ago> There are equally valid technical reasons for using `new`, so it's dogma I really don't want to get into this argument but that's not what dogma means. Dogma is something that comes from an authority as being undeniable but saying you can't or shouldn't use new isn't undeniable, it's deniable as you said yourself there are valid reasons for using new. and now I hate myself. Thanks.
- phpnode 12y agodamn, I've been using that word slightly incorrectly for years, and now I hate myself. Thanks.
- BinaryIdiot 12y agoWhoa a pleasant exchange on the internet. Yay.
- davedx 12y agoWhat are the technical reasons to avoid using new?
- yummyfajitas 12y agoHow is writing a function to build an object "over-engineering"? It doesn't seem more complicated, and it appears it can help mitigate a particular human error (forgetting new).
- wildpeaks 12y agoThe linter would catch the missing new
- Havvy 12y agoA linter to catch the bugs and then go back and fix them is more work than just making it not possible to have the bug exist. Also, the lint you are talking about is based on a typographic convention that you could easily get wrong. For instance, the linter won't catch the bug in this snippet: ``` var yourClass = require('your-class'); var myInstance = yourClass(); ```
- phpnode 12y agoIn your example you have two "bugs", you're not capitalizing the constructor name and you're not using `new`. I would have to make two separate mistakes to reproduce the issue you're talking about. The only way I'd make the first mistake is if I was unfamiliar with `your-class`'s API, so nothing can save me there. JSHint would catch the second one.
- clux 12y agoNitpick, on the nothing can save me there part. This is arguably a failure of `your-class`'s API. A file exposing a class SHOULD either wrap it: `module.exports = function () { return new YourClass(); }` or use `if (!(this instanceof YourClass)) return new YourClass()` guards in the constructor to stop users of a class having to worry about whether or not to use new. At least this seems to be the convention many module publishers seem to adhere to.
- 12y ago
- epidemian 12y agoOver-engineering sounds a bit of a stretch in this case. The article compares using a normal function that returns an object vs using a special function that needs to be called with different syntax and that magically returns `this`. The former doesn't seem more over-engineered to me, in fact is seems simpler than the later.