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
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.
(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).
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.
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.
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.
I hate this so much. Literally stopped using Perl and switched to PHP to get away from the "Look, ma! I can condense 6 comprehensible lines to one complete gibberish line that still works!" crowd.
I'm not saying I won't use shorthand if/else format on very rare occasions where you have to do a bunch of different if else's within your HTML for some reason, but in general, I try to avoid it.
Except there's literally no change in performance as a normal compiler will treat those the same. It just looks nice and trim down the time an experienced dev reads and understands the code by around 200ms.