Conditional Assignment (null coalescing)
Thought I'd share one of my favorite language features (of any language that has it): conditional assignment, sometimes called null coalescing. Here's a few versions of it:
C#:
var a = ( b??c); // a = b if b is truthy, c if b is falsey
// or:
(b ?? c).callMethod(); // nice!
javascript:
var a = b || c;
(b || c).callMethod();
python/ruby:
a = b || c
// lower precedence (i.e. only if b isn't defined, not "falsey")
a = b or c
php:
$a = b || c;
$a = b or c; // just like python
Written by Nick Jacob
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#