DEV Community

Adam Cuppy for ZEAL

Posted on

My favorite code-based expression

For whatever reason, I love writing Ruby's or/equal operator (||=). Said in human-speak, if the variable on the left is null then assign it the value on the right; otherwise, leave the variable on the left as is: @left ||= 'right'

Simplicity

The concept of or/equal exists throughout life, "Is the oven on? No, then set it to 350 degrees." We get it. The flow is natural.

Interpretability

The syntax is easy to parse. If you're familiar with || meaning or and = meaning should now equal then combining them leads down a logical path towards or should now equal. Neat!

What's your favorite programming expression/method/operator?

Post and example. Why do you love writing it?

Top comments (0)