top 50 comments

sorted by: hot top controversial new old
[–] 83 points 2 years ago (19 children)

People ITT hating on null coalescing operators need to touch grass. Null coalescing and null conditional (string?.Trim()) are immensely useful and quite readable. One only has to be remotely conscious of edge cases where they can impair readability, which is true of every syntax feature

  • source
  • hideshow 19 child comments
  • load more comments (19 replies)
    [–] 66 points 2 years ago (5 children)

    My coworker flips his shit every time I include a ternary operator in a PR. He also insists on refactoring any block of code longer than two lines into its own function, even when it's only used once.

    He is not well liked.

  • source
  • hideshow 5 child comments
  • [–] 36 points 2 years ago (2 children)

    He also insists on refactoring any block of code longer than two lines into its own function

    Thanks, uncle Bob.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 18 points 2 years ago*

    His advice is great for newer programmers. They are taken literally by newer programmers, but the goal is not to force the dogma onto everyone. Maybe that should be more clear before the new people make a fool of themselves. They'll learn why or how to apply these rules once they get more experience.

    I know the episode you're referring to and the important part is to realize you can use functions names/signatures to convey and structure information about your code, not just as a way to reuse code. This is often misunderstood by newer programmers, self-taught programmers. Your code should be easy to understand so it's up to us to make it well structured. Functions aren't only to avoid duplicate code

  • source
  • parent
  • load more comments (1 reply)
  • load more comments (1 reply)
    [–] 54 points 2 years ago (18 children)

    And no one on his team ever understood his code.

    Sometimes being declarative is better than being "smart"

  • source
  • hideshow 18 child comments
  • [–] 49 points 2 years ago (2 children)

    The last panel is infinitely more readable than parsing the whole chunk of logic above. Maybe you're just not used to this language's (I think this meme used C#) null operators.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 34 points 2 years ago

    Sure, if the rest of the team is first semester CS students doing their first group project. This is not an obscure 1337 h4x0r trick only known to programming gods writing COBOL code inside banking mainframes, it's a simple operator.

  • source
  • parent
  • load more comments (6 replies)
    [–] 49 points 2 years ago* (last edited 2 years ago) (14 children)

    Ruby:

    a || b

    (no return as last line is returned implicitly, no semicolon)

    EDIT: As pointed out in the comments, this is not strictly equivalent, as it will return b if a is false as well as if it's nil (these are the only two falsy values in Ruby).

  • source
  • hideshow 14 child comments
  • load more comments (5 replies)
    [–] 33 points 2 years ago (4 children)

    I enjoy this:

    return a.or(b);
    

    But yeah, that requires an Option type rather than null pointers...

  • source
  • hideshow 4 child comments
  • load more comments (4 replies)
    [–] 28 points 2 years ago (2 children)
  • load more comments (1 reply)
    [–] 24 points 2 years ago (12 children)

    There's a nice list of this feature by language on the Wikipedia page for anyone interested: https://en.wikipedia.org/wiki/Null_coalescing_operator#Examples_by_languages

  • source
  • hideshow 12 child comments
  • [–] 14 points 2 years ago (11 children)

    This was my first time actually seeing a Rust example, and I hate it.

  • source
  • parent
  • hideshow 11 child comments
  • [–] 12 points 2 years ago (2 children)

    Other languages: if a is null return b.

    Rust: here is an array of strings, we are going to parse the array to numbers. If that conversion fails we handle the exception and return the minimum integer value. We then save the result in a new vector. We also print it.

    I like rust, but I hate the example too. It's needlessly complex. Should have just been a.unwrap_or(b).

  • source
  • parent
  • hideshow 2 child comments
  • load more comments (2 replies)
  • load more comments (2 replies)
  • [–] 18 points 2 years ago* (2 children)

    (or a b)

    i never thought of lisp as concise before

  • source
  • hideshow 2 child comments
  • load more comments (2 replies)
    [–] 17 points 2 years ago (7 children)

    I’m learning swift and I actually just discovered ?? today. Am I missing out in other languages?

  • source
  • hideshow 7 child comments
  • load more comments (2 replies)
    [–] 17 points 2 years ago (3 children)

    Loads of beginners in this thread. Here's how it's done in the industry.

    The code:

    return a;

    The test:

    a = rand()%100+1;

    It works, boss.

  • source
  • hideshow 3 child comments
  • load more comments (3 replies)
    [–] 15 points 2 years ago (3 children)

    For the love of god, please do not use single-line alternatives to braced scopes. It's only tolerable in lambdas like Array.map( v => v**2 ). It's not for an implicit scope. It's for evaluating a return value.

    But holy shit is it nice to have ?. in Javascript. It's such an anything-goes language until you look at it funny and it just shits the bed and silently dies. Hate hate haaate having to check if things exist, when so many other details fail politely and impact nothing. At least now it's one extra character instead of try-catch rigmarole.

  • source
  • hideshow 3 child comments
  • [–] 9 points 2 years ago (1 child)

    I'm fine with non-braced blocks, but they must always be on the same line as the parent statement (e.g. if (a != null) return a) to visually distinguish them. (inb4 argument about long conditions: they'd usually be spread out over several lines, and the statement would go on the closing parenthese (which is on a line by itself when split over multiple lines))

  • source
  • parent
  • hideshow 1 child comment
  • load more comments (1 reply)
  • load more comments (1 reply)
    [–] 14 points 2 years ago

    Nullish coalescing makes a lot of stuff much easier to read and write.

  • source
  • [+] 13 points 2 years ago (1 child)
    load more comments (1 reply)
    [–] 12 points 2 years ago* (last edited 2 years ago) (5 children)

    return a and a or b → return a or b

    correction from @murtaza64

  • source
  • hideshow 5 child comments
  • load more comments (5 replies)
    [–] 12 points 2 years ago (8 children)

    Please don't use #2. It is how you get the goto fail bug

  • source
  • hideshow 8 child comments
  • load more comments (6 replies)
    [–] 12 points 2 years ago* (last edited 2 years ago) (5 children)

    Python, checking in ...

    return (a or b)
    

    Parentheses aren't necessary, I just prefer them for readability.

    See python documentation on boolean operators for reference. Short story is a or b is an expression that evaluates to a if a is "truthy" else b. "Falsy" is empty strings/containers, 0 and None.

  • source
  • hideshow 5 child comments
  • load more comments (2 replies)
    [–] 10 points 2 years ago (5 children)

    Yea uh is this actually equivalent? In all of those other cases you're checking if a is null and in the last case my understanding is it is checking to see if a is falsely. In the case that a is 0, or undefined, or an empty array or any other kind of non null falsey value, then the behavior would be different.

  • source
  • hideshow 5 child comments
  • load more comments
    view more: next ›