text
stringlengths 0
444
|
|---|
NOTE: Whether organizing control flow with `and` and `or` is a good idea has been a controversial topic in the community for a long time. But if you do, prefer these operators over `&&`/`||`. As the different operators are meant to have different semantics that makes it easier to reason whether you're dealing with a logical expression (that will get reduced to a boolean value) or with flow of control.
|
.Why is using `and` and `or` as logical operators a bad idea?
|
****
|
Simply put - because they add some cognitive overhead, as they don't behave like similarly named logical operators in other languages.
|
First of all, `and` and `or` operators have lower precedence than the `=` operator, whereas the `&&` and `||` operators have higher precedence than the `=` operator, based on order of operations.
|
[source,ruby]
|
----
|
foo = true and false # results in foo being equal to true. Equivalent to (foo = true) and false
|
bar = false or true # results in bar being equal to false. Equivalent to (bar = false) or true
|
----
|
Also `&&` has higher precedence than `||`, where as `and` and `or` have the same one. Funny enough, even though `and` and `or`
|
were inspired by Perl, they don't have different precedence in Perl.
|
[source,ruby]
|
----
|
true or true and false # => false (it's effectively (true or true) and false)
|
true || true && false # => true (it's effectively true || (true && false)
|
false or true and false # => false (it's effectively (false or true) and false)
|
false || true && false # => false (it's effectively false || (true && false))
|
----
|
****
|
=== Multi-line Ternary Operator [[no-multiline-ternary]]
|
Avoid multi-line `?:` (the ternary operator); use `if`/`unless` instead.
|
=== `if` as a Modifier [[if-as-a-modifier]]
|
Prefer modifier `if`/`unless` usage when you have a single-line body.
|
Another good alternative is the usage of control flow `&&`/`||`.
|
[source,ruby]
|
----
|
# bad
|
if some_condition
|
do_something
|
end
|
# good
|
do_something if some_condition
|
# another good option
|
some_condition && do_something
|
----
|
=== Multi-line `if` Modifiers [[no-multiline-if-modifiers]]
|
Avoid modifier `if`/`unless` usage at the end of a non-trivial multi-line block.
|
[source,ruby]
|
----
|
# bad
|
10.times do
|
# multi-line body omitted
|
end if some_condition
|
# good
|
if some_condition
|
10.times do
|
# multi-line body omitted
|
end
|
end
|
----
|
=== Nested Modifiers [[no-nested-modifiers]]
|
Avoid nested modifier `if`/`unless`/`while`/`until` usage.
|
Prefer `&&`/`||` if appropriate.
|
[source,ruby]
|
----
|
# bad
|
do_something if other_condition if some_condition
|
# good
|
do_something if some_condition && other_condition
|
----
|
=== `if` vs `unless` [[unless-for-negatives]]
|
Prefer `unless` over `if` for negative conditions (or control flow `||`).
|
[source,ruby]
|
----
|
# bad
|
do_something if !some_condition
|
# bad
|
do_something if not some_condition
|
# good
|
do_something unless some_condition
|
# another good option
|
some_condition || do_something
|
----
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.