Open aspiers opened 10 years ago
:+1: Sounds good to me.
If some_flag
is not a boolean, the behavior of |
and ||
are very different.
@mvz That's exactly the point I was making.
@aspiers D'oh, the whole bit about 'traditionally' threw me off, and I totally missed 'for boolean arithmetic' at the top.
Haha, no worries ;-)
It's also worth noting that even in the realm of boolean arithmetic they're not strictly equivalent. Namely the boolean operators will short circuit, and the bitwise operators will not:
# executes the predicate
true | predicate_with_side_effect?
# does not execute the predicate
true || predicate_with_side_effect?
This could actually have a lot of unintended effects for using |=
instead of ||=
. For instance in your original example if some_flag
is already truthy then ||=
would skip execution of do_something
while |=
would cause do_something
to execute.
|
and||
both work for boolean arithmetic, e.g. in||=
could be interchanged with|=
. Similarly with&
and&&
. However, traditionally the single character operators|
and&
are used for bitwise arithmetic, whereas the double character operators||
and&&
are used for boolean arithmetic. I can't think of a good reason for breaking from this tradition, whereas adhering to tradition follows the Principle of Least Surprise, therefore I'm considering submitting a pull request to add this guideline. However I'd welcome other opinions before I do so.