Reputation: 15491
Im refactoring my older code parts, have lots of returns with multi line like:
if ...
return false
else
return true
end
How could one refactor to use a single line and return true or false?
Upvotes: 3
Views: 6771
Reputation: 43
Following previous answer, but using return indeed:
return foo ? false : true
Upvotes: 0
Reputation: 115521
Say foo
is what is on the right of your if
, then you can replace with:
foo ? false : true
This is known as the ternary operator.
Notice that in your case you could simply do:
!foo
Upvotes: 8