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
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
a.unwrap_or(b)
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 toa
ifa
is “truthy” elseb
. “Falsy” is empty strings/containers,0
andNone
.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.