I am working with a legacy method that filters out some data and returns an array of elements.
Simplified version looks like:
def filtering_rules
items = []
items = apply_rule_one
return items if items == 3
items = apply_rule_two
return items if items == 3
items = apply_rule_three
return items if items == 3
end
I need to add a logger before apply_rule three is run so I did this:
def filtering_rules
items = []
items = apply_rule_one
return items if items == 3
items = apply_rule_two
return items if items == 3
Rails.logger("Rule 1 and 2 failed to return data moving to rule 3") if items.empty?
items = apply_rule_three
return items if items == 3
end
My tests are passing and things are working. But the code is not DRY and the logger inside the rules filter is ugly. Any suggestions as far as patterns or best practices are concerned?
return items if items == 3, so you always return 3?return items if items == 3