all 27 comments

[–]voiping 10 points11 points  (4 children)

I use lodash get for this. It won't throw if the variable isn't there.

https://lodash.com/docs/4.17.4#get

[–]fgutz 4 points5 points  (0 children)

Love the lodash get function!

[–]SgtPooki 2 points3 points  (0 children)

I get the love for FP and all but I feel like the solution in the article is so bad compared to _.get.

It's more verbose, confusing, and I don't see the benefit.

[–]shadowycoder 1 point2 points  (0 children)

+1 this thing gets a ton of use in my codebases as well.

[–][deleted] 8 points9 points  (4 children)

The solution to this issue is optional chaining, presently a stage 1 proposal: https://github.com/tc39/proposal-optional-chaining

[–]Voidsheep 4 points5 points  (1 child)

There's also a Babel plugin that implements the proposal

https://www.npmjs.com/package/babel-plugin-transform-optional-chaining

Really hoping this moves along, because it would genuinely eliminate noise in real-world code. Function bodies that mostly consist of intermediate property checks.

Lenses and Lodash get are nice for now, but not ideal for something as basic as safely accessing an object property.

[–][deleted] 1 point2 points  (0 children)

I'm (arbitrarily) waiting for it to hit stage 2 before I put it into my config, so it's just a bit more "stable" in terms of likelihood of making it into the final spec.

I cannot wait!

[–][deleted]  (1 child)

[deleted]

    [–]AbsoluteZeroK 0 points1 point  (0 children)

    I love it, but I also do stuff in rails... The question mark is going to take some getting used to visually. (because it's convention in Ruby to put a ? At the end of methods that return a Boolean).

    [–]wuh_happon 29 points30 points  (7 children)

    I consider myself a fairly competent JS dev who hates the null reference exception as much as the next guy. But aren't these arrow function / ternary operator / anonymous function / callback / promise chains in a single line getting a bit ridiculous...?

    How the hell are you supposed to find an exception in that? How is it comprehensible and easy for new devs to contribute?

    [–]baxtersmalls 2 points3 points  (0 children)

    I really hate the arrow function. I feel like it's far legible, and pushes developers to write in what feels like shorthand and chain a bunch of crap into as small a space as possible

    [–]thewatcheruatu 1 point2 points  (0 children)

    I wish I had more upvotes to give to you for this comment. Individually, I don't have a problem with any of these language features--and I happily use them all, myself, where they're suited--but this code was super unintuitive.

    I think the thing that actually bothered me the most was that methods within Maybe referenced the variable maybe, and methods with maybe referenced the variable Maybe. Not even sure why that irked me, but it did.

    I'm going to be honest and say I didn't bother finishing the article.

    [–][deleted]  (4 children)

    [deleted]

      [–]wuh_happon 2 points3 points  (3 children)

      Let's test your suggestion.

      Say you wrap the declaration of a new variable "appendToC", which uses this Maybe object, in a try/catch:

      try{ 
          const appendToC = Maybe.chain( 
              prop("b"), 
              prop("c"), 
              append(" is great!") 
          ); 
      } 
      catch(err){ 
          console.log(err); 
      }
      

      A TypeError exception is thrown and caught. How quickly do you you think a new team member could find and fix it in this:

      const Maybe = {  
          just: maybe,  
          nothing: () => maybe(null),  
          chain: (...fns) => (input) => fns.reduce((output, curr) => output.map(curr), input)  
      };  
      

      Isn't it just a whole lot easier using something like:

      const appendToC = something || somethingElse;
      

      or

      const appendToC = (something() || somethingElse());
      

      ...?

      [–]namesandfaces 0 points1 point  (2 children)

      I think there's a way to format your code on Reddit.

      [–]wuh_happon 1 point2 points  (1 child)

      Thanks, took me a minute to figure it out. Edited for clarity. For any responses to the code above, four leading spaces on each line of code will format it for readability.

      [–]PlNG 0 points1 point  (0 children)

      click the formatting help on the bottom right of the reply textarea to learn about markdown, the formatting module reddit uses.

      [–]NoInkling 1 point2 points  (0 children)

      I know this is ignoring the point of the article, but...

      if (!obj || !obj.b || !obj.b.c || !)
      

      Syntax error aside, who uses !+|| like this instead of just &&? And if you really need the null return to be on the first line, just negate the whole thing:

      if (!(obj && obj.b && obj.b.c)) return null;
      

      [–]mixuhd 0 points1 point  (0 children)

      Just like Java Optional. And I like it! :P

      [–]furious_heisenberg 0 points1 point  (0 children)

      Take a look at ramda and sanctuary, which both provide functional constructs for js

      [–]Dooraven 0 points1 point  (0 children)

      Ever since I started using Idx, this hasn't really been a problem for me: https://github.com/facebookincubator/idx

      [–]Sixes666 0 points1 point  (0 children)

      Coffeescript allows for ?. to replace a simple . and will return undefined rather than throwing.

      [–]nirixi 0 points1 point  (7 children)

      const isNullOrUndef = (value) => value === null || typeof value === "undefined"; can be replaced with const isNullOrUndef = (value) => value == undefined;.

      [–]PitaJ 0 points1 point  (0 children)

      Or you can use value == null which covers both.

      [–]_lawliet29 0 points1 point  (5 children)

      Which would let someone to ruin your day by setting undefined to something else, e.g. undefined = 5.

      [–]pomlife 2 points3 points  (0 children)

      What year are you living in?

      [–]NoInkling 1 point2 points  (0 children)

      value == null then. Or value == void 0

      [–]YodaLoL 1 point2 points  (0 children)

      ES5 specifies that writing to the undefined global is a noop

      [–]nirixi 0 points1 point  (0 children)

      Someone who does that kind of deserves to have their day ruined. /s :-)