Live data from Hacker News

Show HN: Nothing.js – A chainable mock object which always returns itself

github.com

21–30 of 43 posts

Re: Show HN: Nothing.js – A chainable mock object which always returns itself

#21
One thing I've been finding increasingly while using Flow in javascript is, null-pointer exceptions aren't really much of a problem any more.

Where they might have caused a bug, 90% of the time Flow will catch them, and insist that I do a null check and handle that case.

And when they do crop up, it's usually more of a serious failure that I'm happy to trigger an exception early, rather than having it propagate further down the stack by using something like nothing.js. Especially given the list of gotchas. The fact that `Nothing` is not falsy is really problematic.

I'm pretty convinced at this point that `a && a.b && a.b.c && a.b.c.d` is an anti-pattern in javascript anyway; if I'm that unsure about the structure of my data, I have more serious problems than just trying to avoid null pointers.

Re: Show HN: Nothing.js – A chainable mock object which always returns itself

#22
post #19

Earlier quoted context omitted.

It's also hell to debug as it silently always works and propagate. It's good for unit testing though.

That’s true, but ostensibly you only use it in places where you expect nulls and don’t intend to debug anything. That’s why Smalltalk, C#, CoffeeScript, and probably JS and TS soon have null chaining.

Objective-C also allows nil to receive messages which then just returns nil also. I’ve always found this really natural in that you can assume something is either valid or null and avoid a whole lot of null checks this way. That said, I’ve never been terribly frustrated by other languages either which throw when calling methods on null. Maybe it’s because of heavy use of certain design patterns that make it so it’s never really a surprise?

Re: Show HN: Nothing.js – A chainable mock object which always returns itself

#23
I think this kind of access pattern is typically a code smell. Instead of carrying around arbitrarily nested values you should try to normalize it into a known and consistent shape when crossing serialization boundaries.

Proxy pays a big performance penalty, and I doubt it'll be improving any time soon. In addition, I believe it's not possible to polyfill Proxy. An alternative could be to just wrap the deep property access in a try/catch:

    function getQux (input) {
      try {
        return input.foo.bar.baz.qux()
      } catch (e) {
        return null
      }
    }

Re: Show HN: Nothing.js – A chainable mock object which always returns itself

#24
Looks elegant, but am not going to sunset the homebaked library solution yet I don't think, because of the proxy and polyfill overhead, and also because i prefer to freely assign a specific value if nothing:

TS-like syntax for clarity:

  Function Nothing(vTestVar:any,  vIfnull:any, sObjPath?: string, bStrict?: boolean) {... return vResultOfCheck}

Re: Show HN: Nothing.js – A chainable mock object which always returns itself

#25
post #17

This is a really neat idea, and is a lot more ergonomic than nulls or optionals in JS. Until optional chaining makes its way into the spec [0], this is the cleanest way I’ve seen to operate on maybe-null values. [0] https://github.com/tc39/proposal-optional-chaining

Not a new idea, though. It’s basically the venerable Null Object pattern as documented by the Gang of Four book.

Re: Show HN: Nothing.js – A chainable mock object which always returns itself

#27
If you use Babel 7, you can install a plugin [0] to get the optional chaining syntax [1] that's currently a TC39 Stage 1 proposal!

[0] https://www.npmjs.com/package/babel-plugin-transform-optiona...

[1] https://github.com/tc39/proposal-optional-chaining

Re: Show HN: Nothing.js – A chainable mock object which always returns itself

#29
post #27

If you use Babel 7, you can install a plugin [0] to get the optional chaining syntax [1] that's currently a TC39 Stage 1 proposal! [0] https://www.npmjs.com/package/babel-plugin-transform-optiona... [1] https://github.com/tc39/proposal-optional-chaining

Yes this is a much better alternative in my opinion.

Re: Show HN: Nothing.js – A chainable mock object which always returns itself

#30

I think this kind of access pattern is typically a code smell. Instead of carrying around arbitrarily nested values you should try to normalize it into a known and consistent shape when crossing serialization boundaries. Proxy pays a big performance penalty, and I doubt it'll be improving any time soon. In addition, I believe it's not possible to polyfill Proxy. An alternative could be to just wrap the deep property…

Wrapping you code into a try/catch block decreases performance even more, you don't want to do that. You might want to check it out: https://github.com/GoogleChrome/proxy-polyfill
Post reply on HN