It is better to write if the condition

Is there a better way to write this condition down?

if (self.expense.nil? && self.income.nil?) || (!self.expense.nil? && !self.income.nil?)
  puts "test"
end
+3
source share
3 answers

You check if the expressions are true or false. Try the following:

if (self.expense.nil? == self.income.nil?)

You don't care if they are true or false, they just are the same.

+18
source

Despite the logic, since you use if to check for zero, you can shorten the code as follows:

if ((expense && income) || (!expense && !income))
  puts "test"
end

Correct me if I am wrong.

+1
source

You can use Demorgan law for the second half of the if condition

therefore (!self.expense.nil? && !self.income.nil?)it becomes!(self.expense.nil? || self.income.nil?)

0
source

Source: https://habr.com/ru/post/1759001/


All Articles